Created
March 17, 2014 09:12
-
-
Save threeifbywhiskey/9596162 to your computer and use it in GitHub Desktop.
A solution to /r/dailyprogrammer's 153rd Easy challenge using only lambdas, essentially.
This file contains 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
factorial = -> n { | |
n < 2 ? 1 : n * factorial[n - 1] | |
} | |
size = -> ary { | |
sz = 0 | |
(cut = -> { | |
sz += 1 | |
(ary = ary[1..-1]) == [] ? sz : cut[] | |
})[] | |
} | |
map = -> ary, fn { | |
sz = size[ary] | |
i = 0 | |
ret = [] | |
(mapper = -> { | |
ary[i] = fn[ary[i]] | |
(i += 1) == sz ? ary : mapper[] | |
})[] | |
} | |
triangle = -> n { | |
map[[*0..n], -> x { factorial[n] / (factorial[x] * factorial[n - x]) }] | |
} | |
each = -> ary, fn { | |
sz = size[ary] | |
i = 0 | |
(eacher = -> { | |
fn[ary[i]] | |
(i += 1) == sz ? ary : eacher[] | |
})[] | |
} | |
with_index = -> ary { | |
sz = size[ary] | |
i = 0 | |
ret = [] | |
(zipper = -> { | |
ret << [ary[i], i] | |
(i += 1) == sz ? ret : zipper[] | |
})[] | |
} | |
n = 14 | |
each[with_index[triangle[n - 1]], -> x { puts map[triangle[x[1]], -> y { y * x[0] }] * ' ' }] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment