Module: Deterministic::Monad
- Included in:
- Either
- Defined in:
- lib/deterministic/monad.rb
Defined Under Namespace
Classes: NotMonadError
Instance Method Summary collapse
-
#==(other) ⇒ Object
Two monads are equivalent if they are of the same type and when their values are equal.
-
#bind(proc = nil, &block) ⇒ Object
(also: #>>=)
- The monad: takes a function which returns a monad (of the same type), applies the function bind
-
M a -> (a -> Mb) -> M b the self.class, i.e.
- #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) -> M a -> M b.
-
#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
Two monads are equivalent if they are of the same type and when their values are equal
41 42 43 44 |
# File 'lib/deterministic/monad.rb', line 41 def ==(other) return false unless other.is_a? self.class @value == other.instance_variable_get(:@value) end |
#bind(proc = nil, &block) ⇒ Object Also known as: >>=
The monad: takes a function which returns a monad (of the same type), applies the function
- bind
-
M a -> (a -> Mb) -> M b
the self.class, i.e. the containing monad is passed as a second (optional) arg to the function
27 28 29 30 31 |
# File 'lib/deterministic/monad.rb', line 27 def bind(proc=nil, &block) (proc || block).call(value, self.class).tap do |result| raise NotMonadError, "Expected #{result.inspect} to be an Either" unless result.is_a? self.class end 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) -> M a -> M b
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
47 48 49 50 |
# File 'lib/deterministic/monad.rb', line 47 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
36 37 38 |
# File 'lib/deterministic/monad.rb', line 36 def value @value end |