Rails 6 monads 模式匹配只获取 success/failure

Rails 6 monads pattern matching to fetch only success/failure

是否有任何内置或一些巧妙的方法可以从 monad 结果数组中仅获取成功或失败结果?

例如

state
=> [Failure("12345 Product code is not valid"), Success("eiowruqoiu Backbone Product created")]

哪里

state[0].class
=> Dry::Monads::Result::Failure
state[1].class
=> Dry::Monads::Result::Success
state.grep(Dry::Monads::Result::Failure)
state.grep(Dry::Monads::Result::Success)

state.select { |s| s.is_a?(Dry::Monads::Result::Failure) }
state.select { |s| s.is_a?(Dry::Monads::Result::Success) }

或使用 success? / failure? monads 方法

state.select(&:success?)
state.select(&:failure?)
state.reject(&:success?)
state.reject(&:failure?)