UNPKG

@safareli/free

Version:

Combination of a free applicative functor and free monad

2 lines 12.7 kB
!function(t,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?exports.Free=n():t.Free=n()}(this,function(){return function(t){function n(e){if(r[e])return r[e].exports;var o=r[e]={exports:{},id:e,loaded:!1};return t[e].call(o.exports,o,o.exports,n),o.loaded=!0,o.exports}var r={};return n.m=t,n.c=r,n.p="",n(0)}([function(t,n,r){"use strict";function e(){throw new TypeError("No direct use of Free constructor is Allowed")}function o(t){return this instanceof o?void(this.x=t):new o(t)}function u(t,n){return this instanceof u?(this.x=t,void(this.f=n)):new u(t,n)}function a(t,n){return this instanceof a?(this.x=t,void(this.y=n)):new a(t,n)}function i(t,n){return this instanceof i?(this.x=t,void(this.f=n)):new i(t,n)}var c=r(3),f=c.of,s=c.map,p=c.ap,l=c.chain,h=r(1);o.prototype=Object.create(e.prototype),o.prototype.constructor=o,o.prototype.cata=function(t){return t.Pure(this.x)},u.prototype=Object.create(e.prototype),u.prototype.constructor=u,u.prototype.cata=function(t){return t.Lift(this.x,this.f)},a.prototype=Object.create(e.prototype),a.prototype.constructor=a,a.prototype.cata=function(t){return t.Ap(this.x,this.y)},i.prototype=Object.create(e.prototype),i.prototype.constructor=i,i.prototype.cata=function(t){return t.Chain(this.x,this.f)},e.Pure=o,e.Ap=a,e.Lift=u,e.Chain=i;var y=function(t,n){return function(r){return t(n(r))}},d=function(t){return t};e.Pure.toString=function(){return"Free.Pure"},e.Ap.toString=function(){return"Free.Ap"},e.Lift.toString=function(){return"Free.Lift"},e.Chain.toString=function(){return"Free.Chain"},e.prototype.toString=function(){return this.cata({Pure:function(t){return"Free.Pure("+t+")"},Lift:function(t,n){return"Free.Lift("+t+",=>)"},Ap:function(t,n){return"Free.Ap("+t+","+n+")"},Chain:function(t,n){return"Free.Chain("+t+",=>)"}})},e.of=e.Pure,e.liftF=function(t){return e.Lift(t,d)},e.prototype.map=function(t){return this.cata({Pure:function(n){return e.Pure(t(n))},Lift:function(n,r){return e.Lift(n,y(t,r))},Ap:function(n,r){return e.Ap(n,s(function(n){return s(t,n)},r))},Chain:function(n,r){return e.Chain(n,function(n){return s(t,r(n))})}})},e.prototype.ap=function(t){var n=this;return this.cata({Pure:function(n){return s(function(t){return t(n)},t)},Ap:function(){return e.Ap(n,t)},Lift:function(){return e.Ap(n,t)},Chain:function(){return e.Ap(n,t)}})},e.prototype.chain=function(t){var n=this;return this.cata({Pure:function(n){return t(n)},Ap:function(){return e.Chain(n,t)},Lift:function(){return e.Chain(n,t)},Chain:function(n,r){return e.Chain(n,function(n){return l(t,r(n))})}})},e.prototype.hoist=function(t){return this.foldMap(y(e.liftF,t),e)},e.prototype.retract=function(t){return this.foldMap(d,t)},e.prototype.foldMap=function(t,n){return n.chainRec(function(r,e,o){return o.cata({Pure:function(t){return s(e,f(n,t))},Lift:function(n,r){return s(y(e,r),t(n))},Ap:function(r,o){return s(e,p(o.foldMap(t,n),r.foldMap(t,n)))},Chain:function(e,o){return s(y(r,o),e.foldMap(t,n))}})},this)},e.prototype.graft=function(t){return this.foldMap(t,e)};var v=function(t){return{done:!1,value:t}},m=function(t){return{done:!0,value:t}};e.chainRec=function(t,n){return l(function(n){var r=n.done,o=n.value;return r?e.of(o):e.chainRec(t,o)},t(v,m,n))},h([e,e.prototype]),t.exports=e},function(t,n,r){"use strict";var e=r(2),o=function(t){return Object.keys(e).forEach(function(n){"function"==typeof t[n]&&(t[e[n]]=t[n])})},u=function(t){return t.forEach(o)};t.exports=u},function(t,n){!function(){"use strict";var n={equals:"fantasy-land/equals",concat:"fantasy-land/concat",empty:"fantasy-land/empty",map:"fantasy-land/map",ap:"fantasy-land/ap",of:"fantasy-land/of",reduce:"fantasy-land/reduce",traverse:"fantasy-land/traverse",chain:"fantasy-land/chain",chainRec:"fantasy-land/chainRec",extend:"fantasy-land/extend",extract:"fantasy-land/extract",bimap:"fantasy-land/bimap",promap:"fantasy-land/promap"};"object"==typeof t&&"object"==typeof t.exports?t.exports=n:self.FantasyLand=n}()},function(t,n,r){var e,o,u;!function(a){"use strict";"object"==typeof t&&"object"==typeof t.exports?t.exports=a():null!=r(4)?(o=[],e=a,u="function"==typeof e?e.apply(n,o):e,!(void 0!==u&&(t.exports=u))):self.sanctuaryTypeClasses=a()}(function(){"use strict";var t=function(t,n){return Object.prototype.hasOwnProperty.call(n,t)},n=function(t){return t},r=function(t){return function(n){return[t].concat(n)}},e=function kt(t){return null!=t&&"String"===kt(t["@@type"])?t["@@type"]:Object.prototype.toString.call(t).slice("[object ".length,-"]".length)},o=function(t,n,r){return{"@@type":"sanctuary-type-classes/TypeClass",name:t,test:function(t){return n.every(function(n){return n.test(t)})&&r(t)}}},u="Constructor",a="Value",i=function(n,r,e){for(var o=e,u=0;u<r.length;u+=1){var a=r[u];if(null==o||!n&&!t(a,o))return null;o=o[a]}return"function"==typeof o?o:null},c=function(t,n){return i(!0,t,n)},f=function(t){return i(!1,t,yt)},s=function(t,n,r){var i=function(t){return function(n){for(var o="fantasy-land/"+t,a=r[t],i=0;i<a.length;i+=1)if(a[i]===u){var s=c(["constructor",o],n)||f([e(n),o]);if(s)return s}else{var p=c([o],n)||f([e(n),"prototype",o]);if(p)return p.bind(n)}}},s="sanctuary-type-classes/"+t,p=Object.keys(r),l=o(s,n,function(t){return p.every(function(n){return null!=i(n)(t)})});return l.functions={},p.forEach(function(t){var n="fantasy-land/"+t;r[t].indexOf(u)>=0&&(l.functions[t]=function(t){var r=/function (\w*)/.exec(t);return r&&f([r[1],n])||t[n]})}),l.methods={},p.forEach(function(t){r[t].indexOf(a)>=0&&(l.methods[t]=i(t))}),l},p=s("Setoid",[],{equals:[a]}),l=s("Semigroup",[],{concat:[a]}),h=s("Monoid",[l],{empty:[a,u]}),y=s("Functor",[],{map:[a]}),d=s("Bifunctor",[y],{bimap:[a]}),v=s("Profunctor",[y],{promap:[a]}),m=s("Apply",[y],{ap:[a]}),g=s("Applicative",[m],{of:[a,u]}),b=s("Chain",[m],{chain:[a]}),x=s("ChainRec",[b],{chainRec:[a,u]}),O=s("Monad",[g,b],{}),j=s("Foldable",[],{reduce:[a]}),S=s("Traversable",[y,j],{traverse:[a]}),C=s("Extend",[],{extend:[a]}),A=s("Comonad",[y,C],{extract:[a]}),F=function(){return"null"},q=function(t){return!0},P=function(){return"undefined"},w=function(t){return!0},L=function(){return"object"==typeof this?"new Boolean("+dt(this.valueOf())+")":this.toString()},M=function(t){return typeof t==typeof this&&t.valueOf()===this.valueOf()},N=function(){return"object"==typeof this?"new Number("+dt(this.valueOf())+")":1/this===-(1/0)?"-0":this.toString(10)},R=function(t){return"object"==typeof t?"object"==typeof this&&vt(t.valueOf(),this.valueOf()):isNaN(t)&&isNaN(this)||t===this&&1/t===1/this},E=function(){var t=isNaN(this.valueOf())?NaN:this.toISOString();return"new Date("+dt(t)+")"},k=function(t){return vt(t.valueOf(),this.valueOf())},T=function(t){return t.source===this.source&&t.global===this.global&&t.ignoreCase===this.ignoreCase&&t.multiline===this.multiline&&t.sticky===this.sticky&&t.unicode===this.unicode},B=function(){return""},D=function(){return"object"==typeof this?"new String("+dt(this.valueOf())+")":'"'+this.replace(/\\/g,"\\\\").replace(/[\b]/g,"\\b").replace(/\f/g,"\\f").replace(/\n/g,"\\n").replace(/\r/g,"\\r").replace(/\t/g,"\\t").replace(/\v/g,"\\v").replace(/\0/g,"\\0").replace(/"/g,'\\"')+'"'},I=function(t){return typeof t==typeof this&&t.valueOf()===this.valueOf()},U=function(t){return this+t},V=function(){return[]},$=function(t){return[t]},z=function(t,n){for(var r=function(t){return{value:t,done:!1}},e=function(t){return{value:t,done:!0}},o=[n],u=[];o.length>0;){for(var a=t(r,e,o.shift()),i=[],c=0;c<a.length;c+=1)(a[c].done?u:i).push(a[c].value);Array.prototype.unshift.apply(o,i)}return u},G=function(){for(var t=this.map(dt),n=Object.keys(this).sort(),r=0;r<n.length;r+=1){var e=n[r];/^\d+$/.test(e)||t.push(dt(e)+": "+dt(this[e]))}return"["+t.join(", ")+"]"},H=function(t){if(t.length!==this.length)return!1;for(var n=0;n<this.length;n+=1)if(!vt(t[n],this[n]))return!1;return!0},J=function(t){return this.concat(t)},K=function(t){return this.map(function(n){return t(n)})},Q=function(t){for(var n=[],r=0;r<t.length;r+=1)for(var e=0;e<this.length;e+=1)n.push(t[r](this[e]));return n},W=function(t){var n=[];return this.forEach(function(r){Array.prototype.push.apply(n,t(r))}),n},X=function(t,n){return this.reduce(function(n,r){return t(n,r)},n)},Y=function(t,n){for(var e=n([]),o=this.length-1;o>=0;o-=1)e=jt(bt(r,t(this[o])),e);return e},Z=function(t){return[t(this)]},_=function(){var t=Array.prototype.map.call(this,dt).join(", ");return"(function () { return arguments; }("+t+"))"},tt=function(t){return H.call(this,t)},nt=function(){return"new "+this.name+"("+dt(this.message)+")"},rt=function(t){return vt(t.name,this.name)&&vt(t.message,this.message)},et=function(){return{}},ot=function(){for(var t=[],n=Object.keys(this).sort(),r=0;r<n.length;r+=1){var e=n[r];t.push(dt(e)+": "+dt(this[e]))}return"{"+t.join(", ")+"}"},ut=function(t){var n=this,r=Object.keys(this).sort();return vt(Object.keys(t).sort(),r)&&r.every(function(r){return vt(t[r],n[r])})},at=function(t){var n={};for(var r in this)n[r]=this[r];for(r in t)n[r]=t[r];return n},it=function(t){var n={};for(var r in this)n[r]=t(this[r]);return n},ct=function(t,n){var r=n;for(var e in this)r=t(r,this[e]);return r},ft=function(t){return function(n){return t}},st=function(t){var n=this;return function(r){return t(n(r))}},pt=function(t,n){var r=this;return function(e){return n(r(t(e)))}},lt=function(t){var n=this;return function(r){return t(r)(n(r))}},ht=function(t){var n=this;return function(r){return t(n(r))(r)}},yt={Null:{prototype:{toString:F,"fantasy-land/equals":q}},Undefined:{prototype:{toString:P,"fantasy-land/equals":w}},Boolean:{prototype:{toString:L,"fantasy-land/equals":M}},Number:{prototype:{toString:N,"fantasy-land/equals":R}},Date:{prototype:{toString:E,"fantasy-land/equals":k}},RegExp:{prototype:{"fantasy-land/equals":T}},String:{"fantasy-land/empty":B,prototype:{toString:D,"fantasy-land/equals":I,"fantasy-land/concat":U}},Array:{"fantasy-land/empty":V,"fantasy-land/of":$,"fantasy-land/chainRec":z,prototype:{toString:G,"fantasy-land/equals":H,"fantasy-land/concat":J,"fantasy-land/map":K,"fantasy-land/ap":Q,"fantasy-land/chain":W,"fantasy-land/reduce":X,"fantasy-land/traverse":Y,"fantasy-land/extend":Z}},Arguments:{prototype:{toString:_,"fantasy-land/equals":tt}},Error:{prototype:{toString:nt,"fantasy-land/equals":rt}},Object:{"fantasy-land/empty":et,prototype:{toString:ot,"fantasy-land/equals":ut,"fantasy-land/concat":at,"fantasy-land/map":it,"fantasy-land/reduce":ct}},Function:{"fantasy-land/of":ft,prototype:{"fantasy-land/map":st,"fantasy-land/promap":pt,"fantasy-land/ap":lt,"fantasy-land/chain":ht}}},dt=function(){var t=[],n=function(n,r){t.push(r);try{return n.call(r)}finally{t.pop()}};return function(r){if(t.indexOf(r)>=0)return"<Circular>";var o=e(r);if("Object"===o){var u;try{u=n(r.toString,r)}catch(a){}if(null!=u&&"[object Object]"!==u)return u}return n(f([o,"prototype","toString"])||r.toString,r)}}(),vt=function(){var t=p.methods.equals,n=[],r=function(r,e){n.push(r,e);try{return p.test(r)&&p.test(e)&&t(r)(e)&&t(e)(r)}finally{n.splice(-2,2)}};return function(t,o){return Object(t)===Object(o)||e(t)===e(o)&&n.indexOf(t)<0&&n.indexOf(o)<0&&r(t,o)}}(),mt=function(t,n){return l.methods.concat(t)(n)},gt=function(t){return h.functions.empty(t)()},bt=function(t,n){return y.methods.map(n)(t)},xt=function(t,n,r){return d.methods.bimap(r)(t,n)},Ot=function(t,n,r){return v.methods.promap(r)(t,n)},jt=function(t,n){return m.methods.ap(n)(t)},St=function(t,n,r){return jt(bt(t,n),r)},Ct=function(t,n,r,e){return jt(jt(bt(t,n),r),e)},At=function(t,n){return g.functions.of(t)(n)},Ft=function Tt(t,Tt){return b.methods.chain(Tt)(t)},qt=function(t,n,r){return x.functions.chainRec(t)(n,r)},Pt=function(t,n){var r=n.constructor;return Lt(function(n,e){return t(e)?mt(n,At(r,e)):n},gt(r),n)},wt=function(t,n){var r=n.constructor,e=gt(r);return Ft(function(n){return t(n)?At(r,n):e},n)},Lt=function(t,n,r){return j.methods.reduce(r)(t,n)},Mt=function(t,n,r){return S.methods.traverse(r)(n,t)},Nt=function(t,r){return Mt(t,n,r)},Rt=function Bt(t,Bt){return C.methods.extend(Bt)(t)},Et=function(t){return A.methods.extract(t)()};return{TypeClass:o,Setoid:p,Semigroup:l,Monoid:h,Functor:y,Bifunctor:d,Profunctor:v,Apply:m,Applicative:g,Chain:b,ChainRec:x,Monad:O,Foldable:j,Traversable:S,Extend:C,Comonad:A,toString:dt,equals:vt,concat:mt,empty:gt,map:bt,bimap:xt,promap:Ot,ap:jt,lift2:St,lift3:Ct,of:At,chain:Ft,chainRec:qt,filter:Pt,filterM:wt,reduce:Lt,traverse:Mt,sequence:Nt,extend:Rt,extract:Et}})},function(t,n){(function(n){t.exports=n}).call(n,{})}])}); //# sourceMappingURL=free.umd.min.js.map