Skip to content

Instantly share code, notes, and snippets.

@threeifbywhiskey
Created March 17, 2014 09:12
Show Gist options
  • Save threeifbywhiskey/9596162 to your computer and use it in GitHub Desktop.
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.
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