Skip to content

Commit

Permalink
Fix bug where flat_pmap can return nil
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Sep 6, 2015
1 parent b655c7e commit e22a560
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/twitter/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def deprecate_alias(new_name, old_name, &block)
# @return [Array, Enumerator]
def flat_pmap(enumerable)
return to_enum(:flat_pmap, enumerable) unless block_given?
pmap(enumerable, &Proc.new).flatten!(1)
pmap(enumerable, &Proc.new).flatten(1)
end
module_function :flat_pmap

Expand Down
26 changes: 26 additions & 0 deletions spec/twitter/utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@

describe Twitter::Utils do
describe '#pmap' do
it 'returns an array' do
expect(subject.flat_pmap([], &:reverse)).to be_an(Array)
end

it 'behaves like map' do
array = (0..9).to_a
block = proc { |x| x + 1 }
expect(subject.pmap(array, &block)).to eq(array.collect(&block))
end

it 'maps in parallel' do
delay = 0.1
array = (0..9).to_a
Expand All @@ -18,6 +28,22 @@
end

describe '#flat_pmap' do
it 'always returns an array' do
expect(subject.flat_pmap([], &:reverse)).to be_an(Array)
end

it 'behaves like map for a flat array' do
array = (0..9).to_a
block = proc { |x| x + 1 }
expect(subject.flat_pmap(array, &block)).to eq(array.collect(&block))
end

it 'behaves like flat_map' do
array = (0..4).to_a.combination(2).to_a
block = proc { |x| x.reverse }
expect(subject.flat_pmap(array, &block)).to eq(array.collect(&block).flatten(1))
end

it 'flat maps in parallel' do
delay = 0.1
array = (0..4).to_a.combination(2).to_a
Expand Down

0 comments on commit e22a560

Please sign in to comment.