Module: Dry::Monads::Transformer

Included in:
List, Maybe, Result
Defined in:
lib/dry/monads/transformer.rb

Overview

Advanced tranformations.

Instance Method Summary collapse

Instance Method Details

#fmap2(*args) ⇒ Object

Lifts a block/proc over the 2-level nested structure. This is essentially fmap . fmap (. is the function composition operator from Haskell) or the functor instance for a two-level monadic structure like List Either.

Examples:

List[Right(1), Left(1)].fmap2 { |x| x + 1 } # => List[Right(2), Left(1)]
Right(None).fmap2 { |x| x + 1 } # => Right(None)

Parameters:

  • args (Array<Object>)

    arguments will be passed to the block or the proc

Returns:

  • (Object)

    some monadic value



16
17
18
19
20
21
22
23
# File 'lib/dry/monads/transformer.rb', line 16

def fmap2(*args)
  if block_given?
    fmap { |a| a.fmap { |b| yield(b, *args) } }
  else
    func, *rest = args
    fmap { |a| a.fmap { |b| func.(b, *rest) } }
  end
end

#fmap3(*args) ⇒ Object

Lifts a block/proc over the 3-level nested structure.

Examples:

List[Right(Some(1)), Left(Some(1))].fmap3 { |x| x + 1 } # => List[Right(Some(2)), Left(Some(1))]
Right(None).fmap3 { |x| x + 1 } # => Right(None)

Parameters:

  • args (Array<Object>)

    arguments will be passed to the block or the proc

Returns:

  • (Object)

    some monadic value



33
34
35
36
37
38
39
40
# File 'lib/dry/monads/transformer.rb', line 33

def fmap3(*args)
  if block_given?
    fmap { |a| a.fmap { |b| b.fmap { |c| yield(c, *args) } } }
  else
    func, *rest = args
    fmap { |a| a.fmap { |b| b.fmap { |c| func.(c, *rest) } } }
  end
end