From: MenTaLguY Date: 2005-11-04T09:06:03+09:00 Subject: Re: Anonymous methods, blocks etc. (Cont. 'default block params') On Fri, 2005-11-04 at 07:12 +0900, Trans wrote: > I see. Well, I think given the implementation using SimpleDelegator > that's probably true. In general though I disagree that it would be so > rare --if a highly efficent mechinims were in place. In that case using > #memoize would make sense and could be consitantly used across the > board with procs, promises and methods. I would expect it to be rare not because of the implementation, but because of the expected semantics for lazy evaluation and the way it is typically used. This is a bit of a contrived/simplified example, but let's say you want to right-justify a paragraph (list) of lines. def rjust_lines( lines ) result = nil result = promise { max_length = 0 justified_lines = lines.map { |line| max_length = line.length if line.length > max_length promise { line.rjust( result[0] ) } } [ max_length, justified_lines ] } result[1] end result[0] is the maximum line length, and result[1] is an array of right-justified lines. Because the computations are "one-shot", the result array can be garbage-collected once all of the lines have been demanded or become collectable themselves (which is an advantage over memoize...). Also, we only have to iterate over the list once. Note that this won't actually work yet with the Thunk class I posted earlier, because of limitations of SimpleDelegate. I've got something better in the works, though... -mental