Dallycot

 view release on metacpan or  search on metacpan

lib/Dallycot/Value/Stream.pm  view on Meta::CPAN

package Dallycot::Value::Stream;
our $AUTHORITY = 'cpan:JSMITH';

# ABSTRACT: A linked list of values with a possible generator

use strict;
use warnings;

# RDF List
use utf8;
use Readonly;

Readonly my $HEAD         => 0;
Readonly my $TAIL         => 1;
Readonly my $TAIL_PROMISE => 2;

use parent 'Dallycot::Value::Collection';

use experimental qw(switch);

use Promises qw(deferred);

sub new {
  my ( $class, $head, $tail, $promise ) = @_;
  $class = ref $class || $class;
  return bless [ $head, $tail, $promise ] => $class;
}

sub is_defined { return 1 }

sub is_empty {return}

sub to_rdf {
  my($self, $model) = @_;

  my @things;
  my $root = $self;
  push @things, $root -> [0]->to_rdf($model);
  while($root -> [1]) {
    $root = $root->[1];
    push @things, $root->[0]->to_rdf($model);
  }
  if($root -> [2]) {
    return $model -> list_with_promise(@things, $root->[2]);
  }
  else {
    return $model -> list(@things);
  }
}

sub prepend {
  my ( $self, @things ) = @_;

  my $stream = $self;

  foreach my $thing (@things) {
    $stream = __PACKAGE__->new( $thing, $stream );
  }
  return $stream;
}

sub as_text {
  my ($self) = @_;

  my $text  = "[ ";
  my $point = $self;
  $text .= $point->[$HEAD]->as_text;
  while ( defined $point->[$TAIL] ) {
    $point = $point->[$TAIL];
    if ( defined $point->[$HEAD] ) {
      $text .= ", ";
      $text .= $point->[$HEAD]->as_text;
    }
  }
  if ( defined $point->[$TAIL_PROMISE] ) {
    $text .= ", ...";
  }
  return $text . " ]";
}

sub calculate_length {
  my ( $self, $engine ) = @_;

  my $d = deferred;

  my $ptr = $self;

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 2.635 seconds using v1.00-cache-2.02-grep-82fe00e-cpan-48ebf85a1963 )