Skip to content

Instantly share code, notes, and snippets.

@maasha
Created October 2, 2014 13:53
Show Gist options
  • Save maasha/8a01e3571fc8506a6998 to your computer and use it in GitHub Desktop.
Save maasha/8a01e3571fc8506a6998 to your computer and use it in GitHub Desktop.
require 'pp'
def fib(n) n < 2 ? n : fib(n-1)+fib(n-2); end # Lousy Fibonacci calculator <- heavy job
input = (0 .. 10).to_enum
enums = [input]
threads = []
mutex = Mutex.new
10.times do |i|
thread = Thread.new(enums[i]) do |input|
mutex.synchronize do
enums[i + 1] = Enumerator.new do |yielder|
input.each { |e| yielder << (e + 1); fib(30) }
end.lazy
end
end
end
enums.last.each { |e| p e }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment