Class: Dry::Monads::Maybe::Some

Inherits:
Dry::Monads::Maybe show all
Includes:
RightBiased::Right
Defined in:
lib/dry/monads/maybe.rb,
lib/dry/monads/result.rb

Overview

Represents a value that is present, i.e. not nil.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RightBiased::Right

#===, #and, #apply, #bind, #deconstruct, #deconstruct_keys, #discard, #flatten, included, #or, #or_fmap, #tee, #value!, #value_or, #|

Methods inherited from Dry::Monads::Maybe

#as_json, coerce, json_create, #monad, #none?, pure, #some?, #to_json, #to_maybe, #to_monad, to_proc

Methods included from Transformer

#fmap2, #fmap3

Constructor Details

#initialize(value = Undefined) ⇒ Some

Returns a new instance of Some.

Raises:

  • (ArgumentError)


103
104
105
106
107
108
109
# File 'lib/dry/monads/maybe.rb', line 103

def initialize(value = Undefined)
  raise ArgumentError, "nil cannot be some" if value.nil?

  super()

  @value = Undefined.default(value, Unit)
end

Class Method Details

.[](*value) ⇒ Object

Shortcut for Some()

@example
  include Dry::Monads[:maybe]

  def call
    Some[200, {}, ['ok']] # => Some([200, {}, ['ok']])
  end


99
100
101
# File 'lib/dry/monads/maybe.rb', line 99

def self.[](*value)
  new(value)
end

Instance Method Details

#filter(with = Undefined, &block) ⇒ Maybe::None, Maybe::Some

Accepts a block and runs it against the wrapped value. If the block returns a trurhy value the result is self, otherwise None. If no block is given, the value serves and its result.

Parameters:

  • with (#call) (defaults to: Undefined)

    positional block

  • block (Proc)

    block

Returns:



167
168
169
170
171
172
173
174
175
# File 'lib/dry/monads/maybe.rb', line 167

def filter(with = Undefined, &block)
  block = Undefined.default(with, block || IDENTITY)

  if block.(@value)
    self
  else
    Monads.None()
  end
end

#fmapMaybe::Some, Maybe::None

Does the same thing as #bind except it also wraps the value in an instance of Maybe::Some monad. This allows for easier chaining of calls.

Examples:

Dry::Monads.Some(4).fmap(&:succ).fmap(->(n) { n**2 }) # => Some(25)

Parameters:

  • args (Array<Object>)

    arguments will be transparently passed through to #bind

Returns:

  • (Maybe::Some, Maybe::None)

    Wrapped result, i.e. nil will be mapped to None, other values will be wrapped with Some



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/dry/monads/maybe.rb', line 121

def fmap(...)
  next_value = bind(...)

  if next_value.nil?
    if self.class.warn_on_implicit_nil_coercion
      Core::Deprecations.warn(
        "Block passed to Some#fmap returned `nil` and was chained to None. "\
        "This is literally an unlawful behavior and it will not be supported in "\
        "dry-monads 2. \nPlease, replace `.fmap` with `.maybe` in places where you "\
        "expect `nil` as block result.\n"\
        "You can opt out of these warnings with\n"\
        "Dry::Monads::Maybe.warn_on_implicit_nil_coercion false",
        uplevel: 0,
        tag: :"dry-monads"
      )
    end
    Monads.None()
  else
    Some.new(next_value)
  end
end

#maybeMaybe::Some, Maybe::None

Does the same thing as #bind except it also wraps the value in an instance of the Maybe monad. This allows for easier chaining of calls.

Examples:

Dry::Monads.Some(4).maybe(&:succ).maybe(->(n) { n**2 }) # => Some(25)
Dry::Monads.Some(4).maybe(&:succ).maybe(->(_) { nil }) # => None()

Parameters:

  • args (Array<Object>)

    arguments will be transparently passed through to #bind

Returns:

  • (Maybe::Some, Maybe::None)

    Wrapped result, i.e. nil will be mapped to None, other values will be wrapped with Some



154
155
156
# File 'lib/dry/monads/maybe.rb', line 154

def maybe(...)
  Maybe.coerce(bind(...))
end

#to_result(_fail = Unit) ⇒ Success<Any>

Converts to Sucess(value!)

Parameters:

  • fail (#call)

    Fallback value

  • block (Proc)

    Fallback block

Returns:



408
409
410
# File 'lib/dry/monads/result.rb', line 408

def to_result(_fail = Unit)
  Result::Success.new(@value)
end

#to_sString Also known as: inspect

Returns:

  • (String)


178
179
180
181
182
183
184
# File 'lib/dry/monads/maybe.rb', line 178

def to_s
  if Unit.equal?(@value)
    "Some()"
  else
    "Some(#{@value.inspect})"
  end
end