From: gabriele renzi Date: 2005-10-01T23:16:46+09:00 Subject: Re: Lazy evaluaton (was Re: In your opinion....) Robert Klemme ha scritto: > Daniel Lewis wrote: > >> oooh my thread has split :) > > > Better your thread > than your head. > :-) > >> I'm going to be noting all this down, Lazy Evaluation is a subject in >> Functional Programming and AI. > > > ... although it's by far not restricted to that. > > Btw, recently I discovered (better: started using) an interesting idiom > that exploits the fact that both Proc and Hash implement #[]. > > FUN = config_memoized ? > Hash.new do |h,k| > # complex calculate val > h[k] = val > end : > lambda do |k| > # complex calculate val > end > > # later > > x = FUN[y] > > Nothing really fancy I guess but it's nice how seemingly these > integrate. (Now, is this another thread split?) could'nt you just use the default_proc in a Hash? irb(main):001:0> fun= Hash.new do |h,k| irb(main):002:1* p 'calculations' irb(main):003:1> h[k]= rand irb(main):004:1> end => {} irb(main):005:0> fun[10] "calculations" => 0.364681017817929 irb(main):006:0> fun[10] => 0.364681017817929 ok it is inverting the concept, I know but it seem to get the same result :)