Created
September 27, 2012 07:33
-
-
Save certainty/3792694 to your computer and use it in GitHub Desktop.
generalized method chaining and array as applicative
This file contains 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
class Array | |
def >>(other); self + [other]; end | |
def to_proc(*args) | |
->(r) { inject(r){ |o,(m,*args)| o.send(m,*args) } } | |
end | |
def apply(rec) | |
self.to_proc[rec] | |
end | |
end | |
class Symbol | |
def >>(other); [self,other] end | |
end | |
puts %w(foo bar baz).map(&:upcase >> :reverse) | |
puts (1..9).map(&[[:+,3],[:-,2]]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment