Skip to content

Instantly share code, notes, and snippets.

@marcoonroad
Created December 9, 2014 16:03
Show Gist options
  • Save marcoonroad/e99b3b8f647025a1cf25 to your computer and use it in GitHub Desktop.
Save marcoonroad/e99b3b8f647025a1cf25 to your computer and use it in GitHub Desktop.
Some perl 5 hacking...
# small hacking over symbol table
use feature qw| state |;
sub say { print @_, "\n" }
sub _ (&) { $_[ 0 ] }
sub curry {
my $f = shift;
_ {
my @a = @_;
_ { &$f (@a, @_) }
}
}
sub compose {
my $f = shift;
_ {
my @g = @_;
_ {
my @r = &$f (@_);
@r = &{ shift @g }(@r) while @g;
@r;
}
}
}
sub take {
my $value = shift;
push @togather, $value;
}
sub gather (&) {
my $block = shift;
local @togather;
$block -> ( );
@togather;
}
sub iter {
my @xs = @_;
_{
state $i = 0;
@xs[ $i++ ];
}
}
sub loop (&@) {
my $block = shift;
my @xs = @_;
for my $x (@xs) {
$block -> ($x);
}
}
# tests
my $hello = curry (\&say) -> ("Hello,", " ", "'nice'");
&$hello (" ", "World!");
_ { &{ $_[0] }( ) } -> (_ { &{"say"}("See ya later...") });
my $incr = _ { $_[ 0 ] + 1 };
my $square = _ { $_[ 0 ] ** 2 };
say &{ compose ($incr) -> (_ { $_[ 0 ] + 5 }, $square) }(4);
${"more"} = iter gather {
take "Hello";
take "World!";
take "You found 42!";
};
my $value;
say $value while $value = $more -> ( );
loop {
my $i = shift;
say "Hello, World! -> $i.";
} 1 ... 5;
# end of script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment