Skip to content

Instantly share code, notes, and snippets.

@nwjsmith
Forked from headius/proc_composition.rb
Created September 22, 2010 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nwjsmith/592188 to your computer and use it in GitHub Desktop.
Save nwjsmith/592188 to your computer and use it in GitHub Desktop.
Composable procs
class Proc
def <<(other)
case other
when Proc
Proc.new do |*args|
call(other.call(*args))
end
else
call(other)
end
end
def >>(other)
raise TypeError unless Proc === other
Proc.new do |*args|
other.call(call(*args))
end
end
end
times_six = lambda { |i| i * 2 } << lambda { |i| i * 3 }
times_two_plus_one = lambda { |i| i * 2 } >> lambda { |i| i + 1 }
puts times_six.call(3)
puts times_two_plus_one.call(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment