exalted.future
Advanced tools
Comparing version 0.0.18 to 0.0.19
@@ -1,259 +0,2 @@ | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.exalted = global.exalted || {}, global.exalted.future = {}))); | ||
}(this, (function (exports) { 'use strict'; | ||
const compose = (...rest) => (...a) => | ||
rest | ||
.slice(0, rest.length - 1) | ||
.reduceRight((acc, func) => func(acc), rest.slice(rest.length - 1)[0](...a)); | ||
const map = (...functions) => functor => | ||
functions.reduceRight((functor, f) => functor.map(f), functor); | ||
const id = a => a; | ||
const noop = () => {}; | ||
const isNull = value => value === null || value === undefined; | ||
const Identity$1 = { Left: id, Right: id }; | ||
const iff = (f = id, a) => f(a); | ||
const log = (...a) => (console.log(...a), a[0]); | ||
const eq = (a, r) => r.cata({ Right: b => a === b, Left: b => a === b }); | ||
const Right = a => ({ | ||
ap: app => (app.isLeft ? app : app.map(b => iff(b, a))), | ||
alt: () => Right(a), | ||
bimap: (_, r) => iff(r, a), | ||
cata: (f = Identity) => f.Right(a), | ||
chain: f => iff(f, a), | ||
equals: r => eq(a, r), | ||
fold: f => iff(f, a), | ||
foldl: f => iff(f, null), | ||
foldr: f => iff(f, a), | ||
inspect: () => 'Right(' + a + ')', | ||
isLeft: false, | ||
map: f => Right(iff(f, a)), | ||
of: a => Right(a), | ||
swap: () => Left(a) | ||
}); | ||
Right.of = a => Right(a); | ||
const Left = a => ({ | ||
ap: app => (app.isLeft ? app : Left(a)), | ||
alt: () => Left(a), | ||
bimap: (l, _) => iff(l, a), | ||
cata: (f = Identity) => f.Left(a), | ||
chain: () => Left(a), | ||
map: () => Left(a), | ||
equals: l => eq(a, l), | ||
fold: f => iff(f, a), | ||
foldl: f => iff(f, a), | ||
foldr: f => iff(f, null), | ||
inspect: () => 'Left(' + a + ')', | ||
isLeft: true, | ||
of: a => Left(a), | ||
swap: () => Right(a) | ||
}); | ||
Left.of = a => Left(a); | ||
const nullable = x => (isNull(x) ? Left(x) : Right(x)); | ||
const Either = (l, r) => [Left(l), Right(r)]; | ||
const encase = f => { | ||
try { | ||
return Right(iff(f, null)) | ||
} catch (err) { | ||
return Left(err) | ||
} | ||
}; | ||
Either.encase = encase; | ||
Right.encase = encase; | ||
Left.encase = encase; | ||
Either.error = error => right => (isNull(error) ? Right(right) : Left(error)); | ||
const chain = action => f => | ||
Future((l, r) => { | ||
try { | ||
action(e => l(e), d => f(d).cata({ Left: l, Right: r })); | ||
} catch (e) { | ||
l(e); | ||
} | ||
}); | ||
const alt = action => alt => | ||
Future((l, r) => | ||
Future(action).cata({ | ||
Left: a => { | ||
const b = alt(a); | ||
b.chain ? b.chain(r) : l(b); | ||
}, | ||
Right: r | ||
})); | ||
const Future = action => ({ | ||
ap: () => Future(action), | ||
alt: alt(action), | ||
bimap: () => Future(action), | ||
map: func => chain(action)(x => Future.of(func(x))), | ||
cata: f => action(f.Left, f.Right), | ||
chain: chain(action), | ||
equals: () => false, | ||
fold: f => action(f, f), | ||
foldl: f => action(f, id), | ||
foldr: f => action(id, f), | ||
inspect: () => 'Future(?)', | ||
of: a => Future.of(a), | ||
swap: () => Future(action) | ||
}); | ||
Future.encase = f => { | ||
try { | ||
const a = f(); | ||
return Future((_, r) => r(a)) | ||
} catch (e) { | ||
return Future(l => l(e)) | ||
} | ||
}; | ||
Future.of = x => Future((_, resolve) => resolve(x)); | ||
Future.promise = promise => | ||
Future((reject, resolve) => Future.promise(promise.then(resolve, reject).catch(reject))); | ||
const all = futures => | ||
Future((left, right, errored = false) => | ||
futures.reduce( | ||
(results, future, i) => ( | ||
future.cata({ | ||
Left: error => !errored && ((errored = true), left(error)), | ||
Right: result => ( | ||
(results[i] = result), | ||
!errored && | ||
results.filter(a => a !== undefined).length === futures.length && | ||
right(results)) | ||
}), | ||
results | ||
), | ||
[] | ||
)); | ||
Future.all = (...futures) => all([].concat(...futures)); | ||
const Id = a => ({ | ||
ap: app => app.map(f => iff(f, a)), | ||
alt: () => Id(a), | ||
bimap: (_, r) => iff(r, a), | ||
cata: (f = Identity$1) => f.Right(a), | ||
chain: f => iff(f, a), | ||
equals: id => id.cata({ Right: b => a === b }), | ||
fold: f => iff(f, a), | ||
foldl: f => iff(f, null), | ||
foldr: f => iff(f, a), | ||
map: f => Id(iff(f, a)), | ||
of: a => Id(a), | ||
inspect: () => 'Id(' + a + ')', | ||
swap: () => Id(a) | ||
}); | ||
Id.of = a => Id(a); | ||
const Nothing = () => ({ | ||
ap: () => Nothing(), | ||
bimap: (l, _) => iff(l, null), | ||
cata: (f = Identity$1) => f.Left(null), | ||
chain: () => Nothing(), | ||
alt: f => Maybe(iff(f, null)), | ||
equals: id => id.cata({ Right: b => isNull(b) }), | ||
fold: noop, | ||
foldl: noop, | ||
foldr: noop, | ||
inspect: () => 'Nothing()', | ||
map: () => Nothing(), | ||
of: a => Maybe(a), | ||
swap: () => Just(a) | ||
}); | ||
Nothing.of = () => Nothing(); | ||
const Just = a => ({ | ||
ap: app => app.map(f => f(a)), | ||
bimap: (_, r) => iff(r, a), | ||
cata: (f = Identity$1) => f.Right(a), | ||
chain: f => iff(f, a), | ||
alt: () => Just(a), | ||
equals: id => id.cata({ Right: b => a === b }), | ||
fold: f => iff(f, a), | ||
foldl: noop, | ||
foldr: f => iff(f, a), | ||
inspect: () => 'Just(' + a + ')', | ||
map: f => Maybe(iff(f, a)), | ||
of: a => Maybe(a), | ||
swap: () => Nothing() | ||
}); | ||
Just.of = a => Just(a); | ||
const encase$1 = f => { | ||
try { | ||
return Maybe(iff(f, null)) | ||
} catch (_) { | ||
return Nothing() | ||
} | ||
}; | ||
Just.encase = encase$1; | ||
Nothing.encase = encase$1; | ||
const Maybe = a => (isNull(a) ? Nothing() : Just(a)); | ||
Maybe.of = a => Maybe(a); | ||
Maybe.encase = encase$1; | ||
const head = list => (list.length > 0 ? Maybe(list[0]) : Nothing()); | ||
const tail = list => (list.length > 0 ? Maybe(list.slice(1)) : Nothing()); | ||
const last = list => (list.length > 0 ? Maybe(list[list.length - 1]) : Nothing()); | ||
const get = (...args) => a => args.reduce((acc, b) => acc.map(c => c && c[b]), Maybe(a)); | ||
exports.Either = Either; | ||
exports.Future = Future; | ||
exports.Id = Id; | ||
exports.Identity = Identity$1; | ||
exports.Just = Just; | ||
exports.Left = Left; | ||
exports.Maybe = Maybe; | ||
exports.Nothing = Nothing; | ||
exports.Right = Right; | ||
exports.compose = compose; | ||
exports.get = get; | ||
exports.head = head; | ||
exports.id = id; | ||
exports.iff = iff; | ||
exports.isNull = isNull; | ||
exports.last = last; | ||
exports.log = log; | ||
exports.map = map; | ||
exports.noop = noop; | ||
exports.nullable = nullable; | ||
exports.tail = tail; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
}))); | ||
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((n=n||self).exaltedFuture={})}(this,function(n){var t=function(n){return n},r=function(){},u=function(n){return null==n},e={Left:t,Right:t},i=function(n,r){return void 0===n&&(n=t),n(r)},c=function(n,t){return t.cata({Right:function(t){return n===t},Left:function(t){return n===t}})},o=function n(t){return{ap:function(n){return n.isLeft?n:n.map(function(n){return i(n,t)})},alt:function(){return n(t)},bimap:function(n,r){return i(r,t)},cata:function(n){return void 0===n&&(n=Identity),n.Right(t)},chain:function(n){return i(n,t)},equals:function(n){return c(t,n)},fold:function(n){return i(n,t)},foldl:function(n){return i(n,null)},foldr:function(n){return i(n,t)},inspect:function(){return"Right("+t+")"},isLeft:!1,map:function(r){return n(i(r,t))},of:function(t){return n(t)},swap:function(){return f(t)}}};o.of=function(n){return o(n)};var f=function n(t){return{ap:function(r){return r.isLeft?r:n(t)},alt:function(){return n(t)},bimap:function(n,r){return i(n,t)},cata:function(n){return void 0===n&&(n=Identity),n.Left(t)},chain:function(){return n(t)},map:function(){return n(t)},equals:function(n){return c(t,n)},fold:function(n){return i(n,t)},foldl:function(n){return i(n,t)},foldr:function(n){return i(n,null)},inspect:function(){return"Left("+t+")"},isLeft:!0,of:function(t){return n(t)},swap:function(){return o(t)}}};f.of=function(n){return f(n)};var l=function(n,t){return[f(n),o(t)]},p=function(n){try{return o(i(n,null))}catch(n){return f(n)}};l.encase=p,o.encase=p,f.encase=p,l.error=function(n){return function(t){return u(n)?o(t):f(n)}};var s=function(n){return function(t){return h(function(r,u){try{n(function(n){return r(n)},function(n){return t(n).cata({Left:r,Right:u})})}catch(n){r(n)}})}},d=function(n){return function(t){return h(function(r,u){return h(n).cata({Left:function(n){var e=t(n);e.chain?e.chain(u):r(e)},Right:u})})}},h=function n(r){return{ap:function(){return n(r)},alt:d(r),bimap:function(){return n(r)},map:function(t){return s(r)(function(r){return n.of(t(r))})},cata:function(n){return r(n.Left,n.Right)},chain:s(r),equals:function(){return!1},fold:function(n){return r(n,n)},foldl:function(n){return r(n,t)},foldr:function(n){return r(t,n)},inspect:function(){return"Future(?)"},of:function(t){return n.of(t)},swap:function(){return n(r)}}};h.encase=function(n){try{var t=n();return h(function(n,r){return r(t)})}catch(n){return h(function(t){return t(n)})}},h.of=function(n){return h(function(t,r){return r(n)})},h.promise=function(n){return h(function(t,r){return h.promise(n.then(r,t).catch(t))})};var g=function(n){return h(function(t,r,u){return void 0===u&&(u=!1),n.reduce(function(e,i,c){return i.cata({Left:function(n){return!u&&(u=!0,t(n))},Right:function(t){return e[c]=t,!u&&e.filter(function(n){return void 0!==n}).length===n.length&&r(e)}}),e},[])})};h.all=function(){var n;return g((n=[]).concat.apply(n,[].slice.call(arguments)))};var v=function n(t){return{ap:function(n){return n.map(function(n){return i(n,t)})},alt:function(){return n(t)},bimap:function(n,r){return i(r,t)},cata:function(n){return void 0===n&&(n=e),n.Right(t)},chain:function(n){return i(n,t)},equals:function(n){return n.cata({Right:function(n){return t===n}})},fold:function(n){return i(n,t)},foldl:function(n){return i(n,null)},foldr:function(n){return i(n,t)},map:function(r){return n(i(r,t))},of:function(t){return n(t)},inspect:function(){return"Id("+t+")"},swap:function(){return n(t)}}};v.of=function(n){return v(n)};var m=function n(){return{ap:function(){return n()},bimap:function(n,t){return i(n,null)},cata:function(n){return void 0===n&&(n=e),n.Left(null)},chain:function(){return n()},alt:function(n){return L(i(n,null))},equals:function(n){return n.cata({Right:function(n){return u(n)}})},fold:r,foldl:r,foldr:r,inspect:function(){return"Nothing()"},map:function(){return n()},of:function(n){return L(n)},swap:function(){return R(a)}}};m.of=function(){return m()};var R=function n(t){return{ap:function(n){return n.map(function(n){return n(t)})},bimap:function(n,r){return i(r,t)},cata:function(n){return void 0===n&&(n=e),n.Right(t)},chain:function(n){return i(n,t)},alt:function(){return n(t)},equals:function(n){return n.cata({Right:function(n){return t===n}})},fold:function(n){return i(n,t)},foldl:r,foldr:function(n){return i(n,t)},inspect:function(){return"Just("+t+")"},map:function(n){return L(i(n,t))},of:function(n){return L(n)},swap:function(){return m()}}};R.of=function(n){return R(n)};var y=function(n){try{return L(i(n,null))}catch(n){return m()}};R.encase=y,m.encase=y;var L=function(n){return u(n)?m():R(n)};L.of=function(n){return L(n)},L.encase=y,n.Either=l,n.Future=h,n.Id=v,n.Identity=e,n.Just=R,n.Left=f,n.Maybe=L,n.Nothing=m,n.Right=o,n.compose=function(){var n=[].slice.call(arguments);return function(){var t;return n.slice(0,n.length-1).reduceRight(function(n,t){return t(n)},(t=n.slice(n.length-1))[0].apply(t,[].slice.call(arguments)))}},n.get=function(){var n=arguments;return function(t){return[].slice.call(n).reduce(function(n,t){return n.map(function(n){return n&&n[t]})},L(t))}},n.head=function(n){return n.length>0?L(n[0]):m()},n.id=t,n.iff=i,n.isNull=u,n.last=function(n){return n.length>0?L(n[n.length-1]):m()},n.log=function(){var n,t=[].slice.call(arguments);return(n=console).log.apply(n,t),t[0]},n.map=function(){var n=arguments;return function(t){return[].slice.call(n).reduceRight(function(n,t){return n.map(t)},t)}},n.noop=r,n.nullable=function(n){return u(n)?f(n):o(n)},n.tail=function(n){return n.length>0?L(n.slice(1)):m()}}); | ||
//# sourceMappingURL=index.umd.js.map |
{ | ||
"name": "exalted.future", | ||
"version": "0.0.18", | ||
"version": "0.0.19", | ||
"description": "Exalted monadic library & functional fun, fantasy-land compliant, mostly.", | ||
@@ -8,5 +8,8 @@ "repository": "https://github.com/pre63/exalted.future.git", | ||
"license": "Apache-2.0", | ||
"main": "dist/index.cjs", | ||
"module": "dist/index.esm.js", | ||
"browser": "dist/index.umd.js", | ||
"source": "src/index.js", | ||
"main": "dist/index.js", | ||
"module": "dist/index.module.js", | ||
"unpkg": "dist/index.umd.js", | ||
"umd:main": "dist/foo.umd.js", | ||
"esmodule": "dist/foo.modern.js", | ||
"type": "module", | ||
@@ -16,5 +19,5 @@ "private": false, | ||
"scripts": { | ||
"rollup": "./node_modules/.bin/rollup -c", | ||
"build": "yarn rollup", | ||
"pub": "rm -rf dist && yarn build && yarn publish", | ||
"build": "microbundle", | ||
"prebuild": "rm -rf dist", | ||
"pub": "yarn build && yarn publish", | ||
"test": "jest ./ci/*", | ||
@@ -25,9 +28,4 @@ "pretest": "yarn build", | ||
"devDependencies": { | ||
"@babel/preset-env": "^7.12.1", | ||
"@rollup/plugin-babel": "^5.2.1", | ||
"@rollup/plugin-commonjs": "^16.0.0", | ||
"@rollup/plugin-node-resolve": "^10.0.0", | ||
"jest": "^26.6.1", | ||
"rollup": "^2.32.1", | ||
"rollup-plugin-sizes": "^1.0.3" | ||
"microbundle": "^0.12.4" | ||
}, | ||
@@ -34,0 +32,0 @@ "files": [ |
@@ -9,3 +9,3 @@ ![](https://badgen.net/bundlephobia/minzip/exalted.future) | ||
A javascript and typescript monadic library & functional fun. [fantasy-land](https://github.com/fantasyland/fantasy-land) compliant, mostly. | ||
A javascript monadic library & functional fun. [fantasy-land](https://github.com/fantasyland/fantasy-land) compliant, mostly. | ||
@@ -19,3 +19,3 @@ The style of monad object is inspired by [DrBoolean](https://github.com/DrBoolean) course on [egghead.io](https://egghead.io/courses/professor-frisby-introduces-composable-functional-javascript) and his book [Mostly adequate guide to FP](https://github.com/MostlyAdequate/mostly-adequate-guide). | ||
# Natural Transformation | ||
One of the main goals of the exalted.future is to make it possible to rely on natural transformation when composing Monads. So that you can write in one continuous flow your program, agnostic of the Monad you are working with. That is why `mapReduce`, `reduce`, `fold`, and `fork` use the same language, `cata`. You can always call cata on an object, and it will compute your results. The following example attempts to illustrate that. Regardless that the fetch succeeds or fails the outcome will be the same, indifferent to calling `cata` on Maybe, Either (Left|Right), or Future. | ||
One of the main goals of the exalted.future is to make it possible to rely on natural transformation when composing Monads. So that you can write in one continuous flow your program, agnostic of the Monad you are working with. That is why `flatMap`, `mapReduce`, `reduce`, `fold`, and `fork` use the same language, `cata`. You can always call cata on an object, and it will compute your results. The following example attempts to illustrate that. Regardless that the fetch succeeds or fails the outcome will be the same, indifferent to calling `cata` on Maybe, Either (Left|Right), or Future. | ||
```javascript | ||
@@ -393,2 +393,2 @@ Future.promise(fetch('https://jsonplaceholder.typicode.com/todos/1')) | ||
[7]: https://github.com/fantasyland/fantasy-land#chain | ||
[8]: https://github.com/fantasyland/fantasy-land#apply | ||
[8]: https://github.com/fantasyland/fantasy-land#apply |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
90184
2
11
392
81
1