Skip to content

Commit

Permalink
Add Future.bichain
Browse files Browse the repository at this point in the history
  • Loading branch information
dicearr committed Jan 5, 2020
1 parent fd37497 commit 28575bc
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export {ap} from './src/ap.js';
export {attemptP} from './src/attempt-p.js';
export {attempt} from './src/attempt.js';
export {bimap} from './src/bimap.js';
export {bichain} from './src/bichain.js';
export {both} from './src/both.js';
export {cache} from './src/cache.js';
export {chainRej} from './src/chain-rej.js';
Expand Down
18 changes: 18 additions & 0 deletions src/bichain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {createTransformation, future, application1, application, func} from './future.js';
import {call} from './internal/utils.js';

export var BichainTransformation = createTransformation(2, 'bichain', {
rejected: function BichainTransformation$rejected(x){ return call(this.$1, x) },
resolved: function BichainTransformation$resolved(x){ return call(this.$2, x) }
});

export function bichain(f){
var context1 = application1(bichain, func, arguments);
return function bichain(g){
var context2 = application(2, bichain, func, arguments, context1);
return function bichain(m){
var context3 = application(3, bichain, future, arguments, context1, context2);
return m._transform(new BichainTransformation(context3, f, g));
};
};
}
9 changes: 9 additions & 0 deletions test/prop/0.algebra.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
alt,
and,
ap,
bichain,
bimap,
cache,
chain,
Expand Down Expand Up @@ -54,6 +55,14 @@ property('ap composition using map', _mx, _mf, _mf, function (mx, mf, mg){
return eq(ap(mx)(ap(mf)(map(B)(mg))))(ap(ap(mx)(mf))(mg));
});

property('bichain left identity', _x, _fm, function (x, f){
return eq(bichain(f)(reject)(reject(x)))(f(x));
});

property('bichain right identity', _x, _fm, function (x, f){
return eq(bichain(resolve)(f)(resolve(x)))(f(x));
});

property('bimap identity', _mx, function (mx){
return eq(bimap(I)(I)(mx))(mx);
});
Expand Down
9 changes: 9 additions & 0 deletions test/prop/2.arbitrary.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import {
after,
and,
bichain,
bimap,
chain,
chainRej,
Expand Down Expand Up @@ -90,6 +91,14 @@ property('mapRej(f)(m) = bimap(f)(I)(m)', anyFuture, function (m){
return eq(mapRej(f)(m))(bimap(f)(I)(m));
});

property('Rejected m => chainRej(B(mk)(f))(m) = bichain(B(mk)(f))(reject)(m)', make, anyRejectedFuture, function (mk, m){
return eq(chainRej(B(mk)(f))(m))(bichain(B(mk)(f))(reject)(m));
});

property('Resolved m => chain(B(mk)(f))(m) = bichain(resolve)(B(mk)(f))(m)', make, anyResolvedFuture, function (mk, m){
return eq(chain(B(mk)(f))(m))(bichain(resolve)(B(mk)(f))(m));
});

property('mapRej(f)(m) = swap(map(f)(swap(m)))', anyFuture, function (m){
return eq(mapRej(f)(m))(swap(map(f)(swap(m))));
});
Expand Down
26 changes: 26 additions & 0 deletions test/unit/4.bichain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {bichain} from '../../index.js';
import {test, assertCrashed, assertRejected, assertResolved, assertValidFuture, eq, throwing, error} from '../util/util.js';
import {testFunction, functionArg, futureArg} from '../util/props.js';
import {resolvedSlow, resolved, rejected, rejectedSlow} from '../util/futures.js';
import {resolve, reject} from '../../index.js';

testFunction('bichain', bichain, [functionArg, functionArg, futureArg], assertValidFuture);

test('runs a bichain transformation on Futures', function (){
return Promise.all([
assertResolved(bichain(reject)(resolve)(resolved), 'resolved'),
assertResolved(bichain(reject)(resolve)(resolvedSlow), 'resolvedSlow'),
assertRejected(bichain(resolve)(reject)(resolved), 'resolved'),
assertRejected(bichain(resolve)(reject)(resolvedSlow), 'resolvedSlow'),
assertResolved(bichain(resolve)(reject)(rejected), 'rejected'),
assertResolved(bichain(resolve)(reject)(rejectedSlow), 'rejectedSlow'),
assertRejected(bichain(reject)(resolve)(rejected), 'rejected'),
assertRejected(bichain(reject)(resolve)(rejectedSlow), 'rejectedSlow'),
assertCrashed(bichain(throwing)(reject)(rejected), error),
assertCrashed(bichain(resolve)(throwing)(resolved), error),
]);
});

test('displays correctly as string', function (){
eq(bichain(resolve)(reject)(resolved).toString(), 'bichain (' + resolve.toString() + ') (' + reject.toString() + ') (resolve ("resolved"))');
});

0 comments on commit 28575bc

Please sign in to comment.