lib/prelude/monad.rb in prelude-0.0.2 vs lib/prelude/monad.rb in prelude-0.0.3

- old
+ new

@@ -21,31 +21,108 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #++ module Prelude - # $Id: monad.rb 7 2006-09-06 17:03:26Z prelude $ + # $Id: monad.rb 13 2006-09-11 05:19:16Z prelude $ # - # The Monad is an Array only in an implementation sence of the word - class Monad < Array + module Monad + State = {} - def wrap(v) - [v] + def wrap + State[object_id] = self + self end + def unwrap + State[object_id] + end + def empty - [] + State[object_id] = nil + self end - def join + def Monad.join(arr) r = [] - each {|a| r.push *a} + arr.each {|a| r.push *a} r end - def bind(&block) - map(&block).join - 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]) + self + +# when f.kind_of?(Proc) : +# State[object_id] = f.call(State[object_id]) +# self + else + f + end # case + end # bind + + alias << bind + end # Monad + +# class Maybe < Monad + +# # maybe -- :: b -> (a -> b) -> Maybe a -> b +# def maybe +# warn "Method 'maybe' is not implemented yet." if $VERBOSE +# return [] +# end + +# # isJust -- :: Maybe a -> Bool +# def isJust +# warn "Method 'isJust' is not implemented yet." if $VERBOSE +# return [] +# end + +# # isNothing -- :: Maybe a -> Bool +# def isNothing +# warn "Method 'isNothing' is not implemented yet." if $VERBOSE +# return [] +# end + +# # fromJust -- :: Maybe a -> a +# def fromJust +# warn "Method 'fromJust' is not implemented yet." if $VERBOSE +# return [] +# end + +# # fromMaybe -- :: a -> Maybe a -> a +# def fromMaybe +# warn "Method 'fromMaybe' is not implemented yet." if $VERBOSE +# return [] +# end + +# # listToMaybe -- :: [a] -> Maybe a +# def listToMaybe +# warn "Method 'listToMaybe' is not implemented yet." if $VERBOSE +# return [] +# end + +# # maybeToList -- :: Maybe a -> [a] +# def maybeToList +# warn "Method 'maybeToList' is not implemented yet." if $VERBOSE +# return [] +# end + +# # catMaybes -- :: [Maybe a] -> [a] +# def catMaybes +# warn "Method 'catMaybes' is not implemented yet." if $VERBOSE +# return [] +# end + +# # mapMaybe -- :: (a -> Maybe b) -> [a] -> [b] +# def mapMaybe +# warn "Method 'mapMaybe' is not implemented yet." if $VERBOSE +# return [] +# end + +# end # Maybe end # Prelude