Created
August 17, 2012 15:14
-
-
Save rcdilorenzo/3379731 to your computer and use it in GitHub Desktop.
Ruby-Like select for Array
This file contains 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
Array::select = (requestedElement) -> | |
count = {} | |
array = [] | |
for element in this | |
count[element] = (count[element] || 0) + 1 | |
if count[requestedElement] | |
for i in [1..count[requestedElement]] | |
array.push(requestedElement) | |
return array | |
else | |
return null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: Doesn't work for requesting an element's property in an array.