3 ms·
I know HOP. It depresses me how much of HOP is irrelevant in Python because they have proper iterators. And how few Perl programmers realize that these days t
by btilly 13y ago
I know HOP.
It depresses me how much of HOP is irrelevant in Python because they have proper iterators. And how few Perl programmers realize that these days the lack is a major omission in the language.
- Sukotto 13y agoI'm a longtime perl user, and I don't know where my blind spots are. Are there any particular resources you would recommend for someone like me to get going quickly with python? In particular ones that specifically address the type of blind spot you mention?
- btilly 13y agoThe Python tutorial teaches the language fairly well: http://docs.python.org/2/tutorial/ http://docs.python.org/2/tutorial/ The iterator section is http://docs.python.org/2/tutorial/classes.html#iterators http://docs.python.org/2/tutorial/classes.html#iterators. Here it is quickly. Suppose that an object has a __iter__ method which will return something with a next method that will, when it has exhausted the iterator, call StopIteration. Then all native looping constructs can loop over that object and it will just work. If you call a function that internally calls yield and nowhere calls return, then you get back a generator, which is an object with those methods. There is also syntax to construct such generators inline without even an explicit function. All of the closure based looping stuff in HOP which supports potentially infinite streams? It is all built in!
- gonnakillme 13y agos/call StopIteration/raise StopIteration/g
- StavrosK 13y agoI wrote a tutorial a while ago exactly for this purpose: http://www.stavros.io/tutorials/python/ http://www.stavros.io/tutorials/python/
- xiaoma 13y agoJavaScript 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/