Last active
April 3, 2019 15:03
-
-
Save kjlape/c5d15c48ea81fb2ad3e0a646c8aa2aeb 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
| if Gem::Version.new("2.6") <= Gem::Version.new(RUBY_VERSION) | |
| def compose(*funcs) | |
| funcs.map(&:to_proc).reduce(:<<) | |
| end | |
| else | |
| def compose(*funcs) | |
| funcs.map(&:to_proc).reduce { |f, g| ->(*x) { f.call(g.call(*x)) } } | |
| end | |
| end | |
| def a(x) | |
| "a(#{x})" | |
| end | |
| def b(x) | |
| "b(#{x})" | |
| end | |
| def c(x) | |
| "c(#{x})" | |
| end | |
| results = %w[functional programming].map(&compose(method(:a), method(:b), method(:c))) | |
| expected = ["a(b(c(functional)))", "a(b(c(programming)))"] | |
| raise "unexpected results! 😵\nactual: #{results}\nexpected: #{expected}" unless results == expected | |
| puts "✅ #{results}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment