Created
September 30, 2012 03:03
-
-
Save ryanflorence/3805727 to your computer and use it in GitHub Desktop.
Sub-classing Array with CoffeeScript
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
class Collection extends Array | |
constructor: (items...) -> | |
@splice 0, 0, items... | |
sum: -> @reduce (sum, n) -> sum + n |
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
c = new Collection 1, 2, 3 | |
console.assert c[0], 1 | |
c.reverse() | |
console.assert c[0], 3 | |
sum = c.sum() | |
console.assert sum, 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
class Collection extends Array
constructor: (items...) ->
@splice 0, 0, items...
sum: -> @reduce (sum, n) -> sum + n