Module: FunctionalLightService::Monad
- Included in:
- EnumBuilder::DataType::AnyEnum
- Defined in:
- lib/functional-light-service/functional/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 :: (a -> Mb) -> M a -> M b the self.class, i.e.
-
#fmap(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.
-
#initialize(init) ⇒ Object
Basicly the
purefunction. -
#inspect ⇒ Object
Return the string representation of the Monad.
-
#join(other) ⇒ Object
If the passed value is monad already, get the value to avoid nesting M[M[A]] is equivalent to M.
- #to_s ⇒ Object
-
#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
58 59 60 61 62 |
# File 'lib/functional-light-service/functional/monad.rb', line 58 def ==(other) return false unless other.is_a? self.class @value == other.monad_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
(a -> Mb) -> M a -> M b
the self.class, i.e. the containing monad is passed as a second (optional) arg to the function
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/functional-light-service/functional/monad.rb', line 33 def bind(proc = nil, &block) (proc || block).call(value).tap do |result| # rubocop:disable Style/CaseEquality parent = self.class.superclass === Object ? self.class : self.class.superclass # rubocop:enable Style/CaseEquality unless result.is_a? parent raise NotMonadError, "Expected #{result.inspect} to be an #{parent}" end end end |
#fmap(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
25 26 27 28 |
# File 'lib/functional-light-service/functional/monad.rb', line 25 def fmap(proc = nil, &block) result = (proc || block).call(value) self.class.new(result) end |
#initialize(init) ⇒ Object
Basicly the pure function
8 9 10 |
# File 'lib/functional-light-service/functional/monad.rb', line 8 def initialize(init) @value = join(init) end |
#inspect ⇒ Object
Return the string representation of the Monad
72 73 74 75 |
# File 'lib/functional-light-service/functional/monad.rb', line 72 def inspect pretty_class_name = self.class.name.split('::')[-1] "#{pretty_class_name}(#{value.inspect})" end |
#join(other) ⇒ Object
If the passed value is monad already, get the value to avoid nesting M[M[A]] is equivalent to M
14 15 16 17 18 19 20 |
# File 'lib/functional-light-service/functional/monad.rb', line 14 def join(other) if other.is_a? self.class other.value else other end end |
#to_s ⇒ Object
53 54 55 |
# File 'lib/functional-light-service/functional/monad.rb', line 53 def to_s value.to_s end |
#value ⇒ Object
Get the underlying value, return in Haskell
- return
M a -> a
49 50 51 |
# File 'lib/functional-light-service/functional/monad.rb', line 49 def value @value end |