Skip to content

Commit

Permalink
Add futures
Browse files Browse the repository at this point in the history
  • Loading branch information
earldouglas committed Jun 11, 2015
1 parent 0aa40ff commit d7d44f6
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 1 deletion.
42 changes: 41 additions & 1 deletion teep.js
Expand Up @@ -143,7 +143,6 @@ var edc;
}
};
};

if (tail === undefined || tail === null) {
return list(head, _nil);
} else if (head === undefined || head === null) {
Expand All @@ -166,13 +165,54 @@ var edc;
}
};

var future = function (f) {
return {
apply: function (k) {
return f(k);
},
map: function (g) {
return future (function (k) {
return f(function (x) {
return k(g(x));
});
});
},
flatMap: function (g) {
return future (function (k) {
return f(function (x) {
return g(x).apply(k);
});
});
},
sequence: function (f2) {
var xs = [];
return future(function (k) {
var kk = function() {
if (xs.length === 2) {
k(xs);
}
};
f(function (x) {
xs.unshift(x);
kk();
});
f2.apply(function (x) {
xs.push(x);
kk();
});
});
}
};
};

edc.teep = {
array: array,
fn: fn,
option: option,
validation: validation,
list: list,
promise: promise,
future: future,
};

for (var i in edc.teep) {
Expand Down
70 changes: 70 additions & 0 deletions test/future_test.js
@@ -0,0 +1,70 @@
'use strict';

var teep = require('../teep.js');
var future = teep.future;
var jsc = require('jsverify');

var async = function(x) {
return function (k) {
k(x);
};
};

var asyncF = function (f) {
return function (x) {
return future(function (k) {
k(f(x));
});
};
};

exports.future = {
'future.apply': function (test) {
jsc.check(jsc.forall('number',
function (x) {
var verify = function (z) {
if (x === z) {
test.done();
}
};
future(async(x)).apply(verify);
}
));
},
'future.map': function (test) {
jsc.check(jsc.forall('number', 'number -> number',
function (x, f) {
var verify = function (z) {
if (f(x) === z) {
test.done();
}
};
future(async(x)).map(f).apply(verify);
}
));
},
'future.flatMap': function (test) {
jsc.check(jsc.forall('number', 'number -> number',
function (x, f) {
var verify = function (z) {
if (f(x) === z) {
test.done();
}
};
future(async(x)).flatMap(asyncF(f)).apply(verify);
}
));
},
'future.sequence': function (test) {
jsc.check(jsc.forall('number',
function (x) {
var verify = function (z) {
if (z.length === 2 && z[0] === x && z[1] === x) {
test.done();
}
};
future(async(x)).sequence(future(async(x))).apply(verify);
}
));
},
};

0 comments on commit d7d44f6

Please sign in to comment.