реализация на perl на hand-made ленивых списках
#!/usr/bin/perl
use strict;
use warnings;
sub node
{
my($h, $t) = @_;
return [$h, $t];
}
sub head
{
my $s = shift;
return $s->[0];
}
sub promise(&)
{
return $_[0];
}
sub is_promise
{
return UNIVERSAL::isa($_[0], 'CODE');
}
sub tail
{
my $s = shift;
if ( is_promise($s->[1]) )
{
$s->[1] = $s->[1]->();
}
return $s->[1];
}
sub drop
{
my $h = head($_[0]);
$_[0] = tail($_[0]);
return $h;
}
sub union
{
my($h, @s) = grep $_, @_;
return unless $h;
return $h unless @s;
node(head($h), promise { union(@s, tail($h)); });
}
sub fread_iter
{
my $fh = shift;
my $str = <$fh>;
if ( !defined $str )
{
close $fh;
return undef;
}
return node($str, promise { fread_iter($fh) });
}
sub make_file_iter
{
my $fname = shift;
my $fh;
open($fh, '<', $fname) or return undef;
return fread_iter($fh);
}
# MAIN
my $iter = union( map { make_file_iter $_ } @ARGV);
my $str;
while( defined($str = drop($iter)) )
{
print $str;
}
Комментариев нет:
Отправить комментарий