Skip to content

Instantly share code, notes, and snippets.

@ukstudio
Created February 3, 2012 06:48
Show Gist options
  • Save ukstudio/1728574 to your computer and use it in GitHub Desktop.
Save ukstudio/1728574 to your computer and use it in GitHub Desktop.
compose
# lambda/proc ver
class Proc
def self.compose(f, g)
lambda {|*args| f[g[*args]] }
end
def <<(g)
Proc.compose(self,g)
end
def <=(*args)
self[*args]
end
end
unlines = lambda{|seq| seq.join("\n") }
sort = lambda{|seq| seq.sort }
lines = lambda{|str| str.split("\n") }
putStr = lambda{|str| puts str}
str = ARGV.join("\n")
putStr << unlines << sort << lines <= str
# method ver
class Method
def <<(g)
Proc.compose(self,g)
end
def <=(*args)
self[*args]
end
end
def unlines(seq)
seq.join("\n")
end
def sort(seq)
seq.sort
end
def lines(str)
str.split("\n")
end
def putStr(str)
puts str
end
method(:putStr) << method(:unlines) << method(:sort) << method(:lines) <= str
@ukstudio
Copy link
Author

ukstudio commented Feb 3, 2012

1.8で使いたい場合はProc#[]をProc#callにする必要がある。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment