v1.3.0
v1.3.0 2019-08-03
BREAKING CHANGES
- Support for Ruby 2.3 was dropped.
Added
-
Result#either
(waiting-for-dev)Success(1).either(-> x { x + 1 }, -> x { x + 2 }) # => 2 Failure(1).either(-> x { x + 1 }, -> x { x + 2 }) # => 3
-
Maybe#to_result
(SpyMachine + flash-gordon)Some(3).to_result(:no_value) # => Success(3) None().to_result { :no_value } # => Failure(:no_value) None().to_result # => Failure()
-
Do notation can be used with
extend
. This simplifies usage in class methods and in other "complicated" cases (gogiel + flash-gordon)class CreateUser extend Dry::Monads::Do::Mixin extend Dry::Monads[:result] def self.run(params) self.call do values = bind Validator.validate(params) user = bind UserRepository.create(values) Success(user) end end end
Or you can bind values directly:
ma = Dry::Monads.Success(1) mb = Dry::Monads.Success(2) Dry::Monads::Do.() do a = Dry::Monads::Do.bind(ma) b = Dry::Monads::Do.bind(mb) Dry::Monads.Success(a + b) end
-
{Some,Success,Failure}#[]
shortcuts for building arrays wrapped within monadic value (flash-gordon)Success[1, 2] # => Success([1, 2])
-
List.unfold
yields a block returningMaybe<Any>
. If the block returnsSome(a)
a
is appended to the output list. ReturningNone
halts the unfloding (flash-gordon)List.unfold(0) do |x| if x > 5 None() else Some[x + 1, 2**x] end end # => List[1, 2, 3, 4, 5]
-
Experimental support for pattern matching! 🎉 (flash-gordon)
case value in Failure(_) then :failure in Success(10) then :ten in Success(100..500 => code) then code in Success() then :empty in Success(:code, x) then x in Success[:status, x] then x in Success({ status: x }) then x in Success({ code: 200..300 => x }) then x end
Read more about pattern matching in Ruby:
- https://medium.com/@baweaver/ruby-2-7-pattern-matching-destructuring-on-point-90f56aaf7b4e
- https://bugs.ruby-lang.org/issues/14912
Keep in mind this feature is experimental and can be changed by 2.7 release. But it rocks already!