Created
February 3, 2012 06:48
-
-
Save ukstudio/1728574 to your computer and use it in GitHub Desktop.
compose
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
| # 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
1.8で使いたい場合はProc#[]をProc#callにする必要がある。