Comparing version 12.0.0-beta.1 to 12.0.0-beta.2
787
index.js
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('concurrify'), require('sanctuary-type-identifiers'), require('sanctuary-show')) : | ||
typeof define === 'function' && define.amd ? define(['concurrify', 'sanctuary-type-identifiers', 'sanctuary-show'], factory) : | ||
(global = global || self, global.Fluture = factory(global.concurrify, global.sanctuaryTypeIdentifiers, global.sanctuaryShow)); | ||
}(this, function (concurrify, type, show) { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('sanctuary-type-identifiers'), require('sanctuary-show'), require('concurrify')) : | ||
typeof define === 'function' && define.amd ? define(['sanctuary-type-identifiers', 'sanctuary-show', 'concurrify'], factory) : | ||
(global = global || self, global.Fluture = factory(global.sanctuaryTypeIdentifiers, global.sanctuaryShow, global.concurrify)); | ||
}(this, function (type, show, concurrify) { 'use strict'; | ||
concurrify = concurrify && concurrify.hasOwnProperty('default') ? concurrify['default'] : concurrify; | ||
type = type && type.hasOwnProperty('default') ? type['default'] : type; | ||
show = show && show.hasOwnProperty('default') ? show['default'] : show; | ||
concurrify = concurrify && concurrify.hasOwnProperty('default') ? concurrify['default'] : concurrify; | ||
@@ -130,2 +130,6 @@ var FL = { | ||
function showArg(x){ | ||
return show(x) + ' :: ' + type.parse(type(x)).name; | ||
} | ||
function error(message){ | ||
@@ -142,6 +146,26 @@ return new Error(message); | ||
it + '() expects its ' + ordinal[at] + ' argument to ' + expected + '.' + | ||
'\n Actual: ' + show(actual) + ' :: ' + type.parse(type(actual)).name | ||
'\n Actual: ' + showArg(actual) | ||
); | ||
} | ||
function invalidArgumentOf(expected){ | ||
return function(it, at, actual){ | ||
return invalidArgument(it, at, expected, actual); | ||
}; | ||
} | ||
function invalidArity(f, args){ | ||
return new TypeError( | ||
f.name + '() expects to be called with a single argument per invocation\n' + | ||
' Saw: ' + args.length + ' arguments' + | ||
Array.prototype.slice.call(args).map(function(arg, i){ | ||
return '\n ' + ( | ||
ordinal[i] ? | ||
ordinal[i].charAt(0).toUpperCase() + ordinal[i].slice(1) : | ||
'Argument ' + String(i + 1) | ||
) + ': ' + showArg(arg); | ||
}).join('') | ||
); | ||
} | ||
function invalidNamespace(m, x){ | ||
@@ -280,7 +304,39 @@ return ( | ||
function Next(x){ | ||
return {done: false, value: x}; | ||
} | ||
function Done(x){ | ||
return {done: true, value: x}; | ||
} | ||
function isIteration(x){ | ||
return isObject(x) && isBoolean(x.done); | ||
} | ||
/*eslint no-cond-assign:0, no-constant-condition:0 */ | ||
function alwaysTrue(){ | ||
return true; | ||
} | ||
var any = {pred: alwaysTrue, error: invalidArgumentOf('be anything')}; | ||
var func = {pred: isFunction, error: invalidArgumentOf('be a Function')}; | ||
var future = {pred: isFuture, error: invalidFutureArgument}; | ||
var positiveInteger = {pred: isUnsigned, error: invalidArgumentOf('be a positive Integer')}; | ||
function application(n, f, type, args, prev){ | ||
if(args.length < 2 && type.pred(args[0])) return captureApplicationContext(prev, n, f); | ||
var e = args.length > 1 ? invalidArity(f, args) : type.error(f.name, n - 1, args[0]); | ||
captureStackTrace(e, f); | ||
throw withExtraContext(e, prev); | ||
} | ||
function application1(f, type, args){ | ||
return application(1, f, type, args, nil); | ||
} | ||
function Future(computation){ | ||
if(!isFunction(computation)) throw invalidArgument('Future', 0, 'be a Function', computation); | ||
return new Computation(captureContext(nil, 'first application of Future', Future), computation); | ||
var context = application1(Future, func, arguments); | ||
return new Computation(context, computation); | ||
} | ||
@@ -293,2 +349,4 @@ | ||
Future['@@type'] = $$type; | ||
Future[FL.of] = resolve; | ||
Future[FL.chainRec] = chainRec; | ||
@@ -304,2 +362,27 @@ Future.prototype['@@show'] = function Future$show(){ | ||
Future.prototype[FL.ap] = function Future$FL$ap(other){ | ||
var context = captureContext(nil, 'a Fantasy Land dispatch to ap', Future$FL$ap); | ||
return other._transform(new ApTransformation(context, this)); | ||
}; | ||
Future.prototype[FL.map] = function Future$FL$map(mapper){ | ||
var context = captureContext(nil, 'a Fantasy Land dispatch to map', Future$FL$map); | ||
return this._transform(new MapTransformation(context, mapper)); | ||
}; | ||
Future.prototype[FL.bimap] = function Future$FL$bimap(lmapper, rmapper){ | ||
var context = captureContext(nil, 'a Fantasy Land dispatch to bimap', Future$FL$bimap); | ||
return this._transform(new BimapTransformation(context, lmapper, rmapper)); | ||
}; | ||
Future.prototype[FL.chain] = function Future$FL$chain(mapper){ | ||
var context = captureContext(nil, 'a Fantasy Land dispatch to chain', Future$FL$chain); | ||
return this._transform(new ChainTransformation(context, mapper)); | ||
}; | ||
Future.prototype[FL.alt] = function Future$FL$alt(other){ | ||
var context = captureContext(nil, 'a Fantasy Land dispatch to alt', Future$FL$alt); | ||
return this._transform(new AltTransformation(context, other)); | ||
}; | ||
Future.prototype.extractLeft = function Future$extractLeft(){ | ||
@@ -387,2 +470,59 @@ return []; | ||
var Never = createInterpreter(0, 'never', function Never$interpret(){ | ||
return noop; | ||
}); | ||
Never.prototype._isNever = true; | ||
var never = new Never(nil); | ||
function isNever(x){ | ||
return isFuture(x) && x._isNever === true; | ||
} | ||
var Crash = createInterpreter(1, 'crash', function Crash$interpret(rec){ | ||
rec(this.$1); | ||
return noop; | ||
}); | ||
function crash(x){ | ||
return new Crash(application1(crash, any, arguments), x); | ||
} | ||
var Reject = createInterpreter(1, 'reject', function Reject$interpret(rec, rej){ | ||
rej(this.$1); | ||
return noop; | ||
}); | ||
Reject.prototype.extractLeft = function Reject$extractLeft(){ | ||
return [this.$1]; | ||
}; | ||
function reject(x){ | ||
return new Reject(application1(reject, any, arguments), x); | ||
} | ||
var Resolve = createInterpreter(1, 'resolve', function Resolve$interpret(rec, rej, res){ | ||
res(this.$1); | ||
return noop; | ||
}); | ||
Resolve.prototype.extractRight = function Resolve$extractRight(){ | ||
return [this.$1]; | ||
}; | ||
function resolve(x){ | ||
return new Resolve(application1(resolve, any, arguments), x); | ||
} | ||
//Note: This function is not curried because it's only used to satisfy the | ||
// Fantasy Land ChainRec specification. | ||
function chainRec(step, init){ | ||
return resolve(Next(init))._transform(new ChainTransformation(nil, function chainRec$recur(o){ | ||
return o.done ? | ||
resolve(o.value) : | ||
step(Next, Done, o.value)._transform(new ChainTransformation(nil, chainRec$recur)); | ||
})); | ||
} | ||
var Transformer = createInterpreter(2, '', function Transformer$interpret(rec, rej, res){ | ||
@@ -547,93 +687,2 @@ | ||
var Never = createInterpreter(0, 'never', function Never$interpret(){ | ||
return noop; | ||
}); | ||
Never.prototype._isNever = true; | ||
var never = new Never(nil); | ||
function isNever(x){ | ||
return isFuture(x) && x._isNever === true; | ||
} | ||
function alwaysTrue(){ | ||
return true; | ||
} | ||
function invalidArgumentOf(expected){ | ||
return function(it, at, actual){ | ||
return invalidArgument(it, at, expected, actual); | ||
}; | ||
} | ||
function isFutureArray(xs){ | ||
if(!isArray(xs)) return false; | ||
for(var i = 0; i < xs.length; i++){ | ||
if(!isFuture(xs[i])) return false; | ||
} | ||
return true; | ||
} | ||
var alternative = {pred: isAlt, error: invalidArgumentOf('have Alt implemented')}; | ||
var any = {pred: alwaysTrue, error: invalidArgumentOf('be anything')}; | ||
var apply = {pred: isApply, error: invalidArgumentOf('have Apply implemented')}; | ||
var bifunctor = {pred: isBifunctor, error: invalidArgumentOf('have Bifunctor implemented')}; | ||
var func = {pred: isFunction, error: invalidArgumentOf('be a Function')}; | ||
var functor = {pred: isFunctor, error: invalidArgumentOf('have Functor implemented')}; | ||
var future = {pred: isFuture, error: invalidFutureArgument}; | ||
var monad = {pred: isChain, error: invalidArgumentOf('have Chain implemented')}; | ||
var positiveInteger = {pred: isUnsigned, error: invalidArgumentOf('be a positive Integer')}; | ||
var futureArray = { | ||
pred: isFutureArray, | ||
error: invalidArgumentOf('be an Array of valid Futures') | ||
}; | ||
function application(n, f, type, x, prev){ | ||
if(type.pred(x)) return captureApplicationContext(prev, n, f); | ||
var e = type.error(f.name, n - 1, x); | ||
captureStackTrace(e, f); | ||
throw withExtraContext(e, prev); | ||
} | ||
function application1(f, type, x){ | ||
return application(1, f, type, x, nil); | ||
} | ||
var Crash = createInterpreter(1, 'crash', function Crash$interpret(rec){ | ||
rec(this.$1); | ||
return noop; | ||
}); | ||
function crash(x){ | ||
return new Crash(application1(crash, any, x), x); | ||
} | ||
var Reject = createInterpreter(1, 'reject', function Reject$interpret(rec, rej){ | ||
rej(this.$1); | ||
return noop; | ||
}); | ||
Reject.prototype.extractLeft = function Reject$extractLeft(){ | ||
return [this.$1]; | ||
}; | ||
function reject(x){ | ||
return new Reject(application1(reject, any, x), x); | ||
} | ||
var Resolve = createInterpreter(1, 'resolve', function Resolve$interpret(rec, rej, res){ | ||
res(this.$1); | ||
return noop; | ||
}); | ||
Resolve.prototype.extractRight = function Resolve$extractRight(){ | ||
return [this.$1]; | ||
}; | ||
function resolve(x){ | ||
return new Resolve(application1(resolve, any, x), x); | ||
} | ||
function BaseTransformation$rejected(x){ | ||
@@ -703,2 +752,12 @@ this.cancel(); | ||
var ApTransformation = createTransformation(1, 'ap', { | ||
resolved: function ApTransformation$resolved(f){ | ||
if(isFunction(f)) return this.$1._transform(new MapTransformation(this.context, f)); | ||
throw typeError( | ||
'ap expects the second Future to resolve to a Function\n' + | ||
' Actual: ' + show(f) | ||
); | ||
} | ||
}); | ||
var AltTransformation = createTransformation(1, 'alt', { | ||
@@ -708,7 +767,49 @@ rejected: function AltTransformation$rejected(){ return this.$1 } | ||
var MapTransformation = createTransformation(1, 'map', { | ||
resolved: function MapTransformation$resolved(x){ | ||
return new Resolve(this.context, call(this.$1, x)); | ||
} | ||
}); | ||
var BimapTransformation = createTransformation(2, 'bimap', { | ||
rejected: function BimapTransformation$rejected(x){ | ||
return new Reject(this.context, call(this.$1, x)); | ||
}, | ||
resolved: function BimapTransformation$resolved(x){ | ||
return new Resolve(this.context, call(this.$2, x)); | ||
} | ||
}); | ||
var ChainTransformation = createTransformation(1, 'chain', { | ||
resolved: function ChainTransformation$resolved(x){ return call(this.$1, x) } | ||
}); | ||
var After = createInterpreter(2, 'after', function After$interpret(rec, rej, res){ | ||
var id = setTimeout(res, this.$1, this.$2); | ||
return function After$cancel(){ clearTimeout(id); }; | ||
}); | ||
After.prototype.extractRight = function After$extractRight(){ | ||
return [this.$2]; | ||
}; | ||
function alwaysNever(_){ | ||
return never; | ||
} | ||
function after(time){ | ||
var context1 = application1(after, positiveInteger, arguments); | ||
return time === Infinity ? alwaysNever : (function after(value){ | ||
var context2 = application(2, after, any, arguments, context1); | ||
return new After(context2, time, value); | ||
}); | ||
} | ||
var alternative = {pred: isAlt, error: invalidArgumentOf('have Alt implemented')}; | ||
function alt(left){ | ||
if(isFuture(left)){ | ||
var context1 = application1(alt, future, left); | ||
var context1 = application1(alt, future, arguments); | ||
return function alt(right){ | ||
var context2 = application(2, alt, future, right, context1); | ||
var context2 = application(2, alt, future, arguments, context1); | ||
return right._transform(new AltTransformation(context2, left)); | ||
@@ -718,5 +819,5 @@ }; | ||
var context = application1(alt, alternative, left); | ||
var context = application1(alt, alternative, arguments); | ||
return function alt(right){ | ||
application(2, alt, alternative, right, context); | ||
application(2, alt, alternative, arguments, context); | ||
return left[FL.alt](right); | ||
@@ -726,33 +827,21 @@ }; | ||
var MapTransformation = createTransformation(1, 'map', { | ||
resolved: function MapTransformation$resolved(x){ | ||
return new Resolve(this.context, call(this.$1, x)); | ||
} | ||
var AndTransformation = createTransformation(1, 'and', { | ||
resolved: function AndTransformation$resolved(){ return this.$1 } | ||
}); | ||
function map(f){ | ||
var context1 = application1(map, func, f); | ||
return function map(m){ | ||
var context2 = application(2, map, functor, m, context1); | ||
return isFuture(m) ? | ||
m._transform(new MapTransformation(context2, f)) : | ||
m[FL.map](f); | ||
function and(left){ | ||
var context1 = application1(and, future, arguments); | ||
return function and(right){ | ||
var context2 = application(2, and, future, arguments, context1); | ||
return right._transform(new AndTransformation(context2, left)); | ||
}; | ||
} | ||
var ApTransformation = createTransformation(1, 'ap', { | ||
resolved: function ApTransformation$resolved(f){ | ||
if(isFunction(f)) return this.$1._transform(new MapTransformation(this.context, f)); | ||
throw typeError( | ||
'ap expects the second Future to resolve to a Function\n' + | ||
' Actual: ' + show(f) | ||
); | ||
} | ||
}); | ||
var apply = {pred: isApply, error: invalidArgumentOf('have Apply implemented')}; | ||
function ap(mx){ | ||
if(isFuture(mx)){ | ||
var context1 = application1(ap, future, mx); | ||
var context1 = application1(ap, future, arguments); | ||
return function ap(mf){ | ||
var context2 = application(2, ap, future, mf, context1); | ||
var context2 = application(2, ap, future, arguments, context1); | ||
return mf._transform(new ApTransformation(context2, mx)); | ||
@@ -762,5 +851,5 @@ }; | ||
var context = application1(ap, apply, mx); | ||
var context = application1(ap, apply, arguments); | ||
return function ap(mf){ | ||
application(2, ap, apply, mf, context); | ||
application(2, ap, apply, arguments, context); | ||
return mx[FL.ap](mf); | ||
@@ -770,60 +859,82 @@ }; | ||
var BimapTransformation = createTransformation(2, 'bimap', { | ||
rejected: function BimapTransformation$rejected(x){ | ||
return new Reject(this.context, call(this.$1, x)); | ||
}, | ||
resolved: function BimapTransformation$resolved(x){ | ||
return new Resolve(this.context, call(this.$2, x)); | ||
function invalidPromise(p, f, a){ | ||
return typeError( | ||
'encaseP() expects the function it\'s given to return a Promise/Thenable' | ||
+ '\n Actual: ' + show(p) + '\n From calling: ' + show(f) | ||
+ '\n With: ' + show(a) | ||
); | ||
} | ||
var EncaseP = createInterpreter(2, 'encaseP', function EncaseP$interpret(rec, rej, res){ | ||
var open = true, fn = this.$1, arg = this.$2, p; | ||
try{ | ||
p = fn(arg); | ||
}catch(e){ | ||
rec(wrapException(e, this)); | ||
return noop; | ||
} | ||
if(!isThenable(p)){ | ||
rec(wrapException(invalidPromise(p, fn, arg), this)); | ||
return noop; | ||
} | ||
p.then(function EncaseP$res(x){ | ||
if(open){ | ||
open = false; | ||
res(x); | ||
} | ||
}, function EncaseP$rej(x){ | ||
if(open){ | ||
open = false; | ||
rej(x); | ||
} | ||
}); | ||
return function EncaseP$cancel(){ open = false; }; | ||
}); | ||
function bimap(f){ | ||
var context1 = application1(bimap, func, f); | ||
return function bimap(g){ | ||
var context2 = application(2, bimap, func, g, context1); | ||
return function bimap(m){ | ||
var context3 = application(3, bimap, bifunctor, m, context2); | ||
return isFuture(m) ? | ||
m._transform(new BimapTransformation(context3, f, g)) : | ||
m[FL.bimap](f, g); | ||
}; | ||
function encaseP(f){ | ||
var context1 = application1(encaseP, func, arguments); | ||
return function encaseP(x){ | ||
var context2 = application(2, encaseP, any, arguments, context1); | ||
return new EncaseP(context2, f, x); | ||
}; | ||
} | ||
var ChainTransformation = createTransformation(1, 'chain', { | ||
resolved: function ChainTransformation$resolved(x){ return call(this.$1, x) } | ||
function attemptP(_){ | ||
return encaseP.apply(this, arguments)(undefined); | ||
} | ||
var Encase = createInterpreter(2, 'encase', function Encase$interpret(rec, rej, res){ | ||
var fn = this.$1, r; | ||
try{ r = fn(this.$2); }catch(e){ rej(e); return noop } | ||
res(r); | ||
return noop; | ||
}); | ||
function chain(f){ | ||
var context1 = application1(chain, func, f); | ||
return function chain(m){ | ||
var context2 = application(2, chain, monad, m, context1); | ||
return isFuture(m) ? | ||
m._transform(new ChainTransformation(context2, f)) : | ||
m[FL.chain](f); | ||
function encase(f){ | ||
var context1 = application1(encase, func, arguments); | ||
return function encase(x){ | ||
var context2 = application(2, encase, any, arguments, context1); | ||
return new Encase(context2, f, x); | ||
}; | ||
} | ||
function Next(x){ | ||
return {done: false, value: x}; | ||
function attempt(_){ | ||
return encase.apply(this, arguments)(undefined); | ||
} | ||
function Done(x){ | ||
return {done: true, value: x}; | ||
} | ||
var bifunctor = {pred: isBifunctor, error: invalidArgumentOf('have Bifunctor implemented')}; | ||
function isIteration(x){ | ||
return isObject(x) && isBoolean(x.done); | ||
function bimap(f){ | ||
var context1 = application1(bimap, func, arguments); | ||
return function bimap(g){ | ||
var context2 = application(2, bimap, func, arguments, context1); | ||
return function bimap(m){ | ||
var context3 = application(3, bimap, bifunctor, arguments, context2); | ||
return isFuture(m) ? | ||
m._transform(new BimapTransformation(context3, f, g)) : | ||
m[FL.bimap](f, g); | ||
}; | ||
}; | ||
} | ||
//Note: This function is not curried because it's only used to satisfy the | ||
// Fantasy Land ChainRec specification. | ||
function chainRec(step, init){ | ||
return resolve(Next(init))._transform(new ChainTransformation(nil, function chainRec$recur(o){ | ||
return o.done ? | ||
resolve(o.value) : | ||
step(Next, Done, o.value)._transform(new ChainTransformation(nil, chainRec$recur)); | ||
})); | ||
} | ||
function Eager(future){ | ||
@@ -899,130 +1010,2 @@ var _this = this; | ||
var ParallelApTransformation = | ||
createParallelTransformation('parallelAp', earlyCrash, earlyReject, noop, { | ||
resolved: function ParallelApTransformation$resolved(f){ | ||
if(isFunction(f)) return this.$1._transform(new MapTransformation(this.context, f)); | ||
throw typeError( | ||
'parallelAp expects the second Future to resolve to a Function\n' + | ||
' Actual: ' + show(f) | ||
); | ||
} | ||
}); | ||
function parallelAp(mx){ | ||
var context1 = application1(parallelAp, future, mx); | ||
return function parallelAp(mf){ | ||
var context2 = application(2, parallelAp, future, mf, context1); | ||
return mf._transform(new ParallelApTransformation(context2, mx)); | ||
}; | ||
} | ||
var RaceTransformation = | ||
createParallelTransformation('race', earlyCrash, earlyReject, earlyResolve, {}); | ||
function race(left){ | ||
var context1 = application1(race, future, left); | ||
return function race(right){ | ||
var context2 = application(2, race, future, right, context1); | ||
return right._transform(new RaceTransformation(context2, left)); | ||
}; | ||
} | ||
var After = createInterpreter(2, 'after', function After$interpret(rec, rej, res){ | ||
var id = setTimeout(res, this.$1, this.$2); | ||
return function After$cancel(){ clearTimeout(id); }; | ||
}); | ||
After.prototype.extractRight = function After$extractRight(){ | ||
return [this.$2]; | ||
}; | ||
function alwaysNever(_){ | ||
return never; | ||
} | ||
function after(time){ | ||
var context1 = application1(after, positiveInteger, time); | ||
return time === Infinity ? alwaysNever : (function after(value){ | ||
var context2 = application(2, after, any, value, context1); | ||
return new After(context2, time, value); | ||
}); | ||
} | ||
var AndTransformation = createTransformation(1, 'and', { | ||
resolved: function AndTransformation$resolved(){ return this.$1 } | ||
}); | ||
function and(left){ | ||
var context1 = application1(and, future, left); | ||
return function and(right){ | ||
var context2 = application(2, and, future, right, context1); | ||
return right._transform(new AndTransformation(context2, left)); | ||
}; | ||
} | ||
function invalidPromise(p, f, a){ | ||
return typeError( | ||
'encaseP() expects the function it\'s given to return a Promise/Thenable' | ||
+ '\n Actual: ' + show(p) + '\n From calling: ' + show(f) | ||
+ '\n With: ' + show(a) | ||
); | ||
} | ||
var EncaseP = createInterpreter(2, 'encaseP', function EncaseP$interpret(rec, rej, res){ | ||
var open = true, fn = this.$1, arg = this.$2, p; | ||
try{ | ||
p = fn(arg); | ||
}catch(e){ | ||
rec(wrapException(e, this)); | ||
return noop; | ||
} | ||
if(!isThenable(p)){ | ||
rec(wrapException(invalidPromise(p, fn, arg), this)); | ||
return noop; | ||
} | ||
p.then(function EncaseP$res(x){ | ||
if(open){ | ||
open = false; | ||
res(x); | ||
} | ||
}, function EncaseP$rej(x){ | ||
if(open){ | ||
open = false; | ||
rej(x); | ||
} | ||
}); | ||
return function EncaseP$cancel(){ open = false; }; | ||
}); | ||
function encaseP(f){ | ||
var context1 = application1(encaseP, func, f); | ||
return function encaseP(x){ | ||
var context2 = application(2, encaseP, any, x, context1); | ||
return new EncaseP(context2, f, x); | ||
}; | ||
} | ||
function attemptP(f){ | ||
return encaseP(f)(undefined); | ||
} | ||
var Encase = createInterpreter(2, 'encase', function Encase$interpret(rec, rej, res){ | ||
var fn = this.$1, r; | ||
try{ r = fn(this.$2); }catch(e){ rej(e); return noop } | ||
res(r); | ||
return noop; | ||
}); | ||
function encase(f){ | ||
var context1 = application1(encase, func, f); | ||
return function encase(x){ | ||
var context2 = application(2, encase, any, x, context1); | ||
return new Encase(context2, f, x); | ||
}; | ||
} | ||
function attempt(f){ | ||
return encase(f)(undefined); | ||
} | ||
var PairTransformation = createTransformation(1, 'pair', { | ||
@@ -1042,5 +1025,5 @@ resolved: function PairTransformation$resolved(x){ | ||
function both(left){ | ||
var context1 = application1(both, future, left); | ||
var context1 = application1(both, future, arguments); | ||
return function both(right){ | ||
var context2 = application(2, both, future, right, context1); | ||
var context2 = application(2, both, future, arguments, context1); | ||
return right._transform(new BothTransformation(context2, left)); | ||
@@ -1164,3 +1147,3 @@ }; | ||
function cache(m){ | ||
return new Cache(application1(cache, future, m), m); | ||
return new Cache(application1(cache, future, arguments), m); | ||
} | ||
@@ -1173,5 +1156,5 @@ | ||
function chainRej(f){ | ||
var context1 = application1(chainRej, func, f); | ||
var context1 = application1(chainRej, func, arguments); | ||
return function chainRej(m){ | ||
var context2 = application(2, chainRej, future, m, context1); | ||
var context2 = application(2, chainRej, future, arguments, context1); | ||
return m._transform(new ChainRejTransformation(context2, f)); | ||
@@ -1181,4 +1164,16 @@ }; | ||
var monad = {pred: isChain, error: invalidArgumentOf('have Chain implemented')}; | ||
function chain(f){ | ||
var context1 = application1(chain, func, arguments); | ||
return function chain(m){ | ||
var context2 = application(2, chain, monad, arguments, context1); | ||
return isFuture(m) ? | ||
m._transform(new ChainTransformation(context2, f)) : | ||
m[FL.chain](f); | ||
}; | ||
} | ||
function done(callback){ | ||
var context1 = application1(done, func, callback); | ||
var context1 = application1(done, func, arguments); | ||
function done$res(x){ | ||
@@ -1188,3 +1183,3 @@ callback(null, x); | ||
return function done(m){ | ||
application(2, done, future, m, context1); | ||
application(2, done, future, arguments, context1); | ||
return m._interpret(raise, callback, done$res); | ||
@@ -1195,3 +1190,3 @@ }; | ||
function extractLeft(m){ | ||
application1(extractLeft, future, m); | ||
application1(extractLeft, future, arguments); | ||
return m.extractLeft(); | ||
@@ -1201,3 +1196,3 @@ } | ||
function extractRight(m){ | ||
application1(extractRight, future, m); | ||
application1(extractRight, future, arguments); | ||
return m.extractRight(); | ||
@@ -1216,7 +1211,7 @@ } | ||
function fold(f){ | ||
var context1 = application1(fold, func, f); | ||
var context1 = application1(fold, func, arguments); | ||
return function fold(g){ | ||
var context2 = application(2, fold, func, g, context1); | ||
var context2 = application(2, fold, func, arguments, context1); | ||
return function fold(m){ | ||
var context3 = application(3, fold, future, m, context2); | ||
var context3 = application(3, fold, future, arguments, context2); | ||
return m._transform(new FoldTransformation(context3, f, g)); | ||
@@ -1228,9 +1223,9 @@ }; | ||
function forkCatch(f){ | ||
var context1 = application1(forkCatch, func, f); | ||
var context1 = application1(forkCatch, func, arguments); | ||
return function forkCatch(g){ | ||
var context2 = application(2, forkCatch, func, g, context1); | ||
var context2 = application(2, forkCatch, func, arguments, context1); | ||
return function forkCatch(h){ | ||
var context3 = application(3, forkCatch, func, h, context2); | ||
var context3 = application(3, forkCatch, func, arguments, context2); | ||
return function forkCatch(m){ | ||
application(4, forkCatch, future, m, context3); | ||
application(4, forkCatch, future, arguments, context3); | ||
return m._interpret(f, g, h); | ||
@@ -1243,7 +1238,7 @@ }; | ||
function fork(f){ | ||
var context1 = application1(fork, func, f); | ||
var context1 = application1(fork, func, arguments); | ||
return function fork(g){ | ||
var context2 = application(2, fork, func, g, context1); | ||
var context2 = application(2, fork, func, arguments, context1); | ||
return function fork(m){ | ||
application(3, fork, future, m, context2); | ||
application(3, fork, future, arguments, context2); | ||
return m._interpret(raise, f, g); | ||
@@ -1327,3 +1322,3 @@ }; | ||
function go(generator){ | ||
return new Go(application1(go, func, generator), generator); | ||
return new Go(application1(go, func, arguments), generator); | ||
} | ||
@@ -1434,7 +1429,7 @@ | ||
function hook(acquire){ | ||
var context1 = application1(hook, future, acquire); | ||
var context1 = application1(hook, future, arguments); | ||
return function hook(dispose){ | ||
var context2 = application(2, hook, func, dispose, context1); | ||
var context2 = application(2, hook, func, arguments, context1); | ||
return function hook(consume){ | ||
var context3 = application(3, hook, func, consume, context2); | ||
var context3 = application(3, hook, func, arguments, context2); | ||
return new Hook(context3, acquire, dispose, consume); | ||
@@ -1455,5 +1450,5 @@ }; | ||
function lastly(cleanup){ | ||
var context1 = application1(lastly, future, cleanup); | ||
var context1 = application1(lastly, future, arguments); | ||
return function lastly(program){ | ||
var context2 = application(2, lastly, future, program, context1); | ||
var context2 = application(2, lastly, future, arguments, context1); | ||
return program._transform(new LastlyTransformation(context2, cleanup)); | ||
@@ -1470,5 +1465,5 @@ }; | ||
function mapRej(f){ | ||
var context1 = application1(mapRej, func, f); | ||
var context1 = application1(mapRej, func, arguments); | ||
return function mapRej(m){ | ||
var context2 = application(2, mapRej, future, m, context1); | ||
var context2 = application(2, mapRej, future, arguments, context1); | ||
return m._transform(new MapRejTransformation(context2, f)); | ||
@@ -1478,2 +1473,14 @@ }; | ||
var functor = {pred: isFunctor, error: invalidArgumentOf('have Functor implemented')}; | ||
function map(f){ | ||
var context1 = application1(map, func, arguments); | ||
return function map(m){ | ||
var context2 = application(2, map, functor, arguments, context1); | ||
return isFuture(m) ? | ||
m._transform(new MapTransformation(context2, f)) : | ||
m[FL.map](f); | ||
}; | ||
} | ||
var Node = createInterpreter(1, 'node', function Node$interpret(rec, rej, res){ | ||
@@ -1505,5 +1512,37 @@ function Node$done(err, val){ | ||
function node(f){ | ||
return new Node(application1(node, func, f), f); | ||
return new Node(application1(node, func, arguments), f); | ||
} | ||
var ParallelApTransformation = | ||
createParallelTransformation('parallelAp', earlyCrash, earlyReject, noop, { | ||
resolved: function ParallelApTransformation$resolved(f){ | ||
if(isFunction(f)) return this.$1._transform(new MapTransformation(this.context, f)); | ||
throw typeError( | ||
'parallelAp expects the second Future to resolve to a Function\n' + | ||
' Actual: ' + show(f) | ||
); | ||
} | ||
}); | ||
function parallelAp(mx){ | ||
var context1 = application1(parallelAp, future, arguments); | ||
return function parallelAp(mf){ | ||
var context2 = application(2, parallelAp, future, arguments, context1); | ||
return mf._transform(new ParallelApTransformation(context2, mx)); | ||
}; | ||
} | ||
function isFutureArray(xs){ | ||
if(!isArray(xs)) return false; | ||
for(var i = 0; i < xs.length; i++){ | ||
if(!isFuture(xs[i])) return false; | ||
} | ||
return true; | ||
} | ||
var futureArray = { | ||
pred: isFutureArray, | ||
error: invalidArgumentOf('be an Array of valid Futures') | ||
}; | ||
var Parallel = createInterpreter(2, 'parallel', function Parallel$interpret(rec, rej, res){ | ||
@@ -1554,5 +1593,5 @@ | ||
function parallel(max){ | ||
var context1 = application1(parallel, positiveInteger, max); | ||
var context1 = application1(parallel, positiveInteger, arguments); | ||
return function parallel(ms){ | ||
var context2 = application(2, parallel, futureArray, ms, context1); | ||
var context2 = application(2, parallel, futureArray, arguments, context1); | ||
return ms.length === 0 ? emptyArray : new Parallel(context2, max, ms); | ||
@@ -1562,4 +1601,27 @@ }; | ||
var RaceTransformation = | ||
createParallelTransformation('race', earlyCrash, earlyReject, earlyResolve, {}); | ||
function race(left){ | ||
var context1 = application1(race, future, arguments); | ||
return function race(right){ | ||
var context2 = application(2, race, future, arguments, context1); | ||
return right._transform(new RaceTransformation(context2, left)); | ||
}; | ||
} | ||
function uncurry(f){ | ||
return function(a, b){ | ||
return f(a)(b); | ||
}; | ||
} | ||
var Par = concurrify(Future, never, uncurry(race), uncurry(parallelAp)); | ||
function isParallel(x){ | ||
return x instanceof Par || type(x) === Par['@@type']; | ||
} | ||
function promise(m){ | ||
application1(promise, future, m); | ||
application1(promise, future, arguments); | ||
return new Promise(function promise$computation(res, rej){ | ||
@@ -1585,5 +1647,5 @@ m._interpret(rej, rej, res); | ||
function rejectAfter(time){ | ||
var context1 = application1(rejectAfter, positiveInteger, time); | ||
var context1 = application1(rejectAfter, positiveInteger, arguments); | ||
return time === Infinity ? alwaysNever$1 : (function rejectAfter(value){ | ||
var context2 = application(2, rejectAfter, any, value, context1); | ||
var context2 = application(2, rejectAfter, any, arguments, context1); | ||
return new RejectAfter(context2, time, value); | ||
@@ -1593,2 +1655,9 @@ }); | ||
var parallel$1 = {pred: isParallel, error: invalidArgumentOf('be a ConcurrentFuture')}; | ||
function seq(par){ | ||
application1(seq, parallel$1, arguments); | ||
return par.sequential; | ||
} | ||
var SwapTransformation = createTransformation(0, 'swap', { | ||
@@ -1604,3 +1673,3 @@ resolved: function SwapTransformation$resolved(x){ | ||
function swap(m){ | ||
var context = application1(swap, future, m); | ||
var context = application1(swap, future, arguments); | ||
return m._transform(new SwapTransformation(context)); | ||
@@ -1610,5 +1679,5 @@ } | ||
function value(res){ | ||
var context1 = application1(value, func, res); | ||
var context1 = application1(value, func, arguments); | ||
return function value(m){ | ||
application(2, value, future, m, context1); | ||
application(2, value, future, arguments, context1); | ||
function value$rej(x){ | ||
@@ -1625,53 +1694,9 @@ raise(error( | ||
Future[FL.of] = resolve; | ||
Future[FL.chainRec] = chainRec; | ||
Future.prototype[FL.ap] = function Future$FL$ap(other){ | ||
var context = captureContext(nil, 'a Fantasy Land dispatch to ap', Future$FL$ap); | ||
return other._transform(new ApTransformation(context, this)); | ||
}; | ||
Future.prototype[FL.map] = function Future$FL$map(mapper){ | ||
var context = captureContext(nil, 'a Fantasy Land dispatch to map', Future$FL$map); | ||
return this._transform(new MapTransformation(context, mapper)); | ||
}; | ||
Future.prototype[FL.bimap] = function Future$FL$bimap(lmapper, rmapper){ | ||
var context = captureContext(nil, 'a Fantasy Land dispatch to bimap', Future$FL$bimap); | ||
return this._transform(new BimapTransformation(context, lmapper, rmapper)); | ||
}; | ||
Future.prototype[FL.chain] = function Future$FL$chain(mapper){ | ||
var context = captureContext(nil, 'a Fantasy Land dispatch to chain', Future$FL$chain); | ||
return this._transform(new ChainTransformation(context, mapper)); | ||
}; | ||
Future.prototype[FL.alt] = function Future$FL$alt(other){ | ||
var context = captureContext(nil, 'a Fantasy Land dispatch to alt', Future$FL$alt); | ||
return this._transform(new AltTransformation(context, other)); | ||
}; | ||
function uncurry(f){ | ||
return function(a, b){ | ||
return f(a)(b); | ||
}; | ||
} | ||
var Par = concurrify(Future, never, uncurry(race), uncurry(parallelAp)); | ||
function isParallel(x){ | ||
return x instanceof Par || type(x) === Par['@@type']; | ||
} | ||
function seq(par){ | ||
if(!isParallel(par)) throw invalidArgument('seq', 0, 'be a ConcurrentFuture', par); | ||
return par.sequential; | ||
} | ||
var Fluture = /*#__PURE__*/Object.freeze({ | ||
Par: Par, | ||
isParallel: isParallel, | ||
seq: seq, | ||
'default': Future, | ||
Future: Future, | ||
reject: reject, | ||
resolve: resolve, | ||
isFuture: isFuture, | ||
@@ -1707,7 +1732,7 @@ never: never, | ||
parallel: parallel, | ||
Par: Par, | ||
promise: promise, | ||
race: race, | ||
rejectAfter: rejectAfter, | ||
reject: reject, | ||
resolve: resolve, | ||
seq: seq, | ||
swap: swap, | ||
@@ -1714,0 +1739,0 @@ value: value, |
{ | ||
"name": "fluture", | ||
"version": "12.0.0-beta.1", | ||
"version": "12.0.0-beta.2", | ||
"description": "FantasyLand compliant (monadic) alternative to Promises", | ||
@@ -82,4 +82,4 @@ "main": "index", | ||
"rollup": "^1.0.0", | ||
"rollup-plugin-commonjs": "^9.1.6", | ||
"rollup-plugin-node-resolve": "^4.0.0", | ||
"rollup-plugin-commonjs": "^10.0.0", | ||
"rollup-plugin-node-resolve": "^5.0.0", | ||
"sanctuary-benchmark": "^1.0.0", | ||
@@ -86,0 +86,0 @@ "sanctuary-either": "^1.1.0", |
@@ -102,3 +102,3 @@ # [![Fluture](logo.png)](#butterfly) | ||
Fluture is hosted in full with all of its dependencies at | ||
https://cdn.jsdelivr.net/gh/fluture-js/Fluture@12.0.0-beta.1/dist/bundle.js | ||
https://cdn.jsdelivr.net/gh/fluture-js/Fluture@12.0.0-beta.2/dist/bundle.js | ||
@@ -105,0 +105,0 @@ This script will add `Fluture` to the global scope. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
168223
51
3163