3 ms·
JavaScript doesn't have them either, until you include a functional toolchain library like Underscore. Does Perl have any similar library?
by xiaoma 13y ago
JavaScript doesn't have them either, until you include a functional toolchain library like Underscore. Does Perl have any similar library?
- cytzol 13y agoThere's List::Util and List::MoreUtils: http://search.cpan.org/~adamk/List-MoreUtils-0.33/lib/List/MoreUtils.pm http://search.cpan.org/~adamk/List-MoreUtils-0.33/lib/List/M...
- neilk 13y agoJS 1.7, already available in some browsers and bleeding edge Node, has iterators and generators. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guid...
- simcop2387 13y agoNot 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?)
- draegtun 13y agoThere are plenty of iterator/generator libraries on CPAN. My personal favourite is Coro::Generator - https://metacpan.org/module/Coro::Generator https://metacpan.org/module/Coro::Generator Re: Underscore... there is a Perl port - http://vti.github.io/underscore-perl/ http://vti.github.io/underscore-perl/