Created
October 9, 2013 14:57
-
-
Save codenamev/6902624 to your computer and use it in GitHub Desktop.
Ruby-style sum method for Array
This file contains hidden or 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
# 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