Module: Monads::Monad
Class Method Summary collapse
Instance Method Summary collapse
-
#fmap(&block) ⇒ Object
- fmap
-
(a -> b) -> M a -> M b.
- #initialize(value) ⇒ Object
-
#join ⇒ Object
- join
-
M (M a) -> M a.
- #method_missing(method, *args, &block) ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
26 27 28 29 30 |
# File 'lib/ruby-monads/monad.rb', line 26 def method_missing(method, *args, &block) fmap do |value| value.public_send(method, *args, &block) end end |
Class Method Details
.included(base) ⇒ Object
3 4 5 6 7 |
# File 'lib/ruby-monads/monad.rb', line 3 def self.included(base) base.class_eval do private_class_method :new end end |
Instance Method Details
#fmap(&block) ⇒ Object
- fmap
-
(a -> b) -> M a -> M b
14 15 16 17 18 |
# File 'lib/ruby-monads/monad.rb', line 14 def fmap(&block) bind do |value| self.class.unit(block.call(value)) end end |
#initialize(value) ⇒ Object
9 10 11 |
# File 'lib/ruby-monads/monad.rb', line 9 def initialize(value) @value = value end |
#join ⇒ Object
- join
-
M (M a) -> M a
21 22 23 24 |
# File 'lib/ruby-monads/monad.rb', line 21 def join value = @value.is_a?(monad_type) ? @value.unwrap(nil) : @value monad_type.unit(value) end |