Skip to content

Commit

Permalink
added experimental oterate() function with docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Chantreux committed Aug 18, 2015
1 parent 7aa24ec commit df6fc61
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -9,3 +9,4 @@ Current version is broken with perl < 5.16 because
or just *remove* `lines` from the perlude core (it is a very wrong place)
* yada (`...`) was introduced in perl 5.14 AFAIK
* investigate possible bug in range with 1 argument
* missing tests for oterate
20 changes: 19 additions & 1 deletion lib/Perlude.pm
Expand Up @@ -18,15 +18,33 @@ our @EXPORT = qw<
nth
chunksOf
as_open
oterate
>;

# ABSTRACT: Shell and Powershell pipes, haskell keywords mixed with the awesomeness of perl. forget shell scrpting now!
# ABSTRACT: Shell and Powershell pipes, haskell keywords mixed with the awesomeness of perl. forget shell scripting now!

use Carp;

our $VERSION = '0.61';
our $EXHAUSTED = sub {()};

sub oterate {
my ($iter,$filter,$self) = @_;
my ( $i, @i ) = ref $iter ? @$iter : $iter ;
my ( $f, @f) = ref $filter ? @$filter : $filter;
my $exhausted = 0;
my @v;
sub {

return if $exhausted;

do { @v and return shift @v }
while (@v = $self->$i(@i));

$exhausted=1;
return;
}
};

sub _natural_from {
my ($fn, $pos, $v) = @_;
Expand Down
45 changes: 45 additions & 0 deletions lib/Perlude.pod
Expand Up @@ -238,6 +238,51 @@ L<http://hackage.haskell.org/packages/archive/base/latest/doc/html/Prelude.html#

=head2 generators

=head3 oterate ($iter,$filter,$self) # EXPERIMENTAL

it's B<Oterate> (like Object Iterate). the basic boring pattern is

my $tap = TAP::Parser->new({qw( source test)});
while (my $e = $tap->next) {
if ($e->is_ok) { say $e->as_string }
}

replacing it by perlude gives you power but the code
still isn't pleasant to read.

now {say $_->as_string}
filter {$_->is_ok}
sub {
state $tap = TAP::Parser->new({qw( source test)});
$tap->next // ()
}

Perlude + L<https://metacpan.org/pod/curry> does it better

now {say $_->as_string}
filter {$_->is_ok}
TAP::Parser
-> new({qw( source test)})
-> curry::next;

oterate does the same, plus adds the filter into the generator

oterate qw(next is_ok)
, TAP::Parser->new({qw( source test)})

using array ref, you can also add arguments to both the iterator and the
filter:

oterate
, [qw( next file )]
, [qw( is readable )]
, Some->new

is like

filter {$_->is('readable')} Some->new->curry::next('file')


=head3 range $begin, [ $end, [ $step ] ]

A range of numbers from $begin to $end (infinity if $end isn't set) $step by $step.
Expand Down

0 comments on commit df6fc61

Please sign in to comment.