Module: Enumerable
- Defined in:
- lib/errgonomic/core_ext/enumerable.rb
Overview
The all-or-nothing collection, which Rust spells as a collect into
Option<Vec
Instance Method Summary collapse
-
#sequence_options ⇒ Errgonomic::Option::Any
Collect an Enumerable of Options into an Option of an Array, stopping at the first None.
-
#sequence_results ⇒ Errgonomic::Result::Any
Collect an Enumerable of Results into a Result of an Array, stopping at the first Err, which is returned as it stands so it keeps its error.
Instance Method Details
#sequence_options ⇒ Errgonomic::Option::Any
Collect an Enumerable of Options into an Option of an Array, stopping at the first None. A member that is not an Option raises unconditionally: with_ambiguous_downstream_errors relaxes what a block returned, not what a caller passed in.
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/errgonomic/core_ext/enumerable.rb', line 40 def values = [] each do |member| unless member.is_a?(Errgonomic::Option::Any) raise Errgonomic::TypeMismatchError, "cannot sequence_options #{member.class}; it is not an Option" end return None() if member.none? member.tap_some { |value| values << value } end Some(values) end |
#sequence_results ⇒ Errgonomic::Result::Any
Collect an Enumerable of Results into a Result of an Array, stopping at the first Err, which is returned as it stands so it keeps its error. A member that is not a Result raises on the same terms as sequence_options.
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/errgonomic/core_ext/enumerable.rb', line 81 def sequence_results values = [] each do |member| unless member.is_a?(Errgonomic::Result::Any) raise Errgonomic::TypeMismatchError, "cannot sequence_results #{member.class}; it is not a Result" end return member if member.err? member.tap_ok { |value| values << value } end Ok(values) end |