Created
December 26, 2011 23:42
-
-
Save gatlin/1522297 to your computer and use it in GitHub Desktop.
Functional definition of map in Perl
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; | |
sub myMap (&@) { | |
my ($fn, @lst) = @_; | |
return () unless @lst; | |
return (sub { local $_=$_[0];$_[1]->() }->($lst[0],$fn), | |
&myMap( $fn, @lst[1..$#lst] )); | |
} | |
# usage: | |
# myMap { print "$_\n" } (1,2,3,4); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This does not handle scalar context. It blows up in memory usage and destroys your function stack quickly. It should be a good study of Perl syntax for beginners, though.