Skip to content

Instantly share code, notes, and snippets.

@alyssais
Created April 7, 2014 12:27
Show Gist options
  • Save alyssais/10019357 to your computer and use it in GitHub Desktop.
Save alyssais/10019357 to your computer and use it in GitHub Desktop.
A Ruby implementation of a waterfall control flow thingy.
module Async
class << self
def waterfall(objects, handler, &block)
enumerator = objects.each
prev_result = nil
run_next = lambda do
begin
# onwards!
prev_result = handler.(enumerator.next, prev_result) do |success|
success ? run_next.call : block.(false)
end
rescue StopIteration
# done!
block.(true)
end
end
run_next.call
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment