Skip to content

Instantly share code, notes, and snippets.

@gatlin
Created December 26, 2011 23:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gatlin/1522297 to your computer and use it in GitHub Desktop.
Save gatlin/1522297 to your computer and use it in GitHub Desktop.
Functional definition of map in Perl
#!/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);
@gatlin
Copy link
Author

gatlin commented Dec 27, 2011

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment