Skip to content

v1.2.0

Compare
Choose a tag to compare
@flash-gordon flash-gordon released this 15 Jan 11:03
v1.2.0
a7af485

v1.2.0 2019-01-12

BREAKING CHANGES

  • Support for Ruby 2.2 was dropped. Ruby 2.2 reached its EOL on March 31, 2018.

Added

  • Most of the constructors now have call alias so you can compose them with Procs nicely if you've switched to Ruby 2.6 (flash-gordon)

    pipe = -> x { x.upcase } >> Success
    pipe.('foo') # => Success('FOO')
  • List#collect gathers Some values from the list (flash-gordon)

    include Dry::Monads::List::Mixin
    include Dry::Monads::Maybe::Mixin
    # ...
    List[10, 5, 0].collect do |divisor|
      if divisor.zero?
        None()
      else
        Some(n / divisor)
      end
    end
    # => List[4, 2]

    Without block:

    List[Some(5), None(), Some(3)].collect.map { |x| x * 2 }
    # => [10, 6]
  • Right-biased monads got #flatten and #and (falsh-gordon)

    #flatten removes one level of monadic structure, it's useful when you're dealing with things like Maybe of Maybe of something:

    include Dry::Monads::Maybe::Mixin
    
    Some(Some(1)).flatten # => Some(1)
    Some(None()).flatten # => None
    None().flatten # => None

    In contrast to Array#flatten, dry-monads' version removes only 1 level of nesting, that is always acts as Array#flatten(1):

    Some(Some(Some(1))).flatten # => Some(Some(1))

    #and is handy for combining two monadic values and working with them at once:

    include Dry::Monads::Maybe::Mixin
    
    # using block
    Some(5).and(Some(3)) { |x, y| x + y } # => Some(8)
    # without block
    Some(5).and(Some(3)) # => Some([5, 3])
    # other cases
    Some(5).and(None()) # => None()
    None().and(Some(5)) # => None()
  • Concise imports with Dry::Monads.[]. You're no longer required to require all desired monads and include them one-by-one, the [] method handles it for you (flash-gordon)

    require 'dry/monads'
    
    class CreateUser
      include Dry::Monads[:result, :do]
    
      def initialize(repo, send_email)
        @repo = repo
        @send_email = send_email
      end
    
      def call(name)
        if @repo.user_exist?(name)
          Failure(:user_exists)
        else
          user = yield @repo.add_user(name)
          yield @send_email.(user)
          Success(user)
        end
      end
    end
  • Task.failed is a counterpart of Task.pure, accepts an exception and returns a failed task immediately (flash-gordon)

Compare v1.1.0...v1.2.0