#-- # 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 34 2007-10-23 21:38:09Z prelude $ # # Defines monadic behavior for anything willing to include it. module Monad M_STATE = {} def wrap M_STATE[object_id] = self self end def unwrap M_STATE[object_id] end def empty M_STATE[object_id] = nil self end def Monad.join(arr) r = [] arr.each {|a| r.push *a} r end 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 # 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