lazy.rb provides lazy evaluation and futures in Ruby.

lazy evaluation...

For lazy evaluation, the facilities are similar to those provided by R5 Scheme. There are two functions: promise (similar to Scheme's delay) which takes a block for later evaluation, and demand (similar to Scheme's force), which forces its evaluation (if necessary) and returns its cached result.

Unlike some Scheme implementations, it is safe to pass ordinary values to demand. Promises are also transparent, meaning that in most cases an evaluated promise is not distinguishable from the actual result object it wraps.

To avoid locking overhead for single-threaded applications, promises will not be threadsafe unless you require 'lazy/threadsafe'.


Additionally, the library provides futures, where a computation is run immediately in a background thread. The IO language has these, and they're pretty neat. Futures can be constructed using future. Based on promises, they are also transparent. An attempt to demand the result of a future will block until the background thread completes.

Since futures require threads, requiring 'lazy/future' will automatically require 'lazy/threadsafe' for you.


lazy.rb is made available under the same license as Ruby.