2025-06-27 10:04:22 +08:00

2 lines
34 KiB
JavaScript

var l={Identity:function(t){return t},True:function(){return!0},Blank:function(){}},m={Boolean:"boolean",Number:"number",String:"string",Object:typeof{},Undefined:"undefined",Function:"function"},L={"":l.Identity},o={createLambda:function(t){if(t==null)return l.Identity;if(typeof t===m.String){let n=L[t];if(n!=null)return n;if(t.indexOf("=>")===-1){const e=new RegExp("[$]+","g");let r=0,i;for(;(i=e.exec(t))!=null;)i[0].length>r&&(r=i[0].length);const a=[];for(let c=1;c<=r;c++){let p="";for(let h=0;h<c;h++)p+="$";a.push(p)}const f=a.join(",");return n=new Function(f,"return "+t),L[t]=n,n}else{const e=t.match(/^[(\s]*([^()]*?)[)\s]*=>(.*)/);return n=new Function(e[1],e[2].match(/\breturn\b/)?e[2]:"return "+e[2]),L[t]=n,n}}return t},defineProperty:function(t,n,e){Object.defineProperty(t,n,{enumerable:!1,configurable:!0,writable:!0,value:e})},compare:function(t,n){return t===n?0:t>n?1:-1},dispose:function(t){t!=null&&t.dispose()},hasNativeIteratorSupport:function(){return typeof Symbol<"u"&&typeof Symbol.iterator<"u"}},w={Before:0,Running:1,After:2},s=function(t,n,e){var r=new B,i=w.Before;this.current=r.current,this.moveNext=function(){try{switch(i){case w.Before:i=w.Running,t();case w.Running:return n.apply(r)?!0:(this.dispose(),!1);case w.After:return!1}}catch(a){throw this.dispose(),a}},this.dispose=function(){if(i==w.Running)try{e()}finally{i=w.After}}},B=function(){var t=null;this.current=function(){return t},this.yieldReturn=function(n){return t=n,!0},this.yieldBreak=function(){return!1}},u=function(t){this.getEnumerator=t};u.Utils={};u.Utils.createLambda=function(t){return o.createLambda(t)};u.Utils.createEnumerable=function(t){return new u(t)};u.Utils.createEnumerator=function(t,n,e){return new s(t,n,e)};u.Utils.extendTo=function(t){var n=t.prototype,e;t===Array?(e=d.prototype,o.defineProperty(n,"getSource",function(){return this})):(e=u.prototype,o.defineProperty(n,"getEnumerator",function(){return u.from(this).getEnumerator()}));for(let r in e){const i=e[r];n[r]!=i&&(n[r]!=null&&(r=r+"ByLinq",n[r]==i)||i instanceof Function&&o.defineProperty(n,r,i))}};u.Utils.recallFrom=function(t){var n=t.prototype,e;t===Array?(e=d.prototype,delete n.getSource):(e=u.prototype,delete n.getEnumerator);for(const r in e){const i=e[r];n[r+"ByLinq"]?delete n[r+"ByLinq"]:n[r]==i&&i instanceof Function&&delete n[r]}};u.choice=function(){var t=arguments;return new u(function(){return new s(function(){t=t[0]instanceof Array?t[0]:t[0].getEnumerator!=null?t[0].toArray():t},function(){return this.yieldReturn(t[Math.floor(Math.random()*t.length)])},l.Blank)})};u.cycle=function(){var t=arguments;return new u(function(){var n=0;return new s(function(){t=t[0]instanceof Array?t[0]:t[0].getEnumerator!=null?t[0].toArray():t},function(){return n>=t.length&&(n=0),this.yieldReturn(t[n++])},l.Blank)})};u.empty=function(){return new u(function(){return new s(l.Blank,function(){return!1},l.Blank)})};u.from=function(t){if(t==null)return u.empty();if(t instanceof u)return t;if(typeof t==m.Number||typeof t==m.Boolean)return u.repeat(t,1);if(typeof t==m.String)return new u(function(){var n=0;return new s(l.Blank,function(){return n<t.length?this.yieldReturn(t.charAt(n++)):!1},l.Blank)});if(typeof t!=m.Function){if(typeof t.length==m.Number)return new d(t);if(typeof Symbol<"u"&&typeof t[Symbol.iterator]<"u")return new u(function(){return new s(l.Blank,function(){var n=t.next();return n.done?!1:this.yieldReturn(n.value)},l.Blank)})}return new u(function(){var n=[],e=0;return new s(function(){for(const r in t){const i=t[r];!(i instanceof Function)&&Object.prototype.hasOwnProperty.call(t,r)&&n.push({key:r,value:i})}},function(){return e<n.length?this.yieldReturn(n[e++]):!1},l.Blank)})},u.make=function(t){return u.repeat(t,1)};u.matches=function(t,n,e){return e==null&&(e=""),n instanceof RegExp&&(e+=n.ignoreCase?"i":"",e+=n.multiline?"m":"",n=n.source),e.indexOf("g")===-1&&(e+="g"),new u(function(){var r;return new s(function(){r=new RegExp(n,e)},function(){var i=r.exec(t);return i?this.yieldReturn(i):!1},l.Blank)})};u.range=function(t,n,e){return e==null&&(e=1),new u(function(){var r,i=0;return new s(function(){r=t-e},function(){return i++<n?this.yieldReturn(r+=e):this.yieldBreak()},l.Blank)})};u.rangeDown=function(t,n,e){return e==null&&(e=1),new u(function(){var r,i=0;return new s(function(){r=t+e},function(){return i++<n?this.yieldReturn(r-=e):this.yieldBreak()},l.Blank)})};u.rangeTo=function(t,n,e){return e==null&&(e=1),t<n?new u(function(){var r;return new s(function(){r=t-e},function(){var i=r+=e;return i<=n?this.yieldReturn(i):this.yieldBreak()},l.Blank)}):new u(function(){var r;return new s(function(){r=t+e},function(){var i=r-=e;return i>=n?this.yieldReturn(i):this.yieldBreak()},l.Blank)})};u.repeat=function(t,n){return n!=null?u.repeat(t).take(n):new u(function(){return new s(l.Blank,function(){return this.yieldReturn(t)},l.Blank)})};u.repeatWithFinalize=function(t,n){return t=o.createLambda(t),n=o.createLambda(n),new u(function(){var e;return new s(function(){e=t()},function(){return this.yieldReturn(e)},function(){e!=null&&(n(e),e=null)})})};u.generate=function(t,n){return n!=null?u.generate(t).take(n):(t=o.createLambda(t),new u(function(){return new s(l.Blank,function(){return this.yieldReturn(t())},l.Blank)}))};u.toInfinity=function(t,n){return t==null&&(t=0),n==null&&(n=1),new u(function(){var e;return new s(function(){e=t-n},function(){return this.yieldReturn(e+=n)},l.Blank)})};u.toNegativeInfinity=function(t,n){return t==null&&(t=0),n==null&&(n=1),new u(function(){var e;return new s(function(){e=t+n},function(){return this.yieldReturn(e-=n)},l.Blank)})};u.unfold=function(t,n){return n=o.createLambda(n),new u(function(){var e=!0,r;return new s(l.Blank,function(){return e?(e=!1,r=t,this.yieldReturn(r)):(r=n(r),this.yieldReturn(r))},l.Blank)})};u.defer=function(t){return new u(function(){var n;return new s(function(){n=u.from(t()).getEnumerator()},function(){return n.moveNext()?this.yieldReturn(n.current()):this.yieldBreak()},function(){o.dispose(n)})})};u.prototype.traverseBreadthFirst=function(t,n){var e=this;return t=o.createLambda(t),n=o.createLambda(n),new u(function(){var r,i=0,a=[];return new s(function(){r=e.getEnumerator()},function(){for(;;){if(r.moveNext())return a.push(r.current()),this.yieldReturn(n(r.current(),i));const f=u.from(a).selectMany(function(c){return t(c)});if(f.any())i++,a=[],o.dispose(r),r=f.getEnumerator();else return!1}},function(){o.dispose(r)})})};u.prototype.traverseDepthFirst=function(t,n){var e=this;return t=o.createLambda(t),n=o.createLambda(n),new u(function(){var r=[],i;return new s(function(){i=e.getEnumerator()},function(){for(;;){if(i.moveNext()){const a=n(i.current(),r.length);return r.push(i),i=u.from(t(i.current())).getEnumerator(),this.yieldReturn(a)}if(r.length<=0)return!1;o.dispose(i),i=r.pop()}},function(){try{o.dispose(i)}finally{u.from(r).forEach(function(a){a.dispose()})}})})};u.prototype.flatten=function(){var t=this;return new u(function(){var n,e=null;return new s(function(){n=t.getEnumerator()},function(){for(;;){if(e!=null){if(e.moveNext())return this.yieldReturn(e.current());e=null}if(n.moveNext())if(n.current()instanceof Array){o.dispose(e),e=u.from(n.current()).selectMany(l.Identity).flatten().getEnumerator();continue}else return this.yieldReturn(n.current());return!1}},function(){try{o.dispose(n)}finally{o.dispose(e)}})})};u.prototype.pairwise=function(t){var n=this;return t=o.createLambda(t),new u(function(){var e;return new s(function(){e=n.getEnumerator(),e.moveNext()},function(){var r=e.current();return e.moveNext()?this.yieldReturn(t(r,e.current())):!1},function(){o.dispose(e)})})};u.prototype.scan=function(t,n){var e;n==null?(n=o.createLambda(t),e=!1):(n=o.createLambda(n),e=!0);var r=this;return new u(function(){var i,a,f=!0;return new s(function(){i=r.getEnumerator()},function(){if(f){if(f=!1,e)return this.yieldReturn(a=t);if(i.moveNext())return this.yieldReturn(a=i.current())}return i.moveNext()?this.yieldReturn(a=n(a,i.current())):!1},function(){o.dispose(i)})})};u.prototype.select=function(t){if(t=o.createLambda(t),t.length<=1)return new b(this,null,t);var n=this;return new u(function(){var e,r=0;return new s(function(){e=n.getEnumerator()},function(){return e.moveNext()?this.yieldReturn(t(e.current(),r++)):!1},function(){o.dispose(e)})})};u.prototype.selectMany=function(t,n){var e=this;return t=o.createLambda(t),n==null&&(n=function(r,i){return i}),n=o.createLambda(n),new u(function(){var r,i=void 0,a=0;return new s(function(){r=e.getEnumerator()},function(){if(i===void 0&&!r.moveNext())return!1;do{if(i==null){const f=t(r.current(),a++);i=u.from(f).getEnumerator()}if(i.moveNext())return this.yieldReturn(n(r.current(),i.current()));o.dispose(i),i=null}while(r.moveNext());return!1},function(){try{o.dispose(r)}finally{o.dispose(i)}})})};u.prototype.where=function(t){if(t=o.createLambda(t),t.length<=1)return new E(this,t);var n=this;return new u(function(){var e,r=0;return new s(function(){e=n.getEnumerator()},function(){for(;e.moveNext();)if(t(e.current(),r++))return this.yieldReturn(e.current());return!1},function(){o.dispose(e)})})};u.prototype.choose=function(t){t=o.createLambda(t);var n=this;return new u(function(){var e,r=0;return new s(function(){e=n.getEnumerator()},function(){for(;e.moveNext();){const i=t(e.current(),r++);if(i!=null)return this.yieldReturn(i)}return this.yieldBreak()},function(){o.dispose(e)})})};u.prototype.ofType=function(t){var n;switch(t){case Number:n=m.Number;break;case String:n=m.String;break;case Boolean:n=m.Boolean;break;case Function:n=m.Function;break;default:n=null;break}return n===null?this.where(function(e){return e instanceof t}):this.where(function(e){return typeof e===n})};u.prototype.zip=function(){var t=arguments,n=o.createLambda(arguments[arguments.length-1]),e=this;if(arguments.length==2){const r=arguments[0];return new u(function(){var i,a,f=0;return new s(function(){i=e.getEnumerator(),a=u.from(r).getEnumerator()},function(){return i.moveNext()&&a.moveNext()?this.yieldReturn(n(i.current(),a.current(),f++)):!1},function(){try{o.dispose(i)}finally{o.dispose(a)}})})}else return new u(function(){var r,i=0;return new s(function(){var a=u.make(e).concat(u.from(t).takeExceptLast().select(u.from)).select(function(f){return f.getEnumerator()}).toArray();r=u.from(a)},function(){if(r.all(function(a){return a.moveNext()})){const a=r.select(function(f){return f.current()}).toArray();return a.push(i++),this.yieldReturn(n.apply(null,a))}else return this.yieldBreak()},function(){u.from(r).forEach(o.dispose)})})};u.prototype.merge=function(){var t=arguments,n=this;return new u(function(){var e,r=-1;return new s(function(){e=u.make(n).concat(u.from(t).select(u.from)).select(function(i){return i.getEnumerator()}).toArray()},function(){for(;e.length>0;){r=r>=e.length-1?0:r+1;const i=e[r];if(i.moveNext())return this.yieldReturn(i.current());i.dispose(),e.splice(r--,1)}return this.yieldBreak()},function(){u.from(e).forEach(o.dispose)})})};u.prototype.join=function(t,n,e,r,i){n=o.createLambda(n),e=o.createLambda(e),r=o.createLambda(r),i=o.createLambda(i);var a=this;return new u(function(){var f,c,p=null,h=0;return new s(function(){f=a.getEnumerator(),c=u.from(t).toLookup(e,l.Identity,i)},function(){for(;;){if(p!=null){let v=p[h++];if(v!==void 0)return this.yieldReturn(r(f.current(),v));v=null,h=0}if(f.moveNext()){const v=n(f.current());p=c.get(v).toArray()}else return!1}},function(){o.dispose(f)})})};u.prototype.leftJoin=function(t,n,e,r,i){n=o.createLambda(n),e=o.createLambda(e),r=o.createLambda(r),i=o.createLambda(i);var a=this;return new u(function(){var f,c,p=null,h=0;return new s(function(){f=a.getEnumerator(),c=u.from(t).toLookup(e,l.Identity,i)},function(){for(;;){if(p!=null){let v=p[h++];if(v!==void 0)return this.yieldReturn(r(f.current(),v));v=null,h=0}if(f.moveNext()){const v=n(f.current());if(p=c.get(v).toArray(),p==null||p.length==0)return this.yieldReturn(r(f.current(),null))}else return!1}},function(){o.dispose(f)})})};u.prototype.groupJoin=function(t,n,e,r,i){n=o.createLambda(n),e=o.createLambda(e),r=o.createLambda(r),i=o.createLambda(i);var a=this;return new u(function(){var f=a.getEnumerator(),c=null;return new s(function(){f=a.getEnumerator(),c=u.from(t).toLookup(e,l.Identity,i)},function(){if(f.moveNext()){const p=c.get(n(f.current()));return this.yieldReturn(r(f.current(),p))}return!1},function(){o.dispose(f)})})};u.prototype.all=function(t){t=o.createLambda(t);var n=!0;return this.forEach(function(e){if(!t(e))return n=!1,!1}),n};u.prototype.any=function(t){t=o.createLambda(t);var n=this.getEnumerator();try{if(arguments.length==0)return n.moveNext();for(;n.moveNext();)if(t(n.current()))return!0;return!1}finally{o.dispose(n)}};u.prototype.isEmpty=function(){return!this.any()};u.prototype.concat=function(){var t=this;if(arguments.length==1){const n=arguments[0];return new u(function(){var e,r;return new s(function(){e=t.getEnumerator()},function(){if(r==null){if(e.moveNext())return this.yieldReturn(e.current());r=u.from(n).getEnumerator()}return r.moveNext()?this.yieldReturn(r.current()):!1},function(){try{o.dispose(e)}finally{o.dispose(r)}})})}else{const n=arguments;return new u(function(){var e;return new s(function(){e=u.make(t).concat(u.from(n).select(u.from)).select(function(r){return r.getEnumerator()}).toArray()},function(){for(;e.length>0;){const r=e[0];if(r.moveNext())return this.yieldReturn(r.current());r.dispose(),e.splice(0,1)}return this.yieldBreak()},function(){u.from(e).forEach(o.dispose)})})}};u.prototype.insert=function(t,n){var e=this;return new u(function(){var r,i,a=0,f=!1;return new s(function(){r=e.getEnumerator(),i=u.from(n).getEnumerator()},function(){return a==t&&i.moveNext()?(f=!0,this.yieldReturn(i.current())):r.moveNext()?(a++,this.yieldReturn(r.current())):!f&&i.moveNext()?this.yieldReturn(i.current()):!1},function(){try{o.dispose(r)}finally{o.dispose(i)}})})};u.prototype.alternate=function(t){var n=this;return new u(function(){var e,r,i,a;return new s(function(){t instanceof Array||t.getEnumerator!=null?i=u.from(u.from(t).toArray()):i=u.make(t),r=n.getEnumerator(),r.moveNext()&&(e=r.current())},function(){for(;;){if(a!=null){if(a.moveNext())return this.yieldReturn(a.current());a=null}if(e==null&&r.moveNext()){e=r.current(),a=i.getEnumerator();continue}else if(e!=null){const f=e;return e=null,this.yieldReturn(f)}return this.yieldBreak()}},function(){try{o.dispose(r)}finally{o.dispose(a)}})})};u.prototype.contains=function(t,n){n=o.createLambda(n);var e=this.getEnumerator();try{for(;e.moveNext();)if(n(e.current())===t)return!0;return!1}finally{o.dispose(e)}};u.prototype.defaultIfEmpty=function(t){var n=this;return t===void 0&&(t=null),new u(function(){var e,r=!0;return new s(function(){e=n.getEnumerator()},function(){return e.moveNext()?(r=!1,this.yieldReturn(e.current())):r?(r=!1,this.yieldReturn(t)):!1},function(){o.dispose(e)})})};u.prototype.distinct=function(t){return this.except(u.empty(),t)};u.prototype.distinctUntilChanged=function(t){t=o.createLambda(t);var n=this;return new u(function(){var e,r;return new s(function(){e=n.getEnumerator()},function(){for(;e.moveNext();){const i=t(e.current());if(r!==i)return r=i,this.yieldReturn(e.current())}return this.yieldBreak()},function(){o.dispose(e)})})};u.prototype.except=function(t,n){n=o.createLambda(n);var e=this;return new u(function(){var r,i;return new s(function(){r=e.getEnumerator(),i=new k(n),u.from(t).forEach(function(a){i.add(a)})},function(){for(;r.moveNext();){const a=r.current();if(!i.contains(a))return i.add(a),this.yieldReturn(a)}return!1},function(){o.dispose(r)})})};u.prototype.intersect=function(t,n){n=o.createLambda(n);var e=this;return new u(function(){var r,i,a;return new s(function(){r=e.getEnumerator(),i=new k(n),u.from(t).forEach(function(f){i.add(f)}),a=new k(n)},function(){for(;r.moveNext();){const f=r.current();if(!a.contains(f)&&i.contains(f))return a.add(f),this.yieldReturn(f)}return!1},function(){o.dispose(r)})})};u.prototype.sequenceEqual=function(t,n){n=o.createLambda(n);var e=this.getEnumerator();try{const r=u.from(t).getEnumerator();try{for(;e.moveNext();)if(!r.moveNext()||n(e.current())!==n(r.current()))return!1;return!r.moveNext()}finally{o.dispose(r)}}finally{o.dispose(e)}};u.prototype.union=function(t,n){n=o.createLambda(n);var e=this;return new u(function(){var r,i,a;return new s(function(){r=e.getEnumerator(),a=new k(n)},function(){var f;if(i===void 0){for(;r.moveNext();)if(f=r.current(),!a.contains(f))return a.add(f),this.yieldReturn(f);i=u.from(t).getEnumerator()}for(;i.moveNext();)if(f=i.current(),!a.contains(f))return a.add(f),this.yieldReturn(f);return!1},function(){try{o.dispose(r)}finally{o.dispose(i)}})})};u.prototype.orderBy=function(t,n){return new g(this,t,n,!1)};u.prototype.orderByDescending=function(t,n){return new g(this,t,n,!0)};u.prototype.reverse=function(){var t=this;return new u(function(){var n,e;return new s(function(){n=t.toArray(),e=n.length},function(){return e>0?this.yieldReturn(n[--e]):!1},l.Blank)})};u.prototype.shuffle=function(){var t=this;return new u(function(){var n;return new s(function(){n=t.toArray()},function(){if(n.length>0){const e=Math.floor(Math.random()*n.length);return this.yieldReturn(n.splice(e,1)[0])}return!1},l.Blank)})};u.prototype.weightedSample=function(t){t=o.createLambda(t);var n=this;return new u(function(){var e,r=0;return new s(function(){e=n.choose(function(i){var a=t(i);return a<=0?null:(r+=a,{value:i,bound:r})}).toArray()},function(){if(e.length>0){const i=Math.floor(Math.random()*r)+1;let a=-1,f=e.length;for(;f-a>1;){const c=Math.floor((a+f)/2);e[c].bound>=i?f=c:a=c}return this.yieldReturn(e[f].value)}return this.yieldBreak()},l.Blank)})};u.prototype.groupBy=function(t,n,e,r){var i=this;return t=o.createLambda(t),n=o.createLambda(n),e!=null&&(e=o.createLambda(e)),r=o.createLambda(r),new u(function(){var a;return new s(function(){a=i.toLookup(t,n,r).toEnumerable().getEnumerator()},function(){for(;a.moveNext();)return e==null?this.yieldReturn(a.current()):this.yieldReturn(e(a.current().key(),a.current()));return!1},function(){o.dispose(a)})})};u.prototype.partitionBy=function(t,n,e,r){var i=this;t=o.createLambda(t),n=o.createLambda(n),r=o.createLambda(r);var a;return e==null?(a=!1,e=function(f,c){return new R(f,c)}):(a=!0,e=o.createLambda(e)),new u(function(){var f,c,p,h=[];return new s(function(){f=i.getEnumerator(),f.moveNext()&&(c=t(f.current()),p=r(c),h.push(n(f.current())))},function(){for(var v;(v=f.moveNext())==!0&&p===r(t(f.current()));)h.push(n(f.current()));if(h.length>0){const y=a?e(c,u.from(h)):e(c,h);return v?(c=t(f.current()),p=r(c),h=[n(f.current())]):h=[],this.yieldReturn(y)}return!1},function(){o.dispose(f)})})};u.prototype.buffer=function(t){var n=this;return new u(function(){var e;return new s(function(){e=n.getEnumerator()},function(){for(var r=[],i=0;e.moveNext();)if(r.push(e.current()),++i>=t)return this.yieldReturn(r);return r.length>0?this.yieldReturn(r):!1},function(){o.dispose(e)})})};u.prototype.aggregate=function(t,n,e){return e=o.createLambda(e),e(this.scan(t,n,e).last())};u.prototype.average=function(t){t=o.createLambda(t);var n=0,e=0;return this.forEach(function(r){n+=t(r),++e}),n/e};u.prototype.count=function(t){t=t==null?l.True:o.createLambda(t);var n=0;return this.forEach(function(e,r){t(e,r)&&++n}),n};u.prototype.max=function(t){return t==null&&(t=l.Identity),this.select(t).aggregate(function(n,e){return n>e?n:e})};u.prototype.min=function(t){return t==null&&(t=l.Identity),this.select(t).aggregate(function(n,e){return n<e?n:e})};u.prototype.maxBy=function(t){return t=o.createLambda(t),this.aggregate(function(n,e){return t(n)>t(e)?n:e})};u.prototype.minBy=function(t){return t=o.createLambda(t),this.aggregate(function(n,e){return t(n)<t(e)?n:e})};u.prototype.sum=function(t){return t==null&&(t=l.Identity),this.select(t).aggregate(0,function(n,e){return n+e})};u.prototype.elementAt=function(t){var n,e=!1;if(this.forEach(function(r,i){if(i==t)return n=r,e=!0,!1}),!e)throw new Error("index is less than 0 or greater than or equal to the number of elements in source.");return n};u.prototype.elementAtOrDefault=function(t,n){n===void 0&&(n=null);var e,r=!1;return this.forEach(function(i,a){if(a==t)return e=i,r=!0,!1}),r?e:n};u.prototype.first=function(t){if(t!=null)return this.where(t).first();var n,e=!1;if(this.forEach(function(r){return n=r,e=!0,!1}),!e)throw new Error("first:No element satisfies the condition.");return n};u.prototype.firstOrDefault=function(t,n){if(t!==void 0){if(typeof t===m.Function||typeof o.createLambda(t)===m.Function)return this.where(t).firstOrDefault(void 0,n);n=t}var e,r=!1;return this.forEach(function(i){return e=i,r=!0,!1}),r?e:n};u.prototype.last=function(t){if(t!=null)return this.where(t).last();var n,e=!1;if(this.forEach(function(r){e=!0,n=r}),!e)throw new Error("last:No element satisfies the condition.");return n};u.prototype.lastOrDefault=function(t,n){if(t!==void 0){if(typeof t===m.Function||typeof o.createLambda(t)===m.Function)return this.where(t).lastOrDefault(void 0,n);n=t}var e,r=!1;return this.forEach(function(i){r=!0,e=i}),r?e:n};u.prototype.single=function(t){if(t!=null)return this.where(t).single();var n,e=!1;if(this.forEach(function(r){if(!e)e=!0,n=r;else throw new Error("single:sequence contains more than one element.")}),!e)throw new Error("single:No element satisfies the condition.");return n};u.prototype.singleOrDefault=function(t,n){if(n===void 0&&(n=null),t!=null)return this.where(t).singleOrDefault(null,n);var e,r=!1;return this.forEach(function(i){if(!r)r=!0,e=i;else throw new Error("single:sequence contains more than one element.")}),r?e:n};u.prototype.skip=function(t){var n=this;return new u(function(){var e,r=0;return new s(function(){for(e=n.getEnumerator();r++<t&&e.moveNext(););},function(){return e.moveNext()?this.yieldReturn(e.current()):!1},function(){o.dispose(e)})})};u.prototype.skipWhile=function(t){t=o.createLambda(t);var n=this;return new u(function(){var e,r=0,i=!1;return new s(function(){e=n.getEnumerator()},function(){for(;!i;)if(e.moveNext()){if(!t(e.current(),r++))return i=!0,this.yieldReturn(e.current());continue}else return!1;return e.moveNext()?this.yieldReturn(e.current()):!1},function(){o.dispose(e)})})};u.prototype.take=function(t){var n=this;return new u(function(){var e,r=0;return new s(function(){e=n.getEnumerator()},function(){return r++<t&&e.moveNext()?this.yieldReturn(e.current()):!1},function(){o.dispose(e)})})};u.prototype.takeWhile=function(t){t=o.createLambda(t);var n=this;return new u(function(){var e,r=0;return new s(function(){e=n.getEnumerator()},function(){return e.moveNext()&&t(e.current(),r++)?this.yieldReturn(e.current()):!1},function(){o.dispose(e)})})};u.prototype.takeExceptLast=function(t){t==null&&(t=1);var n=this;return new u(function(){if(t<=0)return n.getEnumerator();var e,r=[];return new s(function(){e=n.getEnumerator()},function(){for(;e.moveNext();){if(r.length==t)return r.push(e.current()),this.yieldReturn(r.shift());r.push(e.current())}return!1},function(){o.dispose(e)})})};u.prototype.takeFromLast=function(t){if(t<=0||t==null)return u.empty();var n=this;return new u(function(){var e,r,i=[];return new s(function(){e=n.getEnumerator()},function(){for(;e.moveNext();)i.length==t&&i.shift(),i.push(e.current());return r==null&&(r=u.from(i).getEnumerator()),r.moveNext()?this.yieldReturn(r.current()):!1},function(){o.dispose(r)})})};u.prototype.indexOf=function(t){var n=null;return typeof t===m.Function?this.forEach(function(e,r){if(t(e,r))return n=r,!1}):this.forEach(function(e,r){if(e===t)return n=r,!1}),n!==null?n:-1};u.prototype.lastIndexOf=function(t){var n=-1;return typeof t===m.Function?this.forEach(function(e,r){t(e,r)&&(n=r)}):this.forEach(function(e,r){e===t&&(n=r)}),n};u.prototype.cast=function(){return this};u.prototype.asEnumerable=function(){return u.from(this)};u.prototype.toArray=function(){var t=[];return this.forEach(function(n){t.push(n)}),t};u.prototype.toLookup=function(t,n,e){t=o.createLambda(t),n=o.createLambda(n),e=o.createLambda(e);var r=new k(e);return this.forEach(function(i){var a=t(i),f=n(i),c=r.get(a);c!==void 0?c.push(f):r.add(a,[f])}),new A(r)};u.prototype.toObject=function(t,n){t=o.createLambda(t),n=o.createLambda(n);var e={};return this.forEach(function(r){e[t(r)]=n(r)}),e};u.prototype.toDictionary=function(t,n,e){t=o.createLambda(t),n=o.createLambda(n),e=o.createLambda(e);var r=new k(e);return this.forEach(function(i){r.add(t(i),n(i))}),r};u.prototype.toJSONString=function(t,n){if(typeof JSON===m.Undefined||JSON.stringify==null)throw new Error("toJSONString can't find JSON.stringify. This works native JSON support Browser or include json2.js");return JSON.stringify(this.toArray(),t,n)};u.prototype.toJoinedString=function(t,n){return t==null&&(t=""),n==null&&(n=l.Identity),this.select(n).toArray().join(t)};u.prototype.doAction=function(t){var n=this;return t=o.createLambda(t),new u(function(){var e,r=0;return new s(function(){e=n.getEnumerator()},function(){return e.moveNext()?(t(e.current(),r++),this.yieldReturn(e.current())):!1},function(){o.dispose(e)})})};u.prototype.forEach=function(t){t=o.createLambda(t);var n=0,e=this.getEnumerator();try{for(;e.moveNext()&&t(e.current(),n++)!==!1;);}finally{o.dispose(e)}};u.prototype.force=function(){var t=this.getEnumerator();try{for(;t.moveNext(););}finally{o.dispose(t)}};u.prototype.letBind=function(t){t=o.createLambda(t);var n=this;return new u(function(){var e;return new s(function(){e=u.from(t(n)).getEnumerator()},function(){return e.moveNext()?this.yieldReturn(e.current()):!1},function(){o.dispose(e)})})};u.prototype.share=function(){var t=this,n,e=!1;return new N(function(){return new s(function(){n==null&&(n=t.getEnumerator())},function(){if(e)throw new Error("enumerator is disposed");return n.moveNext()?this.yieldReturn(n.current()):!1},l.Blank)},function(){e=!0,o.dispose(n)})};u.prototype.memoize=function(){var t=this,n,e,r=!1;return new N(function(){var i=-1;return new s(function(){e==null&&(e=t.getEnumerator(),n=[])},function(){if(r)throw new Error("enumerator is disposed");return i++,n.length<=i?e.moveNext()?this.yieldReturn(n[i]=e.current()):!1:this.yieldReturn(n[i])},l.Blank)},function(){r=!0,o.dispose(e),n=null})};o.hasNativeIteratorSupport()&&(u.prototype[Symbol.iterator]=function(){return{enumerator:this.getEnumerator(),next:function(){return this.enumerator.moveNext()?{done:!1,value:this.enumerator.current()}:{done:!0}}}});u.prototype.catchError=function(t){t=o.createLambda(t);var n=this;return new u(function(){var e;return new s(function(){e=n.getEnumerator()},function(){try{return e.moveNext()?this.yieldReturn(e.current()):!1}catch(r){return t(r),!1}},function(){o.dispose(e)})})};u.prototype.finallyAction=function(t){t=o.createLambda(t);var n=this;return new u(function(){var e;return new s(function(){e=n.getEnumerator()},function(){return e.moveNext()?this.yieldReturn(e.current()):!1},function(){try{o.dispose(e)}finally{t()}})})};u.prototype.log=function(t){return t=o.createLambda(t),this.doAction(function(n){typeof console!==m.Undefined&&console.log(t(n))})};u.prototype.trace=function(t,n){return t==null&&(t="Trace"),n=o.createLambda(n),this.doAction(function(e){typeof console!==m.Undefined&&console.log(t,n(e))})};var g=function(t,n,e,r,i){this.source=t,this.keySelector=o.createLambda(n),this.descending=r,this.parent=i,e&&(this.comparer=o.createLambda(e))};g.prototype=new u;g.prototype.createOrderedEnumerable=function(t,n,e){return new g(this.source,t,n,e,this)};g.prototype.thenBy=function(t,n){return this.createOrderedEnumerable(t,n,!1)};g.prototype.thenByDescending=function(t,n){return this.createOrderedEnumerable(t,n,!0)};g.prototype.getEnumerator=function(){var t=this,n,e,r=0;return new s(function(){n=[],e=[],t.source.forEach(function(a,f){n.push(a),e.push(f)});var i=x.create(t,null);i.GenerateKeys(n),e.sort(function(a,f){return i.compare(a,f)})},function(){return r<e.length?this.yieldReturn(n[e[r++]]):!1},l.Blank)};var x=function(t,n,e,r){this.keySelector=t,this.descending=e,this.child=r,this.comparer=n,this.keys=null};x.create=function(t,n){var e=new x(t.keySelector,t.comparer,t.descending,n);return t.parent!=null?x.create(t.parent,e):e};x.prototype.GenerateKeys=function(t){var n=t.length,e=this.keySelector,r=new Array(n);for(let i=0;i<n;i++)r[i]=e(t[i]);this.keys=r,this.child!=null&&this.child.GenerateKeys(t)};x.prototype.compare=function(t,n){var e=this.comparer?this.comparer(this.keys[t],this.keys[n]):o.compare(this.keys[t],this.keys[n]);return e==0?this.child!=null?this.child.compare(t,n):o.compare(t,n):this.descending?-e:e};var N=function(t,n){this.dispose=n,u.call(this,t)};N.prototype=new u;var d=function(t){this.getSource=function(){return t}};d.prototype=new u;d.prototype.any=function(t){return t==null?this.getSource().length>0:u.prototype.any.apply(this,arguments)};d.prototype.count=function(t){return t==null?this.getSource().length:u.prototype.count.apply(this,arguments)};d.prototype.elementAt=function(t){var n=this.getSource();return 0<=t&&t<n.length?n[t]:u.prototype.elementAt.apply(this,arguments)};d.prototype.elementAtOrDefault=function(t,n){n===void 0&&(n=null);var e=this.getSource();return 0<=t&&t<e.length?e[t]:n};d.prototype.first=function(t){var n=this.getSource();return t==null&&n.length>0?n[0]:u.prototype.first.apply(this,arguments)};d.prototype.firstOrDefault=function(t,n){if(t!==void 0)return u.prototype.firstOrDefault.apply(this,arguments);n=t;var e=this.getSource();return e.length>0?e[0]:n};d.prototype.last=function(t){var n=this.getSource();return t==null&&n.length>0?n[n.length-1]:u.prototype.last.apply(this,arguments)};d.prototype.lastOrDefault=function(t,n){if(t!==void 0)return u.prototype.lastOrDefault.apply(this,arguments);n=t;var e=this.getSource();return e.length>0?e[e.length-1]:n};d.prototype.skip=function(t){var n=this.getSource();return new u(function(){var e;return new s(function(){e=t<0?0:t},function(){return e<n.length?this.yieldReturn(n[e++]):!1},l.Blank)})};d.prototype.takeExceptLast=function(t){return t==null&&(t=1),this.take(this.getSource().length-t)};d.prototype.takeFromLast=function(t){return this.skip(this.getSource().length-t)};d.prototype.reverse=function(){var t=this.getSource();return new u(function(){var n;return new s(function(){n=t.length},function(){return n>0?this.yieldReturn(t[--n]):!1},l.Blank)})};d.prototype.sequenceEqual=function(t,n){return(t instanceof d||t instanceof Array)&&n==null&&u.from(t).count()!=this.count()?!1:u.prototype.sequenceEqual.apply(this,arguments)};d.prototype.toJoinedString=function(t,n){var e=this.getSource();return n!=null||!(e instanceof Array)?u.prototype.toJoinedString.apply(this,arguments):(t==null&&(t=""),e.join(t))};d.prototype.getEnumerator=function(){var t=this.getSource(),n=-1;return{current:function(){return t[n]},moveNext:function(){return++n<t.length},dispose:l.Blank}};var E=function(t,n){this.prevSource=t,this.prevPredicate=n};E.prototype=new u;E.prototype.where=function(t){if(t=o.createLambda(t),t.length<=1){const n=this.prevPredicate,e=function(r){return n(r)&&t(r)};return new E(this.prevSource,e)}else return u.prototype.where.call(this,t)};E.prototype.select=function(t){return t=o.createLambda(t),t.length<=1?new b(this.prevSource,this.prevPredicate,t):u.prototype.select.call(this,t)};E.prototype.getEnumerator=function(){var t=this.prevPredicate,n=this.prevSource,e;return new s(function(){e=n.getEnumerator()},function(){for(;e.moveNext();)if(t(e.current()))return this.yieldReturn(e.current());return!1},function(){o.dispose(e)})};var b=function(t,n,e){this.prevSource=t,this.prevPredicate=n,this.prevSelector=e};b.prototype=new u;b.prototype.where=function(t){return t=o.createLambda(t),t.length<=1?new E(this,t):u.prototype.where.call(this,t)};b.prototype.select=function(t){if(t=o.createLambda(t),t.length<=1){const n=this.prevSelector,e=function(r){return t(n(r))};return new b(this.prevSource,this.prevPredicate,e)}else return u.prototype.select.call(this,t)};b.prototype.getEnumerator=function(){var t=this.prevPredicate,n=this.prevSelector,e=this.prevSource,r;return new s(function(){r=e.getEnumerator()},function(){for(;r.moveNext();)if(t==null||t(r.current()))return this.yieldReturn(n(r.current()));return!1},function(){o.dispose(r)})};var k=function(){var t=function(a,f){return Object.prototype.hasOwnProperty.call(a,f)},n=function(a){return a===null?"null":a===void 0?"undefined":typeof a.toString===m.Function?a.toString():Object.prototype.toString.call(a)},e=function(a,f){this.key=a,this.value=f,this.prev=null,this.next=null},r=function(){this.first=null,this.last=null};r.prototype={addLast:function(a){this.last!=null?(this.last.next=a,a.prev=this.last,this.last=a):this.first=this.last=a},replace:function(a,f){a.prev!=null?(a.prev.next=f,f.prev=a.prev):this.first=f,a.next!=null?(a.next.prev=f,f.next=a.next):this.last=f},remove:function(a){a.prev!=null?a.prev.next=a.next:this.first=a.next,a.next!=null?a.next.prev=a.prev:this.last=a.prev}};var i=function(a){this.countField=0,this.entryList=new r,this.buckets={},this.compareSelector=a??l.Identity};return i.prototype={add:function(a,f){var c=this.compareSelector(a),p=n(c),h=new e(a,f);if(t(this.buckets,p)){const v=this.buckets[p];for(let y=0;y<v.length;y++)if(this.compareSelector(v[y].key)===c){this.entryList.replace(v[y],h),v[y]=h;return}v.push(h)}else this.buckets[p]=[h];this.countField++,this.entryList.addLast(h)},get:function(a){var f=this.compareSelector(a),c=n(f);if(t(this.buckets,c)){var p=this.buckets[c];for(let h=0;h<p.length;h++){const v=p[h];if(this.compareSelector(v.key)===f)return v.value}}},set:function(a,f){var c=this.compareSelector(a),p=n(c);if(t(this.buckets,p)){const h=this.buckets[p];for(let v=0;v<h.length;v++)if(this.compareSelector(h[v].key)===c){const y=new e(a,f);return this.entryList.replace(h[v],y),h[v]=y,!0}}return!1},contains:function(a){var f=this.compareSelector(a),c=n(f);if(!t(this.buckets,c))return!1;var p=this.buckets[c];for(let h=0;h<p.length;h++)if(this.compareSelector(p[h].key)===f)return!0;return!1},clear:function(){this.countField=0,this.buckets={},this.entryList=new r},remove:function(a){var f=this.compareSelector(a),c=n(f);if(t(this.buckets,c)){var p=this.buckets[c];for(let h=0;h<p.length;h++)if(this.compareSelector(p[h].key)===f){this.entryList.remove(p[h]),p.splice(h,1),p.length==0&&delete this.buckets[c],this.countField--;return}}},count:function(){return this.countField},toEnumerable:function(){var a=this;return new u(function(){var f;return new s(function(){f=a.entryList.first},function(){if(f!=null){const c={key:f.key,value:f.value};return f=f.next,this.yieldReturn(c)}return!1},l.Blank)})}},i}(),A=function(t){this.count=function(){return t.count()},this.get=function(n){return u.from(t.get(n))},this.contains=function(n){return t.contains(n)},this.toEnumerable=function(){return t.toEnumerable().select(function(n){return new R(n.key,n.value)})}},R=function(t,n){this.key=function(){return t},d.call(this,n)};R.prototype=new d;export{u as E};