Created
June 19, 2011 05:52
-
-
Save tlrobinson/1033805 to your computer and use it in GitHub Desktop.
Implementations of parallel and serial "map" function for Q lib
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
function mapParallel(array, callback) { | |
return array.reduce(function(acc_p, element, index, array) { | |
return Q.when(callback(element, index, array), function(res) { | |
return Q.when(acc_p, function(acc) { | |
return acc.concat([res]); | |
}); | |
}); | |
}, []); | |
} | |
function mapSerial(array, callback) { | |
return array.reduce(function(acc_p, element, index, array) { | |
return Q.when(acc_p, function(acc) { | |
return Q.when(callback(element, index, array), function(res) { | |
return acc.concat([res]); | |
}); | |
}); | |
}, []); | |
} |
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 Q = require("q"); | |
function mapParallel(array, callback) { | |
return array.reduce(function(acc_p, element, index, array) { | |
return Q.when(callback(element, index, array), function(res) { | |
return Q.when(acc_p, function(acc) { | |
return acc.concat([res]); | |
}); | |
}); | |
}, []); | |
} | |
function mapSerial(array, callback) { | |
return array.reduce(function(acc_p, element, index, array) { | |
return Q.when(acc_p, function(acc) { | |
return Q.when(callback(element, index, array), function(res) { | |
return acc.concat([res]); | |
}); | |
}); | |
}, []); | |
} | |
function reduceSerial(array, callback, initialValue) { | |
return array.reduce(function(acc_p, element, index, array) { | |
return Q.when(acc_p, function(acc) { | |
return callback(acc, element, index, array); | |
}) | |
}, initialValue); | |
} | |
var TEST_ARRAY = ["A", "B", "C", "D", "E", "F"]; | |
Q.when(reduceSerial(TEST_ARRAY, function(accumulator, element, index, array) { | |
console.log("REDUCE SERIAL ITEM ", element, index, array); | |
return delayedConcatenate(accumulator, element); | |
}, ""), function(result) { | |
console.log("REDUCE SERIAL RESULT", result); | |
}); | |
Q.when(mapSerial(TEST_ARRAY, function(element, index, array) { | |
console.log("MAP SERIAL ITEM ", element, index, array); | |
return delayedMakeLower(element); | |
}), function(result) { | |
console.log("MAP SERIAL RESULT ", result); | |
}); | |
Q.when(mapParallel(TEST_ARRAY, function(element, index, array) { | |
console.log("MAP PARALLEL ITEM ", element, index, array); | |
return delayedMakeLower(element); | |
}), function(result) { | |
console.log("MAP PARALLEL RESULT ", result); | |
}); | |
function delayedMakeLower(value) { | |
var d = Q.defer(); | |
setTimeout(function() { d.resolve(value.toLowerCase()); }, 250); | |
return d.promise; | |
} | |
function delayedConcatenate(a, b) { | |
var d = Q.defer(); | |
setTimeout(function() { d.resolve(a + b); }, 250); | |
return d.promise; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment