Skip to content

Instantly share code, notes, and snippets.

@kjlape
Last active April 3, 2019 15:03
Show Gist options
  • Select an option

  • Save kjlape/c5d15c48ea81fb2ad3e0a646c8aa2aeb to your computer and use it in GitHub Desktop.

Select an option

Save kjlape/c5d15c48ea81fb2ad3e0a646c8aa2aeb to your computer and use it in GitHub Desktop.
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