1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
package Perlude::Lazy;
use strict;
use warnings;
use 5.10.0;
use Carp qw< croak >;
use Exporter qw< import >;
our $VERSION = '0.0';
our @EXPORT = qw<
enlist unfold
fold
takeWhile take drop
filter apply
now
cycle range
tuple
lines
concat
>;
use Carp;
# End-of-list value: always return itself, with no data
{
my $NIL;
$NIL = sub { $NIL };
sub NIL() { $NIL }
}
# interface with the Perl world
sub enlist (&) {
my ($i) = @_;
my ( $l, @b );
$l = sub {
if (@_) {
my $n = shift;
return ( $l, @b[ 0 .. $n - 1 ] ) if @b >= $n; # there's enough
push @b, my @v = $i->(); # need more
push @b, @v = $i->() while @b < $n && @v; # MOAR
return ( $l, @b < $n ? @b : @b[ 0 .. $n - 1 ] ); # give it a peek
}
else {
return ( $l, shift @b ) if @b; # use the buffer first
push @b, $i->(); # obtain more items
return @b ? ( $l, shift @b ) : NIL;
}
};
}
sub concat {
my ($l, @ls)= @_;
my @v;
my $r;
$r = sub {
while ($l) {
( $l, @v ) = $l->();
return ($r,@v) if @v;
$l = shift @ls;
}
};
$r
}
sub unfold (@) {
my @array = @_;
enlist { @array ? shift @array : () };
}
sub fold ($) {
my ($l) = @_;
my @v;
unless (wantarray) {
if ( defined wantarray ) {
my $n = 0;
$n += @v while 1 < ( ( $l, @v ) = $l->() );
return $n;
}
else {
# The real lazy one: when called in scalar context, values are
# ignored:
# undef while defined ( $l = $l->() );
# But producers must be able to handle that
# So keep that for later and use the eager implementation for now
undef while 1 < ( ( $l, @v ) = $l->() );
return;
}
}
my @r;
push @r, @v while 1 < ( ( $l, @v ) = $l->() );
@r;
}
# stream consumers (lazy)
sub takeWhile (&$) {
my ( $cond, $l ) = @_;
my $m;
$m = sub {
1 < ( ( $l, my @v ) = $l->() ) or return ($l);
return $cond->() ? ( $m, @v ) : ( sub { ( $l, @v ) } ) for @v;
};
}
sub filter (&$) {
my ( $cond, $l ) = @_;
my $m;
$m = sub {
while (1) {
1 < ( ( $l, my @v ) = $l->() ) or return ($l);
$cond->() and return ($m, @v) for @v;
}
};
}
sub take ($$) {
my ( $n, $l ) = @_;
my $m;
$m = sub {
$n-- > 0 or return ($l);
1 < ( ( $l, my @v ) = $l->() ) or return ($l);
( $m, @v );
}
}
sub drop ($$) {
my ( $n, $l ) = @_;
fold take $n, $l;
$l;
}
sub apply (&$) {
my ( $code, $l ) = @_;
my $m;
$m = sub {
1 < ( ( $l, my @v ) = $l->() ) or return $l;
( $m, map $code->(), @v );
}
}
# stream consumers (exhaustive)
sub now (&$) {
my ( $code, $l ) = @_;
my @b;
while (1) {
1 < ( ( $l, my @v ) = $l->() ) or return pop @b;
@b = map $code->(), @v;
}
}
# stream generators
sub cycle (@) {
(my @ring = @_) or return NIL;
my $index = -1;
enlist { $ring[ ( $index += 1 ) %= @ring ] }
}
sub range ($$;$) {
my $begin = shift // croak "range begin undefined";
my $end = shift;
my $step = shift // 1;
return NIL if $step == 0;
$begin -= $step;
my $l;
return $l = defined $end
? $step > 0
? sub { ( ( $begin += $step ) <= $end ) ? ( $l, $begin ) : ($l) }
: sub { ( ( $begin += $step ) >= $end ) ? ( $l, $begin ) : ($l) }
: sub { ( $l, $begin += $step ) };
}
sub tuple ($$) {
my ( $n, $l ) = @_;
croak "$n is not a valid parameter for tuple()" if $n <= 0;
my $m;
$m = sub {
$l = take $n, $l;
my (@r, @v);
push @r, @v while 1 < ( ( $l, @v ) = $l->() );
@r ? ( $m, \@r ) : ( $l )
}
}
sub lines {
# private sub that coerce path to handles
state $fh_coerce = sub {
my $v = shift;
return $v if ref $v;
open my ($fh),$v;
$fh;
};
my $fh = $fh_coerce->( pop );
# only 2 forms accepted for the moment
# form 1: lines 'file'
@_ or return enlist { <$fh> // () };
# confess if not 2nd form
$_[0] eq 'chomp' or confess 'cannot handle parameters ' , join ',', @_ ;
# lines chomp => 'file'
enlist {
defined (my $v = <$fh>) or return;
chomp $v;
$v;
}
}
1;
=head1 Perlude::Lazy
An experimentation of implementing real lazy lists in Perl5.
For real world usecases, please use Perlude instead.
=head1 SYNOPSIS
Haskell prelude miss you when you write perl stuff? Perlude is a port of the
most common keywords. Some other keywords where added when there is no haskell
equivalent.
Example: in haskell you can write
nat = [0..]
is_even x = ( x `mod` 2 ) == 0
evens = filter is_even
main = mapM_ print
$ take 10
$ evens nat
in perlude, the same code will be:
use Perlude;
my $nat = enlist { state $x = 0; $x++ };
sub is_even { ($_ % 2) == 0 }
sub evens { filter {is_even} shift }
traverse {say} take 10, evens $nat
=head1 FUNCTIONS
all the Perlude documentation is relevant. just replace sub by enlist
=cut
|