diff --git a/lib/parallel.rb b/lib/parallel.rb index b22ef29..e6cec7b 100644 --- a/lib/parallel.rb +++ b/lib/parallel.rb @@ -304,6 +304,10 @@ def flat_map(*args, &block) map(*args, &block).flatten(1) end + def filter_map(*args, &block) + map(*args, &block).compact + end + # Number of physical processor cores on the current system. def physical_processor_count @physical_processor_count ||= begin diff --git a/spec/cases/filter_map.rb b/spec/cases/filter_map.rb new file mode 100644 index 0000000..74f17ba --- /dev/null +++ b/spec/cases/filter_map.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true +require './spec/cases/helper' + +result = Parallel.filter_map(['a', 'b', 'c']) do |x| + x if x != 'b' +end +print result.inspect diff --git a/spec/parallel_spec.rb b/spec/parallel_spec.rb index b4cdfcd..642fffd 100644 --- a/spec/parallel_spec.rb +++ b/spec/parallel_spec.rb @@ -540,6 +540,12 @@ def cpus end end + describe ".filter_map" do + it "yields object" do + ruby("spec/cases/filter_map.rb 2>&1").should == '["a", "c"]' + end + end + describe ".any?" do it "returns true if any result is truthy" do ruby("spec/cases/any_true.rb").split(',').should == ['true'] * 3 * 2