Last active
December 16, 2015 13:49
-
-
Save sycobuny/5444365 to your computer and use it in GitHub Desktop.
A perl rewrite of the second base list implementation from http://stevelosh.com/blog/2013/03/list-out-of-lambda/
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 v5.14; | |
my $empty_list = sub { | |
shift->(undef, undef, 1); | |
}; | |
my $prepend = sub { | |
my $el = shift; | |
my $list = shift; | |
sub { shift->($el, $list, 0) } | |
}; | |
my $head = sub { | |
shift->(sub { shift }) | |
}; | |
my $tail = sub { | |
shift->(sub { shift; shift }) | |
}; | |
my $is_empty = sub { | |
shift->(sub { shift; shift; shift }) | |
}; | |
# example of output to verify it works | |
my $names = $prepend->('Alice', $prepend->('Bob', $prepend->('Candice', | |
$empty_list))); | |
say $is_empty->($names) . " should be 0"; | |
say $head->($names) . " should be Alice"; | |
say $tail->($names) . " should be a coderef"; | |
say $head->($tail->($names)) . " should be Bob"; |
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 v5.14; | |
use perl5i::2; | |
no strict 'vars'; | |
$empty_list = func($selector) { | |
$selector->(undef, undef, 1); | |
}; | |
$prepend = func($el, $list) { | |
func($selector) { $selector->($el, $list, 0) } | |
}; | |
$head = func($list) { | |
$list->(func($h) { $h }) | |
}; | |
$tail = func($list) { | |
$list->(func ($h, $t) { $t }) | |
}; | |
$is_empty = func($list) { | |
$list->(func($h, $t, $e) { $e }) | |
}; | |
# example of output to verify it works | |
my $names = $prepend->('Alice', $prepend->('Bob', $prepend->('Candice', | |
$empty_list))); | |
say $is_empty->($names) . " should be 0"; | |
say $head->($names) . " should be Alice"; | |
say $tail->($names) . " should be a coderef"; | |
say $head->($tail->($names)) . " should be Bob"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment