Module: Dry::Monads::RightBiased::Right
- Includes:
- Core::Constants
- Included in:
- Maybe::Some, Dry::Monads::Result::Success, Try::Value
- Defined in:
- lib/dry/monads/right_biased.rb
Overview
Right part
Class Method Summary collapse
Instance Method Summary collapse
- #===(other) ⇒ Boolean
-
#apply(val = Undefined) ⇒ RightBiased::Left, RightBiased::Right
Applies the stored value to the given argument if the argument has type of Right, otherwise returns the argument.
-
#bind(*args, **kwargs) ⇒ Object
Calls the passed in Proc object with value stored in self and returns the result.
-
#discard ⇒ RightBiased::Right
Maps the value to Dry::Monads::Unit, useful when you don't care about the actual value.
-
#fmap ⇒ RightBiased::Right
Abstract method for lifting a block over the monad type.
-
#or ⇒ RightBiased::Right
Ignores arguments and returns self.
-
#or_fmap ⇒ RightBiased::Right
A lifted version of
#or
. -
#tee(*args, &block) ⇒ RightBiased::Right
Does the same thing as #bind except it returns the original monad when the result is a Right.
-
#value! ⇒ Object
Unwraps the underlying value.
-
#value_or(_val = nil) ⇒ Object
Returns value.
Class Method Details
.included(m) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/dry/monads/right_biased.rb', line 18 def self.included(m) super def m.to_proc @to_proc ||= method(:new).to_proc end end |
Instance Method Details
#===(other) ⇒ Boolean
131 132 133 |
# File 'lib/dry/monads/right_biased.rb', line 131 def ===(other) self.class == other.class && value! === other.value! end |
#apply(val = Undefined) ⇒ RightBiased::Left, RightBiased::Right
Applies the stored value to the given argument if the argument has type of Right, otherwise returns the argument.
121 122 123 124 125 126 127 |
# File 'lib/dry/monads/right_biased.rb', line 121 def apply(val = Undefined) unless @value.respond_to?(:call) raise TypeError, "Cannot apply #{ val.inspect } to #{ @value.inspect }" end Undefined.default(val) { yield }.fmap { |unwrapped| curry.(unwrapped) } end |
#bind(*args, **kwargs) ⇒ Object
Calls the passed in Proc object with value stored in self and returns the result.
If proc is nil, it expects a block to be given and will yield to it.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/dry/monads/right_biased.rb', line 47 def bind(*args, **kwargs) if args.empty? && !kwargs.empty? vargs, vkwargs = destructure(@value) kw = [kwargs.merge(vkwargs)] else vargs = [@value] kw = kwargs.empty? ? EMPTY_ARRAY : [kwargs] end if block_given? yield(*vargs, *args, *kw) else obj, *rest = args obj.(*vargs, *rest, *kw) end end |
#discard ⇒ RightBiased::Right
Maps the value to Dry::Monads::Unit, useful when you don't care about the actual value.
143 144 145 |
# File 'lib/dry/monads/right_biased.rb', line 143 def discard fmap { Unit } end |
#fmap ⇒ RightBiased::Right
Abstract method for lifting a block over the monad type. Must be implemented for a right-biased monad.
81 82 83 |
# File 'lib/dry/monads/right_biased.rb', line 81 def fmap(*) raise NotImplementedError end |
#or ⇒ RightBiased::Right
Ignores arguments and returns self. It exists to keep the interface identical to that of Left.
89 90 91 |
# File 'lib/dry/monads/right_biased.rb', line 89 def or(*) self end |
#or_fmap ⇒ RightBiased::Right
A lifted version of #or
. For Dry::Monads::RightBiased::Right acts in the same way as #or
,
that is returns itselt.
97 98 99 |
# File 'lib/dry/monads/right_biased.rb', line 97 def or_fmap(*) self end |
#tee(*args, &block) ⇒ RightBiased::Right
Does the same thing as #bind except it returns the original monad when the result is a Right.
73 74 75 |
# File 'lib/dry/monads/right_biased.rb', line 73 def tee(*args, &block) bind(*args, &block).bind { self } end |
#value! ⇒ Object
Unwraps the underlying value
29 30 31 |
# File 'lib/dry/monads/right_biased.rb', line 29 def value! @value end |
#value_or(_val = nil) ⇒ Object
Returns value. It exists to keep the interface identical to that of RightBiased::Left
104 105 106 |
# File 'lib/dry/monads/right_biased.rb', line 104 def value_or(_val = nil) @value end |