Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add filter-map #336

Merged
merged 1 commit into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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