Skip to content

Instantly share code, notes, and snippets.

@michaelherold
Created November 7, 2018 17:42
Show Gist options
  • Save michaelherold/9c0ef2d6a569089ceee4e811f924e709 to your computer and use it in GitHub Desktop.
Save michaelherold/9c0ef2d6a569089ceee4e811f924e709 to your computer and use it in GitHub Desktop.
It really seems like it should be easier to reduce a list of Results
require 'dry/monads/all'
include Dry::Monads::Result::Mixin
success = Success(:finished)
def merge(result, failures)
result
.or { |failure| Failure(failure + failures) }
.bind { Failure(failures) }
end
result =
[success, success, Failure(2), success, Failure(4), Failure(5)]
.reduce(success) do |acc, el|
el
.or { |*failures| merge(acc, failures) }
.bind { acc }
end
puts result #=> Failure([2, 4, 5])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment