-
-
Save oaleynik/830a0ba0d99f08f28cdf to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
The MIT License (MIT) | |
Copyright (c) 2014 | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
*/ | |
function toArray(args) { | |
return Array.prototype.slice.call(args); | |
} | |
function Future() { | |
this.slots = []; | |
this.failslots = []; | |
this.actions = []; | |
this.failactions = []; | |
} | |
Future.prototype.ready = function(slot) { | |
if(this.completed) | |
slot(this.value); | |
else | |
this.slots.push(slot); | |
} | |
Future.prototype.failed = function(slot) { | |
if(this.hasFailed) | |
slot(this.error); | |
else | |
this.failslots.push(slot); | |
} | |
Future.prototype.complete = function(val) { | |
var me = this; | |
if(this.completed || this.hasFailed) | |
throw "Can't complete an already settled future!"; | |
this.value = val; | |
this.completed = true; | |
for(var i=0, len=this.slots.length; i<len; i++) { | |
this.slots[i](val); | |
} | |
this.slots = null; | |
setTimeout(function() { | |
for(var i=0, len=me.actions.length; i<len; i++) { | |
me.actions[i](val); | |
} | |
me.actions = null; | |
}); | |
} | |
Future.prototype.fail = function(err) { | |
var me = this; | |
if(this.completed || this.hasFailed) | |
throw "Can't complete an already settled future!" | |
this.hasFailed = true; | |
this.error = err; | |
for(var i=0, len=this.failslots.length; i<len; i++) { | |
this.failslots[i](err); | |
} | |
this.failslots = null; | |
setTimeout(function() { | |
for(var i=0, len=me.failactions.length; i<len; i++) { | |
me.failactions[i](err); | |
} | |
me.failactions = null; | |
}); | |
} | |
Future.prototype.fmap = function(fn) { | |
var fut = new Future(); | |
this.ready(function(val) { | |
try { | |
fut.complete( fn(val) ); | |
} catch(err) { | |
fut.fail(err); | |
} | |
}); | |
this.failed(function(err) { | |
fut.fail( err ); | |
}); | |
return fut; | |
} | |
Future.prototype.fmapError = function(fn) { | |
var fut = new Future(); | |
this.ready(function(val) { | |
fut.complete( val ); | |
}); | |
this.failed(function(err) { | |
try { | |
fut.complete( fn(err) ); | |
} catch(err1) { | |
fut.fail( err1 ); | |
} | |
}); | |
return fut; | |
} | |
Future.prototype.flatten = function() { | |
var fut = new Future(); | |
this.failed(function(err) { | |
fut.fail(err); | |
}); | |
this.ready(function(fut2) { | |
fut2.failed( function(err){ | |
fut.fail(err); | |
} ); | |
}); | |
this.ready(function(fut2) { | |
fut2.ready( function(val){ | |
fut.complete(val); | |
} ); | |
}); | |
return fut; | |
} | |
Future.prototype.flatMap = function( fn ) { | |
return this.fmap(fn).flatten(); | |
} | |
Future.prototype.flatMapError = function( fn ) { | |
return this.fmapError(fn).flatten(); | |
} | |
Future.lift1 = function(fn) { | |
return function(fut) { | |
return fut.fmap(f); | |
} | |
} | |
Future.lift2 = function(fn) { | |
return function(fut1, fut2) { | |
return fut1.flatMap(function(value1) { | |
return fut2.flatMap(function(value2) { | |
return Future.unit( fn(value1, value2) ); | |
}); | |
}); | |
} | |
} | |
Future.lift3 = function(fn) { | |
return function(fut1, fut2, fut3) { | |
return fut1.flatMap(function(value1) { | |
return fut2.flatMap(function(value2) { | |
return fut3.flatMap(function(value3) { | |
return Future.unit( fn(value1, value2, value3) ); | |
}); | |
}); | |
}); | |
} | |
} | |
Future.lift = function(fn) { | |
return function() { | |
var args = toArray(arguments), | |
ctx = this; | |
return bindArg(0, []); | |
function bindArg(index, actArgs) { | |
return args[index].flatMap(function(val) { | |
actArgs = actArgs.concat(val); | |
return (index < args.length - 1) ? | |
bindArg(index+1, actArgs) : | |
Future.unit( fn.apply(ctx, actArgs) ); | |
}); | |
} | |
} | |
} | |
Future.never = function() { | |
return new Future(); | |
} | |
Future.unit = function(val) { | |
var fut = new Future(); | |
fut.complete(val); | |
return fut; | |
} | |
Future.delay = function(v, ms) { | |
var f = new Future(); | |
setTimeout(function(){ | |
f.complete(v); | |
}, ms); | |
return f; | |
} | |
function logF(f) { | |
f.do(function(v) { | |
console.log('Future completed with', v); | |
}); | |
} | |
function logErrF(f) { | |
f.doError(function(err) { | |
console.log('Future failed with', err); | |
}); | |
} | |
var fs = require('fs'); | |
function readFileF(file, options) { | |
var f = new Future(); | |
fs.readFile(file, options, function (err, data) { | |
if (err) f.fail(err); | |
else | |
f.complete(data); | |
}); | |
return f; | |
} | |
function readDirF(path) { | |
var f = new Future(); | |
fs.readdir(path, function (err, files) { | |
if (err) f.fail(err); | |
else | |
f.complete(files); | |
}); | |
return f; | |
} | |
var concat = Future.lift(function() { | |
return toArray(arguments).join(' '); | |
}); | |
result = readDirF('testdir').flatMap(function(files) { | |
var filesF = files.map( function(file) { | |
return readFileF('testdir/' + file, {encoding: 'utf8'}); | |
}); | |
return concat.apply(null, filesF); | |
}); | |
logF( result ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment