Skip to content

Instantly share code, notes, and snippets.

@gatlin
Created December 26, 2011 23:42
Show Gist options
  • Select an option

  • Save gatlin/1522297 to your computer and use it in GitHub Desktop.

Select an option

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

gatlin commented Dec 27, 2011

Copy link
Copy Markdown
Author

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