5 ms·
"2. ubiquitous laziness can lead to some really subtle bugs" I find myself implementing Iterable wrappers more often in Java now, largely due to the influence
by jimbokun 16y ago
"2. ubiquitous laziness can lead to some really subtle bugs"
I find myself implementing Iterable wrappers more often in Java now, largely due to the influence of Clojure lazy seqs. Then I can use the abbreviated for loop syntax, for example, without creating a ridiculously large ArrayList in memory.
I know that the Iterator pattern has been around for a long time, but I never realized how broadly it could be applied to use high level iteration constructs while keeping a minimal memory footprint, before I got used to Clojure.
So Clojure has made me a fan of lazy evaluation, even in Java.
- cageface 16y agoGood point. Even if you don't wind up using Clojure for real work there are certainly things to be learned from it.
- rdtsc 16y agoIt is also very easy in Python: Use generator list comprehensions: (x for x in xs) instead of building the full list: [x for x in xs] Use generators instead of plain functions when you can. Learn to use itertools module: http://docs.python.org/library/itertools.html http://docs.python.org/library/itertools.html I use these often: * chain() * imap() * groupby() * takewhile() * tee() * izip() * izip_longest() * permutations() You can make you code quite "lazy" if you practice enough, and eventually it just becomes a habit. Whenever you do anything you start asking yourself : "can I do this in a lazy way?"
- weavejester 16y agoIterators in Python are slightly different from lazy lists in languages like Haskell and Clojure. In Python, iterators are stateful; calling next() moves the iterator on, discarding the previous head. In Clojure, a seq has no state, and the head is not automatically discarded when the next item is accessed.