is similar the bind rule for the Maybe monad. (I say “similar” because if you want to more fully adhere to Maybe-monad semantics, you’ll need to make sure that false is not mistaken for a “nothing” value:

{|o,l| o.nil? ? nil : o.rrss(&l) }

You’ll also need to bar side effects to be able to satisfy the monad laws.)

In effect, we are defining nil to represent the result of a computation that returns nothing (fails), and we’re defining any non-nil value to represent the result of a computation that succeeds in producing a value. Then our bind rule takes the result o of a previous computation (successful or failed) and a method l that, given a successfully produced value, computes a new value (or itself fails). From this, bind generates a new computation that:

fails immediately if o represents a failure;

fails after evaluating o.l if the evaluation results in failure; or

returns the successfully computed result of o.l.

A chain of such computations has the nice property that the chain will stop evaluating subsequent l methods after the first failure. (In a non-strict language, it’s easy to make the entire computation halt after the first failure.)