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)


91
92
93
94
95
96
97
# File 'lib/dry/monads/maybe.rb', line 91

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


89
# File 'lib/dry/monads/maybe.rb', line 89

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

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:



153
154
155
156
157
158
159
160
161
# File 'lib/dry/monads/maybe.rb', line 153

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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/dry/monads/maybe.rb', line 109

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



142
# File 'lib/dry/monads/maybe.rb', line 142

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

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

Converts to Sucess(value!)

Parameters:

  • fail (#call)

    Fallback value

  • block (Proc)

    Fallback block

Returns:



374
# File 'lib/dry/monads/result.rb', line 374

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

#to_sString Also known as: inspect

Returns:

  • (String)


164
165
166
167
168
169
170
# File 'lib/dry/monads/maybe.rb', line 164

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