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 `pure` function.
-
#inspect ⇒ Object
Return the string representation of the Monad.
- #join(other) ⇒ Object
- #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
54 55 56 57 58 |
# File 'lib/functional-light-service/functional/monad.rb', line 54 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
-
(a -> Mb) -> M a -> M b
the self.class, i.e. the containing monad is passed as a second (optional) arg to the function
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/functional-light-service/functional/monad.rb', line 31 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
23 24 25 26 |
# File 'lib/functional-light-service/functional/monad.rb', line 23 def fmap(proc = nil, &block) result = (proc || block).call(value) self.class.new(result) end |
#initialize(init) ⇒ Object
Basicly the `pure` function
6 7 8 |
# File 'lib/functional-light-service/functional/monad.rb', line 6 def initialize(init) @value = join(init) end |
#inspect ⇒ Object
Return the string representation of the Monad
61 62 63 64 |
# File 'lib/functional-light-service/functional/monad.rb', line 61 def inspect pretty_class_name = self.class.name.split('::')[-1] "#{pretty_class_name}(#{value.inspect})" end |
#join(other) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/functional-light-service/functional/monad.rb', line 12 def join(other) if other.is_a? self.class other.value else other end end |
#to_s ⇒ Object
49 50 51 |
# File 'lib/functional-light-service/functional/monad.rb', line 49 def to_s value.to_s end |
#value ⇒ Object
Get the underlying value, return in Haskell
- return
-
M a -> a
45 46 47 |
# File 'lib/functional-light-service/functional/monad.rb', line 45 def value @value end |