Sha256: 652ff03d1630cb44f389cf4a3303ce5bfbb58900999b51ae62ed6aa9a7bf5e0a

Contents?: true

Size: 1.48 KB

Versions: 13

Compression:

Stored size: 1.48 KB

Contents

module Dry
  module Monads
    # Advanced tranformations.
    module Transformer
      # 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.
      #
      # @example
      #   List[Right(1), Left(1)].fmap2 { |x| x + 1 } # => List[Right(2), Left(1)]
      #   Right(None).fmap2 { |x| x + 1 } # => Right(None)
      #
      # @param args [Array<Object>] arguments will be passed to the block or the proc
      # @return [Object] some monadic value
      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

      # Lifts a block/proc over the 3-level nested structure.
      #
      # @example
      #   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)
      #
      # @param args [Array<Object>] arguments will be passed to the block or the proc
      # @return [Object] some monadic value
      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
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
dry-monads-1.3.4 lib/dry/monads/transformer.rb
dry-monads-1.3.3 lib/dry/monads/transformer.rb
dry-monads-1.3.2 lib/dry/monads/transformer.rb
dry-monads-1.3.1 lib/dry/monads/transformer.rb
dry-monads-1.3.0 lib/dry/monads/transformer.rb
dry-monads-1.2.0 lib/dry/monads/transformer.rb
dry-monads-1.1.0 lib/dry/monads/transformer.rb
dry-monads-1.0.1 lib/dry/monads/transformer.rb
dry-monads-1.0.0 lib/dry/monads/transformer.rb
dry-monads-1.0.0.rc1 lib/dry/monads/transformer.rb
dry-monads-1.0.0.beta3 lib/dry/monads/transformer.rb
dry-monads-1.0.0.beta2 lib/dry/monads/transformer.rb
dry-monads-1.0.0.beta1 lib/dry/monads/transformer.rb