Created
March 16, 2011 12:20
-
-
Save rummelonp/872389 to your computer and use it in GitHub Desktop.
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
<script src="jquery.min.js" type="text/javascript"></script> | |
<script src="jquery.enumerable.js" type="text/javascript"></script> |
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
var square = $([1, 2, 3, 4, 5]).collect(function(index, value) { | |
return value * value; | |
}); // => [1, 4, 9, 16, 25] | |
var even = $([1, 2, 3, 4, 5]).select(function(index, value) { | |
return value % 2 == 0; | |
}); // => [2, 4] | |
var sum = $([1, 2, 3, 4, 5]).inject(0, function(result, index, value) { | |
return result + value; | |
}); // => 15 | |
var included = $([1, 2, 3, 4, 5]).include(1); // => true | |
included = $([1, 2, 3, 4, 5]).include(10); // => false | |
var max = $([1, 10, 100, 1000]).max(); // => 1000 | |
max = $([-3, -2, -1]).maxBy(function(index, value) { | |
return value * value; | |
}); // => -3 | |
var min = $([-1000, -100, -10, 0]).min(); // => -1000 | |
min = $([-4, -2, 1, 3]).minBy(function(index, value) { | |
return value * value; | |
}); // => 1 | |
var numbers = $([1, 2, 3, 4, 5, 6]).partition(function(index, value) { | |
return value % 2 == 0; | |
}); // => [[2, 4, 6], [1, 3, 5]] | |
var even = numbers[0]; // => [2, 4, 6] | |
var odd = numbers[1]; // => [1, 3, 5] | |
var sorted_by_string = $(["200", "30", "1000"]).sortBy(function(index, value) { | |
return value; | |
}); // => ["1000", "200", "30"] | |
var sorted_by_number = $(["200", "30", "1000"]).sortBy(function(index, value) { | |
return parseInt(value); | |
}); // => ["30", "200", "1000"] | |
var zip = $([1, 2, 3]).zip([4, 5, 6], [7, 8, 9]) // => [[1, 4, 7], [2, 5, 8], [3, 6, 9]] | |
var cube = $([1, 2, 3]).zip([1, 2, 3], [1, 2, 3], function(arr_value) { | |
return $(arr_value).inject(1, function(result, index, value) { | |
return result * value; | |
}); | |
}); // => [1, 8, 27]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment