Class: Tweed::Monad
Overview
Abstract superclass to represent monads.
To create a monad, subclass and define the monad’s Kleisli triple using define_monad.
Direct Known Subclasses
Tweed::Monads::Identity, Tweed::Monads::List, Tweed::Monads::Maybe
Class Method Summary collapse
-
.[](*args) ⇒ Monad
Syntactic sugar for Monad.return(…).
-
.return(*args) ⇒ Monad
abstract
Lift a value into this monad.
Instance Method Summary collapse
-
#bind(&block) ⇒ Object
abstract
Pull an underlying value out of this monad.
-
#lift_m2(other) {|x, y| ... } ⇒ Object
Adaptation of ‘liftM2` from Haskell.
Class Method Details
.[](*args) ⇒ Monad
Syntactic sugar for Monad.return(…).
60 61 62 |
# File 'lib/tweed/monad.rb', line 60 def self.[](*args) self.return(*args) end |
Instance Method Details
#bind(&block) ⇒ Object
This method is abstract.
Subclass and override using define_monad.
Pull an underlying value out of this monad.
54 55 56 |
# File 'lib/tweed/monad.rb', line 54 def bind(&block) abstract end |
#lift_m2(other) {|x, y| ... } ⇒ Object
Adaptation of ‘liftM2` from Haskell. ‘Lifts’ a binary function into the current monad.
76 77 78 79 80 81 82 |
# File 'lib/tweed/monad.rb', line 76 def lift_m2(other) self.bind do |x| other.bind do |y| self.class[yield(x, y)] end end end |