Module: Dry::Monads

Extended by:
[ Lazy[ Lazy::Mixin[ Lazy::Mixin::Constructors, Maybe::Mixin::Constructors, Result::Mixin::Constructors, Task::Mixin::Constructors, Try::Mixin::Constructors, Validated::Mixin::Constructors, ].freeze, Lazy::Mixin::Constructors, Maybe::Mixin::Constructors, Result::Mixin::Constructors, Task::Mixin::Constructors, Try::Mixin::Constructors, Validated::Mixin::Constructors
Defined in:
lib/dry/monads.rb,
lib/dry/monads/do.rb,
lib/dry/monads/all.rb,
lib/dry/monads/try.rb,
lib/dry/monads/lazy.rb,
lib/dry/monads/list.rb,
lib/dry/monads/task.rb,
lib/dry/monads/unit.rb,
lib/dry/monads/curry.rb,
lib/dry/monads/maybe.rb,
lib/dry/monads/do/all.rb,
lib/dry/monads/either.rb,
lib/dry/monads/errors.rb,
lib/dry/monads/result.rb,
lib/dry/monads/version.rb,
lib/dry/monads/traverse.rb,
lib/dry/monads/undefined.rb,
lib/dry/monads/validated.rb,
lib/dry/monads/transformer.rb,
lib/dry/monads/result/fixed.rb,
lib/dry/monads/right_biased.rb,
lib/json/add/dry/monads/maybe.rb,
lib/dry/monads/conversion_stubs.rb

Overview

Common, idiomatic monads for Ruby

Defined Under Namespace

Modules: ConversionStubs, Curry, Do, RightBiased, Transformer Classes: InvalidFailureTypeError, Lazy, List, Maybe, Result, Task, Try, UnwrapError, Validated

Constant Summary collapse

CONSTRUCTORS =

List of monad constructors

[
  Lazy::Mixin::Constructors,
  Maybe::Mixin::Constructors,
  Result::Mixin::Constructors,
  Task::Mixin::Constructors,
  Try::Mixin::Constructors,
  Validated::Mixin::Constructors,
].freeze
Unit =

Unit is a special object you can use whenever your computations don't return any payload. Previously, if your function ran a side-effect and returned no meaningful value, you had to return things like Success(nil), Success([]), Success({}), Maybe(""), Success(true) and so forth.

You should use Unit if you wish to return an empty monad.

Examples:

with Result

Success(Unit)
Failure(Unit)

with Maybe

Maybe(Unit)
=> Some(Unit)
Object.new.tap do |unit|
  def unit.to_s
    'Unit'
  end

  def unit.inspect
    'Unit'
  end
end
Some =

See Also:

Maybe::Some
None =

See Also:

Maybe::None
Either =
Result
Success =

See Also:

Result::Success
Failure =

See Also:

Result::Failure
VERSION =
'1.1.0'.freeze
Traverse =

List of default traverse functions for types. It is implicitly used by List#traverse for making common cases easier to handle.

{
  Validated => -> el { el.alt_map(to_list) }
}
Undefined =
Dry::Core::Constants::Undefined
Valid =
Validated::Valid
Invalid =

See Also:

Validated::Invalid

Constants included from Result::Mixin::Constructors

Result::Mixin::Constructors::Left, Result::Mixin::Constructors::Right

Class Method Summary collapse

Methods included from Validated::Mixin::Constructors

Invalid, Valid

Methods included from Result::Mixin::Constructors

Failure, Success

Methods included from Maybe::Mixin::Constructors

Maybe, None, Some

Methods included from Task::Mixin::Constructors

Task

Methods included from Lazy::Mixin::Constructors

Lazy

Methods included from Try::Mixin::Constructors

Try

Class Method Details

.included(base) ⇒ Object


6
7
8
9
10
11
12
# File 'lib/dry/monads.rb', line 6

def self.included(base)
  if const_defined?(:CONSTRUCTORS)
    base.include(*CONSTRUCTORS)
  else
    raise "Load all monads first with require 'dry/monads/all'"
  end
end

.Result(error, **options) ⇒ Module

Creates a module that has two methods: Success and Failure. Success is identical to Dry::Monads::Result::Mixin::Constructors#Success and Failure rejects values that don't conform the value of the error parameter. This is essentially a Result type with the Failure part fixed.

Examples:

using dry-types

module Types
  include Dry::Types.module
end

class Operation
  # :user_not_found and :account_not_found are the only
  # values allowed as failure results
  Error =
    Types.Value(:user_not_found) |
    Types.Value(:account_not_found)

  include Dry::Monads::Result(Error)

  def (id)
     = acount_repo.find(id)

     ? Success() : Failure(:account_not_found)
  end

  def find_user(id)
    # ...
  end
end

316
317
318
# File 'lib/dry/monads/result.rb', line 316

def self.Result(error, **options)
  Result::Fixed[error, **options]
end