Mar. 23rd, 2006
Factory and Iterator patterns
Mar. 23rd, 2006 11:51 amProbably I should have posted this somewhere else, like in lambda community, but I know they think of patterns as of stupid struts for clueless "software engineers".
Why would I need a Factory? To do lazy evaluation, right? Build the object when I need it, not all in advance.
Now with Iterator you can do the same, if we imagine an Iterator that keeps only one instance in mind. It returns it when asked, and then stops. Singleton iterator, so to say.
And my question is, for the purpose of lazy evaluation techniques (e.g. streams), would not it make sense to forget about Factories and think of Iterators only?
Why would I need a Factory? To do lazy evaluation, right? Build the object when I need it, not all in advance.
Now with Iterator you can do the same, if we imagine an Iterator that keeps only one instance in mind. It returns it when asked, and then stops. Singleton iterator, so to say.
And my question is, for the purpose of lazy evaluation techniques (e.g. streams), would not it make sense to forget about Factories and think of Iterators only?
Scheme streams in Java
Mar. 23rd, 2006 01:34 pmSee http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-24.html for reference. This book contains the original code in Scheme.

Now, the Java code - I'd rather have it beautified in HTML, but could not find any online tool that would do that... (under cut) ( Read more... )
(define (sieve stream) (cons-stream (stream-car stream) (sieve (stream-filter (lambda (x) (not (divisible? x (stream-car stream)))) (stream-cdr stream))))) (define primes (sieve (integers-starting-from 2)))And below is my Java code (with some questionable features, worth discussing) that implements the same sieve, doing the same trick:

Now, the Java code - I'd rather have it beautified in HTML, but could not find any online tool that would do that... (under cut) ( Read more... )
Дорогие шизофренды (going functional)
Mar. 23rd, 2006 03:14 pmGoing functional, I just stopped understanding why, in Java, I have to concoct an "object" to pass an obvious dependency - I mean, to pass a function. Instead, have to pass a "Map". There's a big difference between a function and a Map. A Map returns a meager null if the key happens to be absent. All I actually need is a closure that does what I tell it to do. Javascript is so much more convenient! I'm not a typoholic anymore.