Module: Dry::Monads::RightBiased::Right
- 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
-
#and(mb) ⇒ RightBiased::Left, RightBiased::Right
Combines the wrapped value with another monadic value.
-
#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.
-
#deconstruct ⇒ Object
private
Pattern matching.
-
#deconstruct_keys(_) ⇒ Object
private
Pattern matching hash values.
-
#discard ⇒ RightBiased::Right
Maps the value to Dry::Monads::Unit, useful when you don't care about the actual value.
-
#flatten ⇒ RightBiased::Right, RightBiased::Left
Removes one level of monad structure by joining two values.
-
#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
15 16 17 18 19 20 21 22 |
# File 'lib/dry/monads/right_biased.rb', line 15 def self.included(m) super def m.to_proc @to_proc ||= method(:new).to_proc end m.singleton_class.send(:alias_method, :call, :new) end |
Instance Method Details
#===(other) ⇒ Boolean
129 130 131 |
# File 'lib/dry/monads/right_biased.rb', line 129 def ===(other) self.class == other.class && value! === other.value! end |
#and(mb) ⇒ RightBiased::Left, RightBiased::Right
Combines the wrapped value with another monadic value. If both values are right-sided, yields a block and passes a tuple of values there. If no block given, returns a tuple of values wrapped with a monadic structure.
174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/dry/monads/right_biased.rb', line 174 def and(mb) bind do |a| mb.fmap do |b| if block_given? yield([a, b]) else [a, b] end end end 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.
119 120 121 122 123 124 125 |
# File 'lib/dry/monads/right_biased.rb', line 119 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.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/dry/monads/right_biased.rb', line 45 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 |
#deconstruct ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Pattern matching
196 197 198 199 200 201 202 203 204 |
# File 'lib/dry/monads/right_biased.rb', line 196 def deconstruct if Unit.equal?(@value) [] elsif @value.is_a?(::Array) @value else [@value] end end |
#deconstruct_keys(_) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Pattern matching hash values
217 218 219 220 221 222 223 |
# File 'lib/dry/monads/right_biased.rb', line 217 def deconstruct_keys(_) if @value.is_a?(::Hash) @value else EMPTY_HASH end end |
#discard ⇒ RightBiased::Right
Maps the value to Dry::Monads::Unit, useful when you don't care about the actual value.
141 142 143 |
# File 'lib/dry/monads/right_biased.rb', line 141 def discard fmap { Unit } end |
#flatten ⇒ RightBiased::Right, RightBiased::Left
Removes one level of monad structure by joining two values.
154 155 156 |
# File 'lib/dry/monads/right_biased.rb', line 154 def flatten bind(&:itself) end |
#fmap ⇒ RightBiased::Right
Abstract method for lifting a block over the monad type. Must be implemented for a right-biased monad.
79 80 81 |
# File 'lib/dry/monads/right_biased.rb', line 79 def fmap(*) raise NotImplementedError end |
#or ⇒ RightBiased::Right
Ignores arguments and returns self. It exists to keep the interface identical to that of Left.
87 88 89 |
# File 'lib/dry/monads/right_biased.rb', line 87 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.
95 96 97 |
# File 'lib/dry/monads/right_biased.rb', line 95 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.
71 72 73 |
# File 'lib/dry/monads/right_biased.rb', line 71 def tee(*args, &block) bind(*args, &block).bind { self } end |
#value! ⇒ Object
Unwraps the underlying value
27 28 29 |
# File 'lib/dry/monads/right_biased.rb', line 27 def value! @value end |
#value_or(_val = nil) ⇒ Object
Returns value. It exists to keep the interface identical to that of RightBiased::Left
102 103 104 |
# File 'lib/dry/monads/right_biased.rb', line 102 def value_or(_val = nil) @value end |