Quoting Trans <transfire / gmail.com>:

> Implict lambdas are useful are useful as convenient lazy
> evaluators.

Hmm, I'm not really convinced they're useful in that respect -- it
seems to me that they aren't really lazy enough.  Evaluation gets
forced too early (even when the object is only referenced casually),
and too often (every time the object is referenced).

Maybe this sort of behavior:

 class X
   def implicit_call ; puts "foo" ; end
 end

 a = X::new
 b = [ a ] # prints "foo"
 c = [ a ] # prints "foo" again

...is useful sometimes, but it's not what most people mean by lazy
evaluation.

Lazy evaluation is normally implemented with a data structure like
this:

 class Thunk
   def initialize( &computation )
     @computation = computation
   end
   def force
     return @result unless @computation
     @result = @computation.call
     @computation = nil
     @result
   end
 end

 def promise( &computation ) ; Thunk::new &computation ; end

Most lazy languages simply do this behind the scenes.

To push it similarly underground in Ruby, you'd rewrite it as
something like (not perfect, but you get the idea...):

 require 'delegate'

 class Thunk < SimpleDelegator
   def initialize( &computation )
     @computation = computation
     super nil
   end
   def __getobj__
     return super unless @computation
     __setobj__ @computation.call
     @computation = nil
     super
   end
 end

 def promise( &computation ) ; Thunk::new &computation ; end

This lets you do stuff like:

 def foo
   value = promise do
     puts "Forced!"
     42
   end
   puts "Promised a computation."
   value
 end

 blah = foo
 puts "Wait for it..."

 values = (0..9).map do |i|
   promise do
     puts "Computing #{i} * 2"
     i * 2
   end
 end

 k = values[3]
 k # no effect
 p values[2]
 p values[4]
 p values[2]
 p k

 p blah

Which will output:

 Promised a computation.
 Wait for it...
 Computing 2 * 2
 4
 Computing 4 * 2
 8
 4
 Computing 3 * 2
 6
 Forced!
 42

Note that no computation is performed until its result is required,
and that each computation is performed at most once.

-mental