Skip to content

Instantly share code, notes, and snippets.

@codenamev
Created October 9, 2013 14:57
Show Gist options
  • Save codenamev/6902624 to your computer and use it in GitHub Desktop.
Save codenamev/6902624 to your computer and use it in GitHub Desktop.
Ruby-style sum method for Array
# USAGE:
# given:
# <div class="price" data-amount="1">$1.00</div>
# <div class="price" data-amount="2">$2.00</div>
# > $('.price').get().sum (price) -> $(price).data('amount')
# > 3
Array::sum = (fn = (x) -> x) ->
@reduce ((a, b) -> a + fn b), 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment