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, &block) ⇒ 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(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 ⇒ 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.
-
#|(_alt) ⇒ RightBiased::Right
Ignores arguments and returns self.
Class Method Details
.included(m) ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/dry/monads/right_biased.rb', line 12 def self.included(m) super def m.to_proc @to_proc ||= method(:new).to_proc end m.singleton_class.alias_method(:call, :new) end |
Instance Method Details
#===(other) ⇒ Boolean
122 123 124 |
# File 'lib/dry/monads/right_biased.rb', line 122 def ===(other) other.instance_of?(self.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.
163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/dry/monads/right_biased.rb', line 163 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, &block) ⇒ RightBiased::Left, RightBiased::Right
Applies the stored value to the given argument if the argument has type of Right, otherwise returns the argument.
112 113 114 115 116 117 118 |
# File 'lib/dry/monads/right_biased.rb', line 112 def apply(val = Undefined, &block) unless @value.respond_to?(:call) raise TypeError, "Cannot apply #{val.inspect} to #{@value.inspect}" end Undefined.default(val, &block).fmap { curry.(_1) } 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.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/dry/monads/right_biased.rb', line 40 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
185 186 187 188 189 190 191 192 193 |
# File 'lib/dry/monads/right_biased.rb', line 185 def deconstruct if Unit.equal?(@value) EMPTY_ARRAY elsif !@value.is_a?(::Array) [@value] else @value end end |
#deconstruct_keys(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
206 207 208 209 210 211 212 |
# File 'lib/dry/monads/right_biased.rb', line 206 def deconstruct_keys(keys) if @value.respond_to?(:deconstruct_keys) @value.deconstruct_keys(keys) 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.
134 |
# File 'lib/dry/monads/right_biased.rb', line 134 def discard = fmap { Unit } |
#flatten ⇒ RightBiased::Right, RightBiased::Left
Removes one level of monad structure by joining two values.
145 |
# File 'lib/dry/monads/right_biased.rb', line 145 def flatten = bind(&:itself) |
#fmap ⇒ RightBiased::Right
Abstract method for lifting a block over the monad type. Must be implemented for a right-biased monad.
72 |
# File 'lib/dry/monads/right_biased.rb', line 72 def fmap(...) = bind(...) |
#or ⇒ RightBiased::Right
Ignores arguments and returns self. It exists to keep the interface identical to that of Left.
78 |
# File 'lib/dry/monads/right_biased.rb', line 78 def or(...) = self |
#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.
92 |
# File 'lib/dry/monads/right_biased.rb', line 92 def or_fmap(...) = self |
#tee ⇒ RightBiased::Right
Does the same thing as #bind except it returns the original monad when the result is a Right.
66 |
# File 'lib/dry/monads/right_biased.rb', line 66 def tee(...) = bind(...).bind { self } |
#value! ⇒ Object
Unwraps the underlying value
24 |
# File 'lib/dry/monads/right_biased.rb', line 24 def value! = @value |
#value_or(_val = nil) ⇒ Object
Returns value. It exists to keep the interface identical to that of RightBiased::Left
97 |
# File 'lib/dry/monads/right_biased.rb', line 97 def value_or(_val = nil, &) = @value |
#|(_alt) ⇒ RightBiased::Right
Ignores arguments and returns self. It exists to keep the interface identical to that of Left.
86 |
# File 'lib/dry/monads/right_biased.rb', line 86 def |(_alt) = self |