From: Daniel Lewis Date: 2005-10-01T21:59:53+09:00 Subject: Re: Lazy evaluaton (was Re: In your opinion....) On 01/10/05, Robert Klemme wrote: > Devin Mullins wrote: > > gabriele renzi wrote: > > > >> Think for example of the IO monad: it allows you to state that you > >> should, say, print "hello" before "world". > >> The rest of the program is still happy, functional and lazy, since he > >> does'nt really care about the console output. > > > > The word "lazy", and the impression I was forming about monads from > > reading these posts, inspired this simple doodad. Is this useful? Is > > it already implemented in some other Ruby library out there? > > > > class Lazy > > (instance_methods - %w{__send__ __id__}).each {|meth| undef_method > > meth } def initialize(blk) > > @blk = blk > > end > > def method_missing(*a,&b) > > @inst = @blk.call unless defined? @inst > > @inst.send(*a,&b) > > end > > end > > > > def lazy(&blk) > > Lazy.new blk > > end > > Delegate comes to mind. Not really shorter... > > require 'delegate' > class Lazy < Delegator > alias :init :initialize > > def initialize(&b) > super(nil) > @bl = b > end > > def __getobj__ > if @bl > @obj = @bl.call() > init(@obj) > @bl = nil > end > @obj > end > end > def lazy(&b)Lazy.new(&b)end > > Only advantage seems that one can reuse lib code for delegation and > initialization. > > Kind regards > > robert > > > oooh my thread has split :) I'm going to be noting all this down, Lazy Evaluation is a subject in Functional Programming and AI. Thanks, Daniel.