Acme-IEnumerable

 view release on metacpan or  search on metacpan

lib/Acme/IEnumerable.pm  view on Meta::CPAN

  }
  Carp::confess("Impossible");
}

sub any {
  my ($self, $predicate) = @_;
  $predicate //= sub { 1 };
  my $base = $self->new();
  while (1) {
    my $item = $base->();
    return 0 unless ref $item;
    local $_ = $$item;
    return 1 if $predicate->($_);
  }
  Carp::confess("Impossible");
}

sub reverse {
  my $self = shift;
  Acme::IEnumerable->from_list(reverse $self->to_perl);
}

sub sum {
  my $self = shift;
  return $self->aggregate(0, sub { $_[0] + $_[1] });
}

sub to_perl {
  my $self = shift;
  my @result;
  my $enum = $self->new();
  for (my $item = $enum->(); ref $item; $item = $enum->()) {
    push @result, $$item;
  }
  @result;
}

sub to_list {
  my ($self) = @_;
  Acme::IEnumerable->from_list($self->to_perl);
}

sub for_each {
  my ($self, $action) = @_;
  my $enum = $self->new();
  for (my $item = $enum->(); ref $item; $item = $enum->()) {
    local $_ = $$item;
    $action->($_);
  }
}

#####################################################################
#
#####################################################################
# sub select_many { ... }
# sub contains { ... }
# sub sequence_equal { ... }
# sub distinct { ... }
# sub union { ... }
# sub except { ... }
# sub intersect { ... }
# sub default_if_empty { ... }
# sub single_or_default { ... }
# sub concat { ... }
# sub group_join { ... }
# sub join { ... }
# sub empty { ... }
# sub cast { ... }
# sub to_lookup { ...}
# sub to_dictionary { ... }

#####################################################################
#
#####################################################################
# sub distinct_by { ... }
# sub min_by { ... }
# sub max_by { ... }

# sub to_enumerable { ... }


1;

__END__

=head1 NAME

Acme::IEnumerable - Proof-of-concept lazy lists, iterators, generators

=head1 SYNOPSIS

  use v5.16;
  use Acme::IEnumerable;

  my @sorted = Acme::IEnumerable
    ->from_list(qw/3 2 1/)
    ->where(sub { $_ < 3 })
    ->order_by(sub { $_ })
    ->to_perl;

  say join ' ', @sorted;

=head1 DESCRIPTION

Experimental implementation of a iterator/generator protocol and lazy
lists on top of it, with plenty of generic methods inspired by .NET's
IEnumerable interface and corresponding facilities in Ruby, Python,
and Haskell. Mainly for discussion purposes.

=head2 STATIC METHODS

=over 2

=item from_list

Creates new C<Acme::IEnumerable::List> from the supplied list.

=item range($from [, $count])

Creates a new C<Acme::IEnumerable> with $count integers from $from.



( run in 0.949 second using v1.01-cache-2.11-cpan-39bf76dae61 )