Module: Deterministic::Monad
- Included in:
- Either
- Defined in:
- lib/deterministic/monad.rb
Defined Under Namespace
Classes: NotMonadError
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#bind(proc = nil, &block) ⇒ Object
- The monad: takes a function which returns a monad, applies bind
-
Ma -> (a -> Mb) -> Mb.
- #initialize(value) ⇒ Object
- #join(value) ⇒ Object
-
#map(proc = nil, &block) ⇒ Object
- The functor: takes a function (a -> b) and applies it to the inner value of the monad (Ma), boxes it back to the same monad (Mb) fmap
-
(a -> b) -> Ma -> Mb.
-
#to_s ⇒ Object
Return the string representation of the Monad.
-
#value ⇒ Object
- Get the underlying value, return in Haskell return
-
m a -> a.
Instance Method Details
#==(other) ⇒ Object
38 39 40 41 |
# File 'lib/deterministic/monad.rb', line 38 def ==(other) return false unless other.is_a? self.class @value == other.instance_variable_get(:@value) end |
#bind(proc = nil, &block) ⇒ Object
The monad: takes a function which returns a monad, applies
- bind
-
Ma -> (a -> Mb) -> Mb
26 27 28 29 30 |
# File 'lib/deterministic/monad.rb', line 26 def bind(proc=nil, &block) result = (proc || block).call(value) raise NotMonadError unless result.is_a? Monad self.class.new(result) end |
#initialize(value) ⇒ Object
5 6 7 |
# File 'lib/deterministic/monad.rb', line 5 def initialize(value) @value = join(value) end |
#join(value) ⇒ Object
11 12 13 14 |
# File 'lib/deterministic/monad.rb', line 11 def join(value) if value.is_a? self.class then value.value else value end end |
#map(proc = nil, &block) ⇒ Object
The functor: takes a function (a -> b) and applies it to the inner value of the monad (Ma), boxes it back to the same monad (Mb)
- fmap
-
(a -> b) -> Ma -> Mb
19 20 21 22 |
# File 'lib/deterministic/monad.rb', line 19 def map(proc=nil, &block) result = (proc || block).call(value) self.class.new(result) end |
#to_s ⇒ Object
Return the string representation of the Monad
44 45 46 47 |
# File 'lib/deterministic/monad.rb', line 44 def to_s pretty_class_name = self.class.name.split('::')[-1] "#{pretty_class_name}(#{self.value.inspect})" end |
#value ⇒ Object
Get the underlying value, return in Haskell
- return
-
m a -> a
34 35 36 |
# File 'lib/deterministic/monad.rb', line 34 def value @value end |