#-- # This file is part of the Prelude library that provides tools to # enable Haskell style functional programming in Ruby. # # http://prelude.rubyforge.org # # Copyright (C) 2006 APP Design, Inc. # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # 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 $ # module Monad State = {} def wrap State[object_id] = self self end def unwrap State[object_id] end def empty 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]) 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