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

Class Method Details

.included(m) ⇒ Object



18
19
20
21
22
23
24
25
# 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
  m.singleton_class.send(:alias_method, :call, :new)
end

Instance Method Details

#===(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:

  • (Boolean)


132
133
134
# File 'lib/dry/monads/right_biased.rb', line 132

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.

Examples:

include Dry::Monads::Result::Mixin

Success(3).and(Success(5)) # => Success([3, 5])
Success(3).and(Failure(:not_a_number)) # => Failure(:not_a_number)
Failure(:not_a_number).and(Success(5)) # => Failure(:not_a_number)
Success(3).and(Success(5)) { |a, b| a + b } # => Success(8)

Parameters:

Returns:



177
178
179
180
181
182
183
184
185
186
187
# File 'lib/dry/monads/right_biased.rb', line 177

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.

Examples:

happy path

create_user = Dry::Monads::Success(CreateUser.new)
name = Success("John")
create_user.apply(name) # equivalent to CreateUser.new.call("John")

unhappy path

name = Failure(:name_missing)
create_user.apply(name) # => Failure(:name_missing)

Returns:



122
123
124
125
126
127
128
# File 'lib/dry/monads/right_biased.rb', line 122

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.

Examples:

Dry::Monads.Right(4).bind(&:succ) # => 5

Parameters:

  • args (Array<Object>)

    arguments that will be passed to a block if one was given, otherwise the first value assumed to be a Proc (callable) object and the rest of args will be passed to this object along with the internal value

Returns:

  • (Object)

    result of calling proc or block on the internal value



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dry/monads/right_biased.rb', line 48

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

#deconstructObject

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

Examples:

case Success(x)
in Success(Integer) then ...
in Success(2..100) then ...
in Success(2..200 => code) then ...
end


198
199
200
201
202
203
204
205
206
# File 'lib/dry/monads/right_biased.rb', line 198

def deconstruct
  if Unit.equal?(@value)
    []
  elsif @value.is_a?(::Array)
    @value
  else
    [@value]
  end
end

#discardRightBiased::Right

Maps the value to Dry::Monads::Unit, useful when you don't care about the actual value.

Examples:

Dry::Monads::Success(:success).discard
# => Success(Unit)

Returns:



144
145
146
# File 'lib/dry/monads/right_biased.rb', line 144

def discard
  fmap { Unit }
end

#flattenRightBiased::Right, RightBiased::Left

Removes one level of monad structure by joining two values.

Examples:

include Dry::Monads::Result::Mixin
Success(Success(5)).flatten # => Success(5)
Success(Failure(:not_a_number)).flatten # => Failure(:not_a_number)
Failure(:not_a_number).flatten # => Failure(:not_a_number)

Returns:



157
158
159
# File 'lib/dry/monads/right_biased.rb', line 157

def flatten
  bind(&:itself)
end

#fmapRightBiased::Right

Abstract method for lifting a block over the monad type. Must be implemented for a right-biased monad.

Returns:

Raises:

  • (NotImplementedError)


82
83
84
# File 'lib/dry/monads/right_biased.rb', line 82

def fmap(*)
  raise NotImplementedError
end

#orRightBiased::Right

Ignores arguments and returns self. It exists to keep the interface identical to that of Left.

Returns:



90
91
92
# File 'lib/dry/monads/right_biased.rb', line 90

def or(*)
  self
end

#or_fmapRightBiased::Right

A lifted version of #or. For Dry::Monads::RightBiased::Right acts in the same way as #or, that is returns itselt.

Returns:



98
99
100
# File 'lib/dry/monads/right_biased.rb', line 98

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.

Examples:

Dry::Monads.Right(4).tee { Right('ok') } # => Right(4)
Dry::Monads.Right(4).tee { Left('fail') } # => Left('fail')

Parameters:

  • args (Array<Object>)

    arguments will be transparently passed through to #bind

Returns:



74
75
76
# File 'lib/dry/monads/right_biased.rb', line 74

def tee(*args, &block)
  bind(*args, &block).bind { self }
end

#value!Object

Unwraps the underlying value

Returns:

  • (Object)


30
31
32
# File 'lib/dry/monads/right_biased.rb', line 30

def value!
  @value
end

#value_or(_val = nil) ⇒ Object

Returns value. It exists to keep the interface identical to that of RightBiased::Left

Returns:

  • (Object)


105
106
107
# File 'lib/dry/monads/right_biased.rb', line 105

def value_or(_val = nil)
  @value
end