Skip to content

Instantly share code, notes, and snippets.

@lambdalisue
Created August 28, 2011 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lambdalisue/1176935 to your computer and use it in GitHub Desktop.
Save lambdalisue/1176935 to your computer and use it in GitHub Desktop.
Array::fold = (f, init)->
c = init
for e in this
c = f(e, c)
c
if Array::reduce is undefined
Array::reduce = (f) -> @[1..].fold(f, @[0])
sum = (a)->
a.reduce (e, c) -> e + c
alert sum([1..20]) #...shows 210
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment