<commit_before>require 'benchmark'
require 'helper'

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
      size  = array.size
      block = proc { |x| sleep(delay) && x + 1 }
      block_without_sleep = proc { |x| x + 1 }
      expected = array.collect(&block_without_sleep)
      elapsed_time = Benchmark.realtime do
        expect(subject.pmap(array, &block)).to eq(expected)
      end
      expect(elapsed_time).to be_between(delay, delay * size)
    end
  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
      size  = array.size
      block = proc { |x| sleep(delay) && x.reverse }
      block_without_sleep = proc(&:reverse)
      expected = array.collect(&block_without_sleep).flatten!(1)
      elapsed_time = Benchmark.realtime do
        expect(subject.flat_pmap(array, &block)).to eq(expected)
      end
      expect(elapsed_time).to be_between(delay, delay * size)
    end
  end
end
<commit_msg>Fix bug where flat_pmap can return nil<commit_after>require 'benchmark'
require 'helper'

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
      size  = array.size
      block = proc { |x| sleep(delay) && x + 1 }
      block_without_sleep = proc { |x| x + 1 }
      expected = array.collect(&block_without_sleep)
      elapsed_time = Benchmark.realtime do
        expect(subject.pmap(array, &block)).to eq(expected)
      end
      expect(elapsed_time).to be_between(delay, delay * size)
    end
  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.flat_map(&block))
    end

    it 'flat maps in parallel' do
      delay = 0.1
      array = (0..4).to_a.combination(2).to_a
      size  = array.size
      block = proc { |x| sleep(delay) && x.reverse }
      block_without_sleep = proc(&:reverse)
      expected = array.collect(&block_without_sleep).flatten!(1)
      elapsed_time = Benchmark.realtime do
        expect(subject.flat_pmap(array, &block)).to eq(expected)
      end
      expect(elapsed_time).to be_between(delay, delay * size)
    end
  end
end

{'fraction_non_alphanumeric': 0.1328103868001082, 'fraction_numerical': 0.011631052204490127, 'mean_word_length': 2.6183953033268104, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6170688', 'n_tokens_mistral': 1301, 'n_tokens_neox': 1264, 'n_words': 316}