parallel.es
Version:
Simple parallelization for EcmaScript
42 lines (41 loc) • 1.18 kB
JavaScript
/**
* Creates an iterator that iterates over the given array
* @param data the array
* @param T element type
* @returns the iterator
*/
function toIterator(data) {
return data[Symbol.iterator]();
}
exports.toIterator = toIterator;
/**
* Converts the given iterator to an array
* @param iterator the iterator that is to be converted into an array
* @param T element type
* @returns {T[]} the array representation of the given iterator
*/
function toArray(iterator) {
var result = [];
var current;
/* tslint:disable:no-conditional-assignment */
while (!(current = iterator.next()).done) {
result.push(current.value);
}
return result;
}
exports.toArray = toArray;
/**
* Flattens the given array.
* @param deepArray the array to flatten
* @param type of the array elements
* @returns returns an array containing all the values contained in the sub arrays of deep array.
*/
function flattenArray(deepArray) {
if (deepArray.length === 0) {
return [];
}
var head = deepArray[0], tail = deepArray.slice(1);
return Array.prototype.concat.apply(head, tail);
}
exports.flattenArray = flattenArray;
;