lib/prelude/monad.rb in prelude-0.0.3 vs lib/prelude/monad.rb in prelude-0.0.5
- old
+ new
@@ -21,50 +21,46 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#++
module Prelude
- # $Id: monad.rb 13 2006-09-11 05:19:16Z prelude $
+ # $Id: monad.rb 34 2007-10-23 21:38:09Z prelude $
#
+ # Defines monadic behavior for anything willing to include it.
module Monad
- State = {}
+ M_STATE = {}
def wrap
- State[object_id] = self
+ M_STATE[object_id] = self
self
end
def unwrap
- State[object_id]
+ M_STATE[object_id]
end
def empty
- State[object_id] = nil
+ M_STATE[object_id] = nil
self
end
def Monad.join(arr)
r = []
arr.each {|a| r.push *a}
r
end
def bind(f=nil)
- case
- when f.kind_of?(Symbol) || f.kind_of?(Method) || f.kind_of?(Proc) :
- State[object_id] = f.to_proc.call(State[object_id])
+ 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
-
-# when f.kind_of?(Proc) :
-# State[object_id] = f.call(State[object_id])
-# self
-
else
f
- end # case
+ end # if
end # bind
- alias << bind
+ alias >> bind
end # Monad
# class Maybe < Monad