Skip to content

Instantly share code, notes, and snippets.

@maasha
Last active August 29, 2015 14:07
Show Gist options
  • Save maasha/e7fd1ce7de6f73c73cd0 to your computer and use it in GitHub Desktop.
Save maasha/e7fd1ce7de6f73c73cd0 to your computer and use it in GitHub Desktop.
Lazy enumeration in parallel
#!/usr/bin/env rbx
require 'pp'
input = (0 .. 10).to_enum.lazy
enums = [input]
threads = []
threads << Thread.new(enums[0]) do |enum|
enums[1] = Enumerator.new do |yielder|
enum.each { |e| yielder << (e + 1) }
end.lazy
end
threads << Thread.new(enums[1]) do |enum|
enums[2] = Enumerator.new do |yielder|
enum.each { |e| yielder << (e + 1) }
end.lazy
end
enums.last.each { |e| p e }
threads.each { |t| t.join }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment