Created
March 17, 2010 21:36
-
-
Save floere/335737 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Just copy in TextMate and play | |
# | |
class Chain | |
def initialize lambdas | |
@lambdas = lambdas | |
end | |
# Wrapper. | |
# | |
def self.[] lam, *args | |
new args.map { |arg| lam[*arg] } | |
end | |
# Non-executing proc join. | |
# | |
def join prok | |
proks = [prok]*(@lambdas.size-1) | |
proks = @lambdas.zip(proks).flatten.compact | |
lambda { proks.each(&:call) } | |
end | |
# Executing proc join. | |
# | |
def join! prok | |
join(prok).call | |
end | |
def call *args | |
join(nil).call *args | |
end | |
end | |
module Kernel | |
def chained lambdas | |
Chain.new lambdas | |
end | |
end | |
restart = lambda { |i| lambda { puts "Restarting Server #{i}." } } | |
wait = lambda { puts "Waiting"; sleep 0.1 } | |
Chain[restart, 1, 2, 3].join! wait | |
# or | |
restarts = [1,2,3].map { |i| restart[i] } | |
chained(restarts).join! wait | |
# or funkier! :) | |
chain = chained(restarts) | |
chained([chain.join(chain), chain.join(wait)]).join! wait |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment