Mar. 23rd, 2006

juan_gandhi: (Default)
Probably 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?
juan_gandhi: (Default)
See http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-24.html for reference. This book contains the original code in Scheme.
(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... )
juan_gandhi: (Default)
Going 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.

Profile

juan_gandhi: (Default)
Juan-Carlos Gandhi

September 2025

S M T W T F S
 1 23456
78910111213
14151617181920
21222324252627
282930    

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Sep. 5th, 2025 06:04 pm
Powered by Dreamwidth Studios