These utilities are loosely inspired by the STL: http://www.sgi.com/tech/stl/stl_index.html
No attempt is made at genericity. When we do support different types of collections, it is with different function names. Genericity is not natively supported by ECMAScript, nor is multi-dispatch, and while emulating it would be possible, we deem it not worth it. Another approach would be to introduce iterator objects for each underlying collection object, but that too is a bit heavy weight in both API complexity and overhead.
The collection types which have at least some algorithm functions available are:
perhaps use Duff's Device http://home.earthlink.net/~kendrasg/info/js_opt/jsOptMain.html#duffsdevice
Document which algorithms allow mutating function arguments.
Static Public Member Functions | |
void | for_map (Object map, Function binary_func) |
Iterate through the properties of Object map calling the binary function binary_func with the two arguments of property name and value. | |
Array | transform_map (Object map, Function binary_func, Array arr) |
Similar to for_map, except that the return values of binary_func are collected into the Array arr. | |
void | for_each (Array arr, Function unary_func) |
Call unary_func on each Array element. | |
void | for_each_call (Array arr, Object obj, Function unary_func) |
Unlike for_each, use obj.call to invoke the member function func on each Array element. | |
void | for_each_call2 (Array arr, Object obj, Function binary_func, Object arg2) |
Just like for_each_call, but a binary function, and the second argument is the fixed arg2. | |
Number | find (Array arr, Object val) |
Returns index of first element that is == val. | |
Object | find_value (Array arr, Object val) |
Like find, but returns the matching value rather than the index. | |
Number | find_if (Array arr, Function unary_predicate) |
Returns index of first element for which unary_predicate is true. | |
Object | find_if_value (Array arr, Function unary_func) |
Like find_if, but rather than returning an index, it returns the first return value from unary_func that tests as true. | |
Object | find_if_nth_value (Array arr, Number n, Function unary_func) |
Like find_if_value, but returns the nth (zero-based) value returned from unary_func that tests as true. | |
Object | find_if_specified (Array arr, Function unary_func) |
Like find_if_value, but returns the first first value that is not null or undefined (vs. | |
Number | count_if (Array arr, Function unary_predicate) |
Counts elements for which unary_predicate is true. | |
Array | generate (Array arr, Function zeroary_func) |
Set all Array members with the result from calling the generator (with no arguments). | |
Array | generate_n (Array arr, Number n, Function zeroary_func) |
Set the first n members of the Array by calling the generator. | |
Number | lower_bound (Array arr, Object val, Function less) |
Return index of first element not less than val. | |
Number | upper_bound (Array arr, Object val, Function less) |
Returns index of first element for which val is less than the element. | |
Array | transform (Array arr, Function unary_func, Array dest) |
Collection the results of invoking unary_func on each element. | |
Object | toMap (Array arr, Function unary_func, Object map) |
Initialize a map with keys being the results of calling unary_func on each element. | |
Object | toSet (Array arr, Object map) |
Initialize a map with keys taken from arr, and the value true. | |
Array | copy (Array arr, Array dest) |
Copy arr into dest. |
|
Copy arr into dest. The same indexes are used (versus doing a push).
|
|
Counts elements for which unary_predicate is true.
|
|
Returns index of first element that is == val. Returns -1 if none.
|
|
Returns index of first element for which unary_predicate is true. Returns -1 if none.
|
|
Like find_if_value, but returns the nth (zero-based) value returned from unary_func that tests as true.
|
|
Like find_if_value, but returns the first first value that is not null or undefined (vs. testing for true).
|
|
Like find_if, but rather than returning an index, it returns the first return value from unary_func that tests as true.
|
|
Like find, but returns the matching value rather than the index. Returns undefined if none match.
|
|
Call unary_func on each Array element. No return value.
|
|
Unlike for_each, use obj.call to invoke the member function func on each Array element.
|
|
Iterate through the properties of Object map calling the binary function binary_func with the two arguments of property name and value. Note that it is assumed that the caller has not added any extraneous properties or will filter those out in func. This does no filtering.
|
|
Set all Array members with the result from calling the generator (with no arguments). Assumes the Array length has already been set.
|
|
Set the first n members of the Array by calling the generator.
|
|
Return index of first element not less than val. This is the first position where val could be inserted in a way preserving the order imposed by the "less" comparator. Returns length if val is greater than all elements.
|
|
Initialize a map with keys being the results of calling unary_func on each element. The corresponding map values are the element values used to produce those keys.
|
|
Initialize a map with keys taken from arr, and the value true.
|
|
Collection the results of invoking unary_func on each element. Note that the same index is used in dest and arr; the results are not just push'd onto dest.
|
|
Similar to for_map, except that the return values of binary_func are collected into the Array arr.
|
|
Returns index of first element for which val is less than the element. This is the highest index where val could be inserted preserving the order imposed by the "less" comparator. Returns length if none. If val does not exist in the container, lower_bound and upper_bound are the same. If val exists exactly once in the container, then lower_bound returns the index of that val, and upper_bound returns the index of that val. Take a look at your favorite C++ text for more information.
|