From: Trans Date: 2005-11-04T10:07:22+09:00 Subject: Re: Anonymous methods, blocks etc. (Cont. 'default block params') MenTaLguY wrote: > 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. I see. I not thinking just of the lazy evaluation. I'm also thinking of it in terms of use as typcial lambda (but with the implict execution) in which case dynamic behavior could play more of a role --i.e. if any data being accessed could have changed in the interm between subsquent calls. But I understand what you mean, that *as a lazy evaluator* caching is typically expected. > 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... Hey that's seems pretty cool. Let me see if I understand correctly. Basically you've set up the calculation of rjusting every line, bu thaven't actually yet done so. Once the result of rjust_lines is used they will get calculated, but not unitl then --and if thy're never used, well you just saved some cycles (well if they cost more then seeting this up) Is that right? Also these kind of remind me of persistant local vars. Btw, I just recalled someone mentioning a #once method. Do you recall that? How does that compare? As for memoize, I guess I was thinking that a modified #memoize would just activate a flag in the Thunk object. Seems to me your approach might be able to improve on memoize itself (if it is as efficent). Intereseted in hearing how it proceeds. T.