Module: Prelude::Monad

Defined in:
lib/prelude/monad.rb

Overview

$Id: monad.rb 34 2007-10-23 21:38:09Z prelude $

Defines monadic behavior for anything willing to include it.

Constant Summary collapse

M_STATE =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.join(arr) ⇒ Object



46
47
48
49
50
# File 'lib/prelude/monad.rb', line 46

def Monad.join(arr)
  r = []
  arr.each {|a| r.push *a}
  r
end

Instance Method Details

#bind(f = nil) ⇒ Object Also known as: >>



52
53
54
55
56
57
58
59
60
# File 'lib/prelude/monad.rb', line 52

def bind(f=nil)
  wrap unless M_STATE[object_id]
  if f.kind_of?(Symbol) || f.kind_of?(Method) || f.kind_of?(Proc)
    M_STATE[object_id] = f.to_proc.call(M_STATE[object_id])
    self
  else
    f
  end # if
end

#emptyObject



41
42
43
44
# File 'lib/prelude/monad.rb', line 41

def empty
  M_STATE[object_id] = nil
  self
end

#unwrapObject



37
38
39
# File 'lib/prelude/monad.rb', line 37

def unwrap
  M_STATE[object_id]
end

#wrapObject



32
33
34
35
# File 'lib/prelude/monad.rb', line 32

def wrap
  M_STATE[object_id] = self
  self
end