module FunctionalLightService::Monad
Public Class Methods
new(init)
click to toggle source
Basicly the `pure` function
# File lib/functional-light-service/functional/monad.rb, line 6 def initialize(init) @value = join(init) end
Public Instance Methods
==(other)
click to toggle source
Two monads are equivalent if they are of the same type and when their values are equal
# 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)
click to toggle source
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
# 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
Also aliased as: '>>='
fmap(proc = nil, &block)
click to toggle source
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
# 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
inspect()
click to toggle source
Return the string representation of the Monad
# 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)
click to toggle source
to_s()
click to toggle source
# File lib/functional-light-service/functional/monad.rb, line 49 def to_s value.to_s end
value()
click to toggle source
Get the underlying value, return in Haskell
- return
-
M a -> a
# File lib/functional-light-service/functional/monad.rb, line 45 def value @value end