lib/prelude/monad.rb in prelude-0.0.1 vs lib/prelude/monad.rb in prelude-0.0.2
- old
+ new
@@ -4,36 +4,48 @@
#
# http://prelude.rubyforge.org
#
# Copyright (C) 2006 APP Design, Inc.
#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
+# 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 program is distributed in the hope that it will be useful,
+# 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 General Public License for more details.
+# 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 General Public License along
-# with this program; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# 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 2 2006-08-25 00:11:17Z prelude $
+ # $Id: monad.rb 7 2006-09-06 17:03:26Z prelude $
#
- # Monad implementation is inspired by the following sources:
- #
- # * article "Late to the Party" posted at http://cwilliams.textdriven.com/pages/monads, I could not detect
- # the author's name
- # * writings by MenTaLguY, see http://moonbase.rydia.net/mental/writings/programming/monads-in-ruby/00introduction.html
- #
- class Monad
+ # The Monad is an Array only in an implementation sence of the word
+ class Monad < Array
+
+ def wrap(v)
+ [v]
+ end
+
+ def empty
+ []
+ end
+
+ def join
+ r = []
+ each {|a| r.push *a}
+ r
+ end
+
def bind(&block)
- collect(&block).flatten
+ map(&block).join
end
+
end # Monad
+
end # Prelude