From: Brian Candler Date: 2008-11-02T21:35:42+09:00 Subject: Re: Iterator objects and lazy evaluation Yuh-Ruey Chen wrote: > Suppose I have a function that computes the union of two arrays > whenever each array is changed. Using a perhaps more possible syntax: > > attr_accessor :array_a > > def foo > lazy_union = lazy_expr { lazy_depends(:array_a) + > lazy_depends(:array_b) } > @array_a = [1,2] > @array_b = [3,4] > p lazy_union # [1,2,3,4] > @array_a[1] = 5 > p lazy_union # [1,5,3,4] > end There is an Observable pattern (observer.rb); however it doesn't automatically notice when an instance variable has been changed, and I don't know of a hook which detects that. In any case, it's possible for the array to remain unchanged (i.e. it contains references to the same objects) whilst those objects still mutate: @array_a = [1, "foo"] ... @array[1] << "bar" Actually, your "lazy_union" could be fine with this, since the union object would also contain the mutated object, but a more general case like lazy_concat which depends on the *values* of the objects would have serious problems. So, do you want your lazy dependency system to notice this? What about objects held an arbitary number of levels deep? I'm not sure that Ruby really suits itself well to this sort of pattern, because of the mutable nature of objects. Perhaps you want to look at Erlang instead. -- Posted via http://www.ruby-forum.com/.