Re:SICP translated to JavaScript
by xiaoma on Saturday September 14, 2013 @03:41PM
(#6386804)
JavaScript doesn't have them either, until you include a functional toolchain library like Underscore. Does Perl have any similar library?
[ Parent
| on HN ]
JS 1.7, already available in some browsers and bleeding edge Node, has iterators and generators.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guid...
There's List::Util and List::MoreUtils: http://search.cpan.org/~adamk/List-MoreUtils-0.33/lib/List/M...
Re:SICP translated to JavaScript
by simcop2387 on Saturday September 14, 2013 @05:25PM
(#6387116)
Not really. As ~btilly suggested in another post, perl really doesn't have anything like it. The closest thing that exists is using a code reference.
sub new_iter {
my $i = 0;
return sub {$i++}
}
my $iter = new_iter();
while(my $number = $iter->()) {
say $number;
}
This is of course a trivial example, but it does give a bit of an idea of the only way Perl has to be able to implement them, essentially anonymous functions and closures. This is useful, but it doesn't make it easy to take say a red-black tree, and iterate over the entire set of values in it (though with higher level languages like perl, you probably wouldn't implement much with a red-black tree, maybe a k-tree would be better?)There are plenty of iterator/generator libraries on CPAN. My personal favourite is Coro::Generator - https://metacpan.org/module/Coro::Generator
Re: Underscore... there is a Perl port - http://vti.github.io/underscore-perl/