Skip to content

Instantly share code, notes, and snippets.

@seratch
Created August 31, 2012 03:49
Show Gist options
  • Save seratch/3548874 to your computer and use it in GitHub Desktop.
Save seratch/3548874 to your computer and use it in GitHub Desktop.
scala.pl
#!/usr/bin/env perl
use strict;
use warnings;
package Seq;
sub apply {
my ($class, @seq) = @_;
bless { seq => \@seq }, $class;
}
sub map {
my $self = shift;
my $f = shift;
Seq->apply(map &$f($_), @{$self->{seq}});
}
sub flatten {
my $self = shift;
Seq->apply(map ref eq 'ARRAY' ? @$_ : $_, @{$self->{seq}});
}
sub flat_map {
my $self = shift;
my $f = shift;
Seq->apply(map &$f($_), @{$self->{seq}})->flatten();
}
sub foreach {
my $self = shift;
my $f = shift;
&$f($_) foreach @{$self->{seq}};
}
package main;
sub echo {
my $arg = shift;
print $arg, $/;
}
sub twice {
my $arg = shift;
$arg * 2;
}
my $seq = Seq->apply(1,2,3,4,5);
$seq->foreach(\&echo);
$seq->map(\&twice)->foreach(\&echo);
Seq->apply((1,2),3,(4,5))->flat_map(\&twice)->foreach(\&echo);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment