Skip to content

Instantly share code, notes, and snippets.

@zaach
Forked from kriskowal/map-reduce.js
Created April 8, 2010 19:38
Show Gist options
  • Save zaach/360446 to your computer and use it in GitHub Desktop.
Save zaach/360446 to your computer and use it in GitHub Desktop.
// Map/Reduce with Promises on Narwhal+Node
var SYSTEM = require("system");
var EL = require("event-loop");
var Q = require("narwhal/promise");
var UTIL = require("narwhal/util");
// to simulate a long latency, promise-returning API
var delay = function (timeout) {
var deferred = Q.Deferred();
EL.setTimeout(function () {
deferred.resolve();
}, timeout);
return deferred.promise;
};
// map
var sumP = UTIL.range(10).map(function (n) {
// UTIL range returns a list from 0 to 9
return Q.when(delay(1000), function () {
return n;
});
// reduce
}).reduce(function (sumP2, nP) {
return Q.when(nP, function (n) {
return Q.when(sumP2, function (sum) {
return sum + n;
});
});
});
return Q.when(sumP, function (sum) {
SYSTEM.print(sum);
});
// Narwhal kriskowal/narwhal/future: http://github.com/kriskowal/narwhal/commit/b32f48a3663b703407f85f9d33d9b29b4b99ef60
// Node kriskowal/node/narwhal-master: http://github.com/kriskowal/node/commit/522b28e2bc8a95effd21804bfc7d2689806bac51
// at narwhal/packages/narwhal-node (make, but install is not necessary)
// Does not yet compile on Linux due to iconv being in libc instead of libiconv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment