Skip to content

Commit

Permalink
add filter-map
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Apr 18, 2023
1 parent 3928204 commit b1c3055
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/parallel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions spec/cases/filter_map.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions spec/parallel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b1c3055

Please sign in to comment.