Skip to content

Instantly share code, notes, and snippets.

@wookay
Created March 20, 2015 08:01
Show Gist options
  • Save wookay/25430088033973b2b2df to your computer and use it in GitHub Desktop.
Save wookay/25430088033973b2b2df to your computer and use it in GitHub Desktop.
unary-function-chainer.jl
# http://www.codewars.com/kata/unary-function-chainer
# http://stackoverflow.com/questions/28713934/how-to-implement-unary-function-chainer-using-python
f1 = (x) -> x*2
f2 = (x) -> x+2
f3 = (x) -> x^2
chained(funcs) = reduce((f,g)->(n)->n|>f|>g, funcs)
@assert 4 == chained([f1 f2 f3])(0)
@assert 36 == chained([f1 f2 f3])(2)
@assert 12 == chained([f3 f2 f1])(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment