UNPKG

typescript-monads

Version:
1 lines 34 kB
(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?factory(exports,require("rxjs"),require("rxjs/operators")):typeof define==="function"&&define.amd?define(["exports","rxjs","rxjs/operators"],factory):(global=typeof globalThis!=="undefined"?globalThis:global||self,factory(global.monads={},global.rxjs,global["rxjs/operators"]))})(this,(function(exports,rxjs,operators){"use strict";var __values$3=undefined&&undefined.__values||function(o){var s=typeof Symbol==="function"&&Symbol.iterator,m=s&&o[s],i=0;if(m)return m.call(o);if(o&&typeof o.length==="number")return{next:function(){if(o&&i>=o.length)o=void 0;return{value:o&&o[i++],done:!o}}};throw new TypeError(s?"Object is not iterable.":"Symbol.iterator is not defined.")};function maybe(value){return new Maybe(value)}function none(){return Maybe.none()}function some(value){return maybe(value)}function maybeProps(path){var segments=path.split(".");return function(obj){var e_1,_a;var current=obj;try{for(var segments_1=__values$3(segments),segments_1_1=segments_1.next();!segments_1_1.done;segments_1_1=segments_1.next()){var segment=segments_1_1.value;if(current===null||current===undefined||!(segment in current)){return none()}current=current[segment]}}catch(e_1_1){e_1={error:e_1_1}}finally{try{if(segments_1_1&&!segments_1_1.done&&(_a=segments_1.return))_a.call(segments_1)}finally{if(e_1)throw e_1.error}}return maybe(current)}}var __extends=undefined&&undefined.__extends||function(){var extendStatics=function(d,b){extendStatics=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(d,b){d.__proto__=b}||function(d,b){for(var p in b)if(Object.prototype.hasOwnProperty.call(b,p))d[p]=b[p]};return extendStatics(d,b)};return function(d,b){if(typeof b!=="function"&&b!==null)throw new TypeError("Class extends value "+String(b)+" is not a constructor or null");extendStatics(d,b);function __(){this.constructor=d}d.prototype=b===null?Object.create(b):(__.prototype=b.prototype,new __)}}();var __values$2=undefined&&undefined.__values||function(o){var s=typeof Symbol==="function"&&Symbol.iterator,m=s&&o[s],i=0;if(m)return m.call(o);if(o&&typeof o.length==="number")return{next:function(){if(o&&i>=o.length)o=void 0;return{value:o&&o[i++],done:!o}}};throw new TypeError(s?"Object is not iterable.":"Symbol.iterator is not defined.")};var Result=function(){function Result(){}Result.ok=function(value){return new OkResult(value)};Result.fail=function(value){return new FailResult(value)};Result.fromPromise=function(promise){return promise.then((function(value){return new OkResult(value)})).catch((function(error){return new FailResult(error)}))};Result.fromObservable=function(observable,defaultError){return import("rxjs").then((function(_a){var firstValueFrom=_a.firstValueFrom,take=_a.take,map=_a.map,catchError=_a.catchError;return firstValueFrom(observable.pipe(take(1),map((function(value){return new OkResult(value)})),catchError((function(error){return[new FailResult(error)]})))).then((function(result){return result}),(function(){return new FailResult(defaultError)}))}))};Result.sequence=function(results){var e_1,_a;if(results.length===0){return new OkResult([])}var values=[];try{for(var results_1=__values$2(results),results_1_1=results_1.next();!results_1_1.done;results_1_1=results_1.next()){var r=results_1_1.value;if(r.isFail()){return new FailResult(r.unwrapFail())}values.push(r.unwrap())}}catch(e_1_1){e_1={error:e_1_1}}finally{try{if(results_1_1&&!results_1_1.done&&(_a=results_1.return))_a.call(results_1)}finally{if(e_1)throw e_1.error}}return new OkResult(values)};Result.all=function(results){return Result.sequence(results)};return Result}();var OkResult=function(_super){__extends(OkResult,_super);function OkResult(successValue){var _this=_super.call(this)||this;_this.successValue=successValue;return _this}OkResult.prototype.isOk=function(){return true};OkResult.prototype.isFail=function(){return false};OkResult.prototype.unwrap=function(){return this.successValue};OkResult.prototype.unwrapOr=function(){return this.unwrap()};OkResult.prototype.unwrapFail=function(){throw new ReferenceError("Cannot unwrap a success as a failure")};OkResult.prototype.maybeOk=function(){return maybe(this.successValue)};OkResult.prototype.maybeFail=function(){return none()};OkResult.prototype.match=function(fn){return fn.ok(this.successValue)};OkResult.prototype.map=function(fn){return Result.ok(fn(this.successValue))};OkResult.prototype.mapFail=function(){return Result.ok(this.successValue)};OkResult.prototype.flatMap=function(fn){return fn(this.successValue)};OkResult.prototype.flatMapMaybe=function(fn,err){return fn(this.successValue).match({some:function(val){return Result.ok(val)},none:function(){return Result.fail(err)}})};OkResult.prototype.toFailWhenOk=function(fn){return Result.fail(fn(this.successValue))};OkResult.prototype.toFailWhenOkFrom=function(val){return Result.fail(val)};OkResult.prototype.tap=function(val){typeof val.ok==="function"&&val.ok(this.successValue)};OkResult.prototype.tapOk=function(fn){fn(this.successValue)};OkResult.prototype.tapFail=function(){};OkResult.prototype.tapFailThru=function(){return this};OkResult.prototype.tapOkThru=function(fn){this.tapOk(fn);return this};OkResult.prototype.tapThru=function(val){this.tap(val);return this};OkResult.prototype.recover=function(){return this};OkResult.prototype.recoverWith=function(){return this};OkResult.prototype.orElse=function(){return this};OkResult.prototype.swap=function(){return Result.fail(this.successValue)};OkResult.prototype.zipWith=function(other,fn){var _this=this;return other.flatMap((function(otherValue){return Result.ok(fn(_this.successValue,otherValue))}))};OkResult.prototype.flatMapPromise=function(fn){return fn(this.successValue).then((function(value){return Result.ok(value)})).catch((function(error){return Result.fail(error)}))};OkResult.prototype.flatMapObservable=function(fn,defaultError){var _this=this;return import("rxjs").then((function(_a){var firstValueFrom=_a.firstValueFrom,take=_a.take,map=_a.map,catchError=_a.catchError;return firstValueFrom(fn(_this.successValue).pipe(take(1),map((function(value){return Result.ok(value)})),catchError((function(error){return[Result.fail(error)]})))).then((function(result){return result}),(function(){return Result.fail(defaultError)}))}))};return OkResult}(Result);var FailResult=function(_super){__extends(FailResult,_super);function FailResult(failureValue){var _this=_super.call(this)||this;_this.failureValue=failureValue;return _this}FailResult.prototype.isOk=function(){return false};FailResult.prototype.isFail=function(){return true};FailResult.prototype.unwrap=function(){throw new Error("Cannot unwrap a failure")};FailResult.prototype.unwrapOr=function(opt){return opt};FailResult.prototype.unwrapFail=function(){return this.failureValue};FailResult.prototype.maybeOk=function(){return none()};FailResult.prototype.maybeFail=function(){return maybe(this.failureValue)};FailResult.prototype.match=function(fn){return fn.fail(this.failureValue)};FailResult.prototype.mapFail=function(fn){return Result.fail(fn(this.failureValue))};FailResult.prototype.map=function(){return Result.fail(this.failureValue)};FailResult.prototype.flatMap=function(){return Result.fail(this.failureValue)};FailResult.prototype.flatMapMaybe=function(){return Result.fail(this.failureValue)};FailResult.prototype.toFailWhenOk=function(){return this};FailResult.prototype.toFailWhenOkFrom=function(val){return Result.fail(val)};FailResult.prototype.tap=function(val){typeof val.fail==="function"&&val.fail(this.failureValue)};FailResult.prototype.tapOk=function(){};FailResult.prototype.tapFail=function(fn){fn(this.failureValue)};FailResult.prototype.tapFailThru=function(fn){this.tapFail(fn);return this};FailResult.prototype.tapOkThru=function(){return this};FailResult.prototype.tapThru=function(val){this.tap(val);return this};FailResult.prototype.recover=function(fn){return Result.ok(fn(this.failureValue))};FailResult.prototype.recoverWith=function(fn){return fn(this.failureValue)};FailResult.prototype.orElse=function(fallback){return fallback};FailResult.prototype.swap=function(){return Result.ok(this.failureValue)};FailResult.prototype.zipWith=function(){return Result.fail(this.failureValue)};FailResult.prototype.flatMapPromise=function(){return Promise.resolve(Result.fail(this.failureValue))};FailResult.prototype.flatMapObservable=function(){return Promise.resolve(Result.fail(this.failureValue))};return FailResult}(Result);var __values$1=undefined&&undefined.__values||function(o){var s=typeof Symbol==="function"&&Symbol.iterator,m=s&&o[s],i=0;if(m)return m.call(o);if(o&&typeof o.length==="number")return{next:function(){if(o&&i>=o.length)o=void 0;return{value:o&&o[i++],done:!o}}};throw new TypeError(s?"Object is not iterable.":"Symbol.iterator is not defined.")};var Maybe=function(){function Maybe(value){this.value=value}Maybe.prototype.of=function(value){return new Maybe(value)};Maybe.none=function(){return new Maybe};Maybe.some=function(value){return new Maybe(value)};Maybe.fromPromise=function(promise){return promise.then((function(value){return new Maybe(value)})).catch((function(){return new Maybe}))};Maybe.fromObservable=function(observable){return import("rxjs").then((function(_a){var firstValueFrom=_a.firstValueFrom,EMPTY=_a.EMPTY,take=_a.take,map=_a.map,catchError=_a.catchError;return firstValueFrom(observable.pipe(take(1),map((function(value){return new Maybe(value)})),catchError((function(){return EMPTY})))).then((function(maybeValue){return maybeValue}),(function(){return new Maybe}))}))};Maybe.sequence=function(maybes){var e_1,_a;if(maybes.length===0){return new Maybe([])}var values=[];try{for(var maybes_1=__values$1(maybes),maybes_1_1=maybes_1.next();!maybes_1_1.done;maybes_1_1=maybes_1.next()){var m=maybes_1_1.value;if(m.isNone()){return new Maybe}values.push(m.valueOrThrow())}}catch(e_1_1){e_1={error:e_1_1}}finally{try{if(maybes_1_1&&!maybes_1_1.done&&(_a=maybes_1.return))_a.call(maybes_1)}finally{if(e_1)throw e_1.error}}return new Maybe(values)};Maybe.prototype.isSome=function(){return!this.isNone()};Maybe.prototype.isNone=function(){return this.value===null||this.value===undefined};Maybe.prototype.valueOr=function(value){return this.isSome()?this.value:value};Maybe.prototype.valueOrUndefined=function(){return this.isSome()?this.value:undefined};Maybe.prototype.valueOrNull=function(){return this.isSome()?this.value:null};Maybe.prototype.valueOrCompute=function(fn){return this.isSome()?this.value:fn()};Maybe.prototype.valueOrThrow=function(msg){return this.isNone()?function(){throw new Error(msg)}():this.value};Maybe.prototype.valueOrThrowErr=function(err){return this.isNone()?function(){return err instanceof Error?function(){throw err}():function(){throw new Error}()}():this.value};Maybe.prototype.tap=function(obj){this.isNone()?typeof obj.none==="function"&&obj.none():typeof obj.some==="function"&&obj.some(this.value)};Maybe.prototype.tapNone=function(fn){this.isNone()&&fn()};Maybe.prototype.tapSome=function(fn){this.isSome()&&fn(this.value)};Maybe.prototype.tapThru=function(val){this.tap(val);return this};Maybe.prototype.tapThruNone=function(fn){this.tapNone(fn);return this};Maybe.prototype.tapThruSome=function(fn){this.tapSome(fn);return this};Maybe.prototype.match=function(pattern){return this.isNone()?pattern.none():pattern.some(this.value)};Maybe.prototype.toArray=function(){return this.isNone()?[]:Array.isArray(this.value)?this.value:[this.value]};Maybe.prototype.map=function(fn){return this.isSome()?new Maybe(fn(this.value)):new Maybe};Maybe.prototype.mapTo=function(t){return this.isSome()?new Maybe(t):new Maybe};Maybe.prototype.flatMap=function(fn){return this.isNone()?new Maybe:fn(this.value)};Maybe.prototype.flatMapAuto=function(fn){return this.isNone()?new Maybe:new Maybe(fn(this.value))};Maybe.prototype.project=function(fn){return this.flatMapAuto(fn)};Maybe.prototype.filter=function(fn){return this.isNone()?new Maybe:fn(this.value)?new Maybe(this.value):new Maybe};Maybe.prototype.apply=function(maybeFn){return this.flatMap((function(v){return maybeFn.flatMapAuto((function(f){return f(v)}))}))};Maybe.prototype.toResult=function(error){return this.map((function(b){return new OkResult(b)})).valueOr(new FailResult(error))};Maybe.prototype.flatMapPromise=function(fn){if(this.isNone()){return Promise.resolve(new Maybe)}return fn(this.value).then((function(value){return new Maybe(value)})).catch((function(){return new Maybe}))};Maybe.prototype.flatMapObservable=function(fn){var _this=this;if(this.isNone()){return Promise.resolve(new Maybe)}return import("rxjs").then((function(_a){var firstValueFrom=_a.firstValueFrom,EMPTY=_a.EMPTY,take=_a.take,map=_a.map,catchError=_a.catchError;return firstValueFrom(fn(_this.value).pipe(take(1),map((function(value){return new Maybe(value)})),catchError((function(){return EMPTY})))).then((function(maybeValue){return maybeValue}),(function(){return new Maybe}))}))};Maybe.prototype.flatMapMany=function(fn){if(this.isNone()){return Promise.resolve(new Maybe)}return Promise.all(fn(this.value)).then((function(values){return new Maybe(values)})).catch((function(){return new Maybe}))};Maybe.prototype.zipWith=function(other,fn){return this.flatMap((function(a){return other.map((function(b){return fn(a,b)}))}))};return Maybe}();function maybeToPromise(catchResponse,handleNoneAsResolved,fallbackValue){if(handleNoneAsResolved===void 0){handleNoneAsResolved=false}return function maybeToPromiseConverter(maybe){if(maybe.isSome()){return Promise.resolve(maybe.valueOrThrow())}return handleNoneAsResolved?Promise.resolve(fallbackValue):Promise.reject(catchResponse)}}function maybeToObservable(m){return m.isNone()?rxjs.EMPTY:rxjs.of(m.valueOrThrow())}function promiseToMaybe(promise){return promise.then((function(value){return maybe(value)})).catch((function(){return maybe()}))}function observableToMaybe(observable){return rxjs.firstValueFrom(observable.pipe(operators.take(1),operators.map((function(value){return maybe(value)})),operators.catchError((function(){return rxjs.EMPTY})))).then((function(maybeValue){return maybeValue}),(function(){return maybe()}))}function tryPromiseToMaybe(fn){return fn().then((function(result){return new Maybe(result)})).catch((function(){return new Maybe}))}var __read$2=undefined&&undefined.__read||function(o,n){var m=typeof Symbol==="function"&&o[Symbol.iterator];if(!m)return o;var i=m.call(o),r,ar=[],e;try{while((n===void 0||n-- >0)&&!(r=i.next()).done)ar.push(r.value)}catch(error){e={error:error}}finally{try{if(r&&!r.done&&(m=i["return"]))m.call(i)}finally{if(e)throw e.error}}return ar};var __spreadArray$2=undefined&&undefined.__spreadArray||function(to,from,pack){if(pack||arguments.length===2)for(var i=0,l=from.length,ar;i<l;i++){if(ar||!(i in from)){if(!ar)ar=Array.prototype.slice.call(from,0,i);ar[i]=from[i]}}return to.concat(ar||Array.prototype.slice.call(from))};var Reader=function(){function Reader(fn){this.fn=fn}Reader.of=function(value){return new Reader((function(){return value}))};Reader.ask=function(){return new Reader((function(config){return config}))};Reader.asks=function(accessor){return new Reader(accessor)};Reader.sequence=function(readers){return new Reader((function(config){return readers.map((function(reader){return reader.run(config)}))}))};Reader.traverse=function(readers,reducer,initialValue){return new Reader((function(config){return readers.reduce((function(acc,reader,index){return reducer(acc,reader.run(config),index)}),initialValue)}))};Reader.combine=function(readers,fn){return new Reader((function(config){var values=readers.map((function(reader){return reader.run(config)}));return fn.apply(void 0,__spreadArray$2([],__read$2(values),false))}))};Reader.prototype.of=function(fn){return new Reader(fn)};Reader.prototype.map=function(fn){var _this=this;return new Reader((function(c){return fn(_this.run(c))}))};Reader.prototype.mapTo=function(val){return this.map((function(){return val}))};Reader.prototype.flatMap=function(fn){var _this=this;return new Reader((function(c){return fn(_this.run(c)).run(c)}))};Reader.prototype.run=function(config){return this.fn(config)};Reader.prototype.local=function(fn){var _this=this;return new Reader((function(c){return _this.run(fn(c))}))};Reader.prototype.zipWith=function(other,fn){var _this=this;return new Reader((function(c){return fn(_this.run(c),other.run(c))}))};Reader.prototype.tap=function(fn){var _this=this;return new Reader((function(c){var result=_this.run(c);fn(result);return result}))};Reader.prototype.andThen=function(other){var _this=this;return new Reader((function(c){_this.run(c);return other.run(c)}))};Reader.prototype.andFinally=function(other){var _this=this;return new Reader((function(c){var result=_this.run(c);other.run(c);return result}))};Reader.prototype.withEnv=function(fn){var _this=this;return new Reader((function(c){return fn(c,_this.run(c))}))};Reader.prototype.filter=function(predicate,defaultValue){var _this=this;return new Reader((function(c){var result=_this.run(c);return predicate(result)?result:defaultValue}))};Reader.prototype.fanout=function(){var _this=this;var fns=[];for(var _i=0;_i<arguments.length;_i++){fns[_i]=arguments[_i]}return new Reader((function(c){var value=_this.run(c);return fns.map((function(fn){return fn(value)}))}))};Reader.prototype.toPromise=function(){var _this=this;return function(env){return Promise.resolve(_this.run(env))}};Reader.prototype.memoize=function(cacheKeyFn){var _this=this;var cache=new Map;return new Reader((function(c){var key=cacheKeyFn?cacheKeyFn(c):c;if(cache.has(key)){return cache.get(key)}var result=_this.run(c);cache.set(key,result);return result}))};return Reader}();function reader(fn){return new Reader(fn)}function readerOf(value){return Reader.of(value)}function ask(){return Reader.ask()}function asks(accessor){return Reader.asks(accessor)}function sequence(readers){return Reader.sequence(readers)}function traverse(readers,reducer,initialValue){return Reader.traverse(readers,reducer,initialValue)}function combine(readers,fn){return Reader.combine(readers,fn)}var Either=function(){function Either(left,right){this.left=left;this.right=right;if(this.neitherExist()){throw new TypeError("Either requires a left or a right")}if(this.bothExist()){throw new TypeError("Either cannot have both a left and a right")}}Either.exists=function(value){return typeof value!=="undefined"&&value!==null};Either.prototype.bothExist=function(){return this.isLeft()&&this.isRight()};Either.prototype.neitherExist=function(){return!this.isLeft()&&!this.isRight()};Either.prototype.isLeft=function(){return Either.exists(this.left)};Either.prototype.isRight=function(){return Either.exists(this.right)};Either.prototype.match=function(pattern){return this.isRight()?pattern.right(this.right):pattern.left(this.left)};Either.prototype.tap=function(pattern){this.isRight()?typeof pattern.right==="function"&&pattern.right(this.right):typeof pattern.left==="function"&&pattern.left(this.left)};Either.prototype.map=function(fn){return this.isRight()?new Either(undefined,fn(this.right)):new Either(this.left)};Either.prototype.flatMap=function(fn){return this.isRight()?fn(this.right):new Either(this.left)};return Either}();function either(left,right){return new Either(left,right)}function ok(value){return Result.ok(value)}function fail(value){return Result.fail(value)}function result(predicate,okValue,failValue){return predicate()?ok(okValue):fail(failValue)}function resultToPromise(result){return result.isOk()?Promise.resolve(result.unwrap()):Promise.reject(result.unwrapFail())}function catchResult(fn,errFn){try{return ok(fn())}catch(err){return fail(errFn?errFn(err):err)}}function unwrapResultAsObservable(){return function unwrapResultAsObservable1(source){return source.pipe(rxjs.map((function(result){if(result.isOk())return result.unwrap();throw result.unwrapFail()})))}}function resultToObservable(result){if(result.isOk()){return rxjs.of(result.unwrap())}else{return rxjs.throwError((function(){return result.unwrapFail()}))}}function promiseToResult(promise){return promise.then((function(value){return ok(value)})).catch((function(error){return fail(error)}))}function tryPromiseToResult(promise,errorMapper){return promise.then((function(value){return ok(value)})).catch((function(error){return fail(errorMapper(error))}))}function observableToResult(observable,defaultError){return rxjs.firstValueFrom(observable.pipe(operators.take(1),operators.map((function(value){return ok(value)})),operators.catchError((function(error){return[fail(error)]})))).then((function(result){return result}),(function(){return fail(defaultError)}))}var Monad=function(){function Monad(){}return Monad}();var __assign=undefined&&undefined.__assign||function(){__assign=Object.assign||function(t){for(var s,i=1,n=arguments.length;i<n;i++){s=arguments[i];for(var p in s)if(Object.prototype.hasOwnProperty.call(s,p))t[p]=s[p]}return t};return __assign.apply(this,arguments)};var __generator=undefined&&undefined.__generator||function(thisArg,body){var _={label:0,sent:function(){if(t[0]&1)throw t[1];return t[1]},trys:[],ops:[]},f,y,t,g=Object.create((typeof Iterator==="function"?Iterator:Object).prototype);return g.next=verb(0),g["throw"]=verb(1),g["return"]=verb(2),typeof Symbol==="function"&&(g[Symbol.iterator]=function(){return this}),g;function verb(n){return function(v){return step([n,v])}}function step(op){if(f)throw new TypeError("Generator is already executing.");while(g&&(g=0,op[0]&&(_=0)),_)try{if(f=1,y&&(t=op[0]&2?y["return"]:op[0]?y["throw"]||((t=y["return"])&&t.call(y),0):y.next)&&!(t=t.call(y,op[1])).done)return t;if(y=0,t)op=[op[0]&2,t.value];switch(op[0]){case 0:case 1:t=op;break;case 4:_.label++;return{value:op[1],done:false};case 5:_.label++;y=op[1];op=[0];continue;case 7:op=_.ops.pop();_.trys.pop();continue;default:if(!(t=_.trys,t=t.length>0&&t[t.length-1])&&(op[0]===6||op[0]===2)){_=0;continue}if(op[0]===3&&(!t||op[1]>t[0]&&op[1]<t[3])){_.label=op[1];break}if(op[0]===6&&_.label<t[1]){_.label=t[1];t=op;break}if(t&&_.label<t[2]){_.label=t[2];_.ops.push(op);break}if(t[2])_.ops.pop();_.trys.pop();continue}op=body.call(thisArg,_)}catch(e){op=[6,e];y=0}finally{f=t=0}if(op[0]&5)throw op[1];return{value:op[0]?op[1]:void 0,done:true}}};var __read$1=undefined&&undefined.__read||function(o,n){var m=typeof Symbol==="function"&&o[Symbol.iterator];if(!m)return o;var i=m.call(o),r,ar=[],e;try{while((n===void 0||n-- >0)&&!(r=i.next()).done)ar.push(r.value)}catch(error){e={error:error}}finally{try{if(r&&!r.done&&(m=i["return"]))m.call(i)}finally{if(e)throw e.error}}return ar};var __spreadArray$1=undefined&&undefined.__spreadArray||function(to,from,pack){if(pack||arguments.length===2)for(var i=0,l=from.length,ar;i<l;i++){if(ar||!(i in from)){if(!ar)ar=Array.prototype.slice.call(from,0,i);ar[i]=from[i]}}return to.concat(ar||Array.prototype.slice.call(from))};var __values=undefined&&undefined.__values||function(o){var s=typeof Symbol==="function"&&Symbol.iterator,m=s&&o[s],i=0;if(m)return m.call(o);if(o&&typeof o.length==="number")return{next:function(){if(o&&i>=o.length)o=void 0;return{value:o&&o[i++],done:!o}}};throw new TypeError(s?"Object is not iterable.":"Symbol.iterator is not defined.")};var List=function(){function List(generator,length){this.length=length;this[Symbol.iterator]=generator}List.prototype.generator=function(){return this[Symbol.iterator]()};List.flattenArgs=function(args){return args.reduce((function(acc,curr){return Array.isArray(curr)?__spreadArray$1(__spreadArray$1([],__read$1(acc),false),__read$1(curr),false):__spreadArray$1(__spreadArray$1([],__read$1(acc),false),[curr],false)}),[])};List.of=function(){var args=[];for(var _i=0;_i<arguments.length;_i++){args[_i]=arguments[_i]}return new List((function(){return __generator(this,(function(_a){switch(_a.label){case 0:return[5,__values(args)];case 1:_a.sent();return[2,args]}}))}),args.length)};List.from=function(iterable){return iterable?new List((function(){return __generator(this,(function(_a){switch(_a.label){case 0:return[5,__values(iterable)];case 1:_a.sent();return[2]}}))}),iterable.length):List.empty()};List.range=function(start,end,step){if(step===void 0){step=1}return new List((function(){var i;return __generator(this,(function(_a){switch(_a.label){case 0:i=start;_a.label=1;case 1:if(!(i<=end))return[3,3];return[4,i];case 2:_a.sent();i+=step;return[3,1];case 3:return[2]}}))}),Math.floor((end-start+1)/step))};List.integers=function(){return this.range(0,Infinity)};List.empty=function(){return new List((function(){return __generator(this,(function(_a){return[2]}))}),0)};List.prototype.map=function(fn){var generator=this.generator();return new List((function(){var generator_1,generator_1_1,value,e_1_1;var e_1,_a;return __generator(this,(function(_b){switch(_b.label){case 0:_b.trys.push([0,5,6,7]);generator_1=__values(generator),generator_1_1=generator_1.next();_b.label=1;case 1:if(!!generator_1_1.done)return[3,4];value=generator_1_1.value;return[4,fn(value)];case 2:_b.sent();_b.label=3;case 3:generator_1_1=generator_1.next();return[3,1];case 4:return[3,7];case 5:e_1_1=_b.sent();e_1={error:e_1_1};return[3,7];case 6:try{if(generator_1_1&&!generator_1_1.done&&(_a=generator_1.return))_a.call(generator_1)}finally{if(e_1)throw e_1.error}return[7];case 7:return[2]}}))}),this.length)};List.prototype.drop=function(count){var generator=this.generator();return new List((function(){var next,n;return __generator(this,(function(_a){switch(_a.label){case 0:next=generator.next();n=1;_a.label=1;case 1:if(!!next.done)return[3,4];if(!(n>count))return[3,3];return[4,next.value];case 2:_a.sent();_a.label=3;case 3:n++;next=generator.next();return[3,1];case 4:return[2]}}))}),this.length-count)};List.prototype.tail=function(){return this.drop(1)};List.prototype.scan=function(fn,seed){var generator=this.generator();return new List((function(){var acc,generator_2,generator_2_1,value,e_2_1;var e_2,_a;return __generator(this,(function(_b){switch(_b.label){case 0:acc=seed;_b.label=1;case 1:_b.trys.push([1,6,7,8]);generator_2=__values(generator),generator_2_1=generator_2.next();_b.label=2;case 2:if(!!generator_2_1.done)return[3,5];value=generator_2_1.value;return[4,acc=fn(acc,value)];case 3:_b.sent();_b.label=4;case 4:generator_2_1=generator_2.next();return[3,2];case 5:return[3,8];case 6:e_2_1=_b.sent();e_2={error:e_2_1};return[3,8];case 7:try{if(generator_2_1&&!generator_2_1.done&&(_a=generator_2.return))_a.call(generator_2)}finally{if(e_2)throw e_2.error}return[7];case 8:return[2]}}))}),this.length)};List.prototype.reduce=function(fn,seed){return this.toArray().reduce(fn,seed)};List.prototype.filter=function(fn){var generator=this.generator();return new List((function(){var generator_3,generator_3_1,value,e_3_1;var e_3,_a;return __generator(this,(function(_b){switch(_b.label){case 0:_b.trys.push([0,5,6,7]);generator_3=__values(generator),generator_3_1=generator_3.next();_b.label=1;case 1:if(!!generator_3_1.done)return[3,4];value=generator_3_1.value;if(!fn(value))return[3,3];return[4,value];case 2:_b.sent();_b.label=3;case 3:generator_3_1=generator_3.next();return[3,1];case 4:return[3,7];case 5:e_3_1=_b.sent();e_3={error:e_3_1};return[3,7];case 6:try{if(generator_3_1&&!generator_3_1.done&&(_a=generator_3.return))_a.call(generator_3)}finally{if(e_3)throw e_3.error}return[7];case 7:return[2]}}))}),this.length)};List.prototype.where=function(fn){return this.filter(fn)};List.prototype.concat=function(){var args=[];for(var _i=0;_i<arguments.length;_i++){args[_i]=arguments[_i]}var generator=this.generator();var toAdd=List.flattenArgs(args);return new List((function(){return __generator(this,(function(_a){switch(_a.label){case 0:return[5,__values(generator)];case 1:_a.sent();return[5,__values(toAdd)];case 2:_a.sent();return[2]}}))}),this.length+toAdd.length)};List.prototype.take=function(count){var generator=this.generator();return new List((function(){var next,n;return __generator(this,(function(_a){switch(_a.label){case 0:next=generator.next();n=0;_a.label=1;case 1:if(!(!next.done&&count>n))return[3,3];return[4,next.value];case 2:_a.sent();n++;next=generator.next();return[3,1];case 3:return[2]}}))}),this.length>count?count:this.length)};List.prototype.all=function(fn){var generator=this.generator();var newList=new List((function(){var _a,_b,value,e_4_1;var e_4,_c;return __generator(this,(function(_d){switch(_d.label){case 0:_d.trys.push([0,7,8,9]);_a=__values(generator),_b=_a.next();_d.label=1;case 1:if(!!_b.done)return[3,6];value=_b.value;if(!fn(value))return[3,3];return[4,value];case 2:_d.sent();return[3,5];case 3:return[4,value];case 4:return[2,_d.sent()];case 5:_b=_a.next();return[3,1];case 6:return[3,9];case 7:e_4_1=_d.sent();e_4={error:e_4_1};return[3,9];case 8:try{if(_b&&!_b.done&&(_c=_a.return))_c.call(_a)}finally{if(e_4)throw e_4.error}return[7];case 9:return[2]}}))}),this.length);return newList.toArray().length===this.length};List.prototype.any=function(fn){var generator=this.generator();var newList=new List((function(){var _a,_b,value,e_5_1;var e_5,_c;return __generator(this,(function(_d){switch(_d.label){case 0:_d.trys.push([0,5,6,7]);_a=__values(generator),_b=_a.next();_d.label=1;case 1:if(!!_b.done)return[3,4];value=_b.value;if(!fn(value))return[3,3];return[4,value];case 2:return[2,_d.sent()];case 3:_b=_a.next();return[3,1];case 4:return[3,7];case 5:e_5_1=_d.sent();e_5={error:e_5_1};return[3,7];case 6:try{if(_b&&!_b.done&&(_c=_a.return))_c.call(_a)}finally{if(e_5)throw e_5.error}return[7];case 7:return[2]}}))}),this.length);return newList.toArray().length>=1};List.prototype.some=function(fn){return this.any(fn)};List.prototype.ofType=function(type){return this.filter((function(a){return a instanceof type}))};List.prototype.toDictionary=function(key){return this.reduce((function(acc,curr,idx){var _a,_b;return key?curr[key]?__assign(__assign({},acc),(_a={},_a[curr[key]]=curr,_a)):acc:__assign(__assign({},acc),(_b={},_b[idx]=curr,_b))}),{})};List.prototype.sum=function(){return this.toArray().reduce((function(acc,curr){return typeof curr==="number"?acc+curr:0}),0)};List.prototype.headOr=function(valueWhenUndefined){return this.headOrUndefined()||valueWhenUndefined};List.prototype.headOrUndefined=function(){return this.generator().next().value};List.prototype.headOrCompute=function(fn){return this.headOrUndefined()||fn()};List.prototype.headOrThrow=function(msg){return this.headOrUndefined()||function(){throw new Error(msg)}()};List.prototype.toArray=function(){return __spreadArray$1([],__read$1(this),false)};List.prototype.toIterable=function(){return this.toArray()};return List}();var __read=undefined&&undefined.__read||function(o,n){var m=typeof Symbol==="function"&&o[Symbol.iterator];if(!m)return o;var i=m.call(o),r,ar=[],e;try{while((n===void 0||n-- >0)&&!(r=i.next()).done)ar.push(r.value)}catch(error){e={error:error}}finally{try{if(r&&!r.done&&(m=i["return"]))m.call(i)}finally{if(e)throw e.error}}return ar};var __spreadArray=undefined&&undefined.__spreadArray||function(to,from,pack){if(pack||arguments.length===2)for(var i=0,l=from.length,ar;i<l;i++){if(ar||!(i in from)){if(!ar)ar=Array.prototype.slice.call(from,0,i);ar[i]=from[i]}}return to.concat(ar||Array.prototype.slice.call(from))};function listOf(){var args=[];for(var _i=0;_i<arguments.length;_i++){args[_i]=arguments[_i]}return List.of.apply(List,__spreadArray([],__read(args),false))}function listFrom(value){return List.from(value)}var StatePair=function(){function StatePair(state,value){this.state=state;this.value=value}return StatePair}();var State=function(){function State(fn){this.fn=fn}State.prototype.of=function(fn){return new State(fn)};State.prototype.map=function(fn){var _this=this;return new State((function(c){return fn(_this.run(c))}))};State.prototype.flatMap=function(fn){var _this=this;return new State((function(c){var pair=fn(_this.run(c)).run(c);return[pair.state,pair.value]}))};State.prototype.run=function(config){var tupple=this.fn(config);return new StatePair(tupple[0],tupple[1])};return State}();var Logger=function(){function Logger(logs,value){this.logs=logs;this.value=value}Logger.logger=function(logs,value){return new Logger(logs,value)};Logger.tell=function(s){return new Logger([s],0)};Logger.startWith=function(s,value){return new Logger([s],value)};Logger.prototype.of=function(v){return new Logger([],v)};Logger.prototype.flatMap=function(fn){var result=fn(this.value);return new Logger(this.logs.concat(result.logs),result.value)};Logger.prototype.flatMapPair=function(fn){var result=fn(this.value);return new Logger(this.logs.concat(result[0]),result[1])};Logger.prototype.runUsing=function(fn){return fn({logs:this.logs,value:this.value})};return Logger}();exports.Either=Either;exports.FailResult=FailResult;exports.List=List;exports.Logger=Logger;exports.Maybe=Maybe;exports.Monad=Monad;exports.OkResult=OkResult;exports.Reader=Reader;exports.Result=Result;exports.State=State;exports.StatePair=StatePair;exports.ask=ask;exports.asks=asks;exports.catchResult=catchResult;exports.combine=combine;exports.either=either;exports.fail=fail;exports.listFrom=listFrom;exports.listOf=listOf;exports.maybe=maybe;exports.maybeProps=maybeProps;exports.maybeToObservable=maybeToObservable;exports.maybeToPromise=maybeToPromise;exports.none=none;exports.observableToMaybe=observableToMaybe;exports.observableToResult=observableToResult;exports.ok=ok;exports.promiseToMaybe=promiseToMaybe;exports.promiseToResult=promiseToResult;exports.reader=reader;exports.readerOf=readerOf;exports.result=result;exports.resultToObservable=resultToObservable;exports.resultToPromise=resultToPromise;exports.sequence=sequence;exports.some=some;exports.traverse=traverse;exports.tryPromiseToMaybe=tryPromiseToMaybe;exports.tryPromiseToResult=tryPromiseToResult;exports.unwrapResultAsObservable=unwrapResultAsObservable;Object.defineProperty(exports,"__esModule",{value:true})}));