Created
August 31, 2012 03:49
-
-
Save seratch/3548874 to your computer and use it in GitHub Desktop.
scala.pl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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