You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

0 lines
25 KiB

2 years ago
  1. Enumerable=function(){var n=function(n){this.GetEnumerator=n},s,u;n.Choice=function(){var t=arguments[0]instanceof Array?arguments[0]:arguments;return new n(function(){return new i(r.Blank,function(){return this.Yield(t[Math.floor(Math.random()*t.length)])},r.Blank)})};n.Cycle=function(){var t=arguments[0]instanceof Array?arguments[0]:arguments;return new n(function(){var n=0;return new i(r.Blank,function(){return n>=t.length&&(n=0),this.Yield(t[n++])},r.Blank)})};n.Empty=function(){return new n(function(){return new i(r.Blank,function(){return!1},r.Blank)})};n.From=function(e){if(e==null)return n.Empty();if(e instanceof n)return e;if(typeof e==f.Number||typeof e==f.Boolean)return n.Repeat(e,1);if(typeof e==f.String)return new n(function(){var n=0;return new i(r.Blank,function(){return n<e.length?this.Yield(e.charAt(n++)):!1},r.Blank)});if(typeof e!=f.Function){if(typeof e.length==f.Number)return new u(e);if(!(e instanceof Object)&&t.IsIEnumerable(e))return new n(function(){var t=!0,n;return new i(function(){n=new Enumerator(e)},function(){return t?t=!1:n.moveNext(),n.atEnd()?!1:this.Yield(n.item())},r.Blank)})}return new n(function(){var n=[],t=0;return new i(function(){for(var t in e)e[t]instanceof Function||n.push({Key:t,Value:e[t]})},function(){return t<n.length?this.Yield(n[t++]):!1},r.Blank)})};n.Return=function(t){return n.Repeat(t,1)};n.Matches=function(t,u,f){return f==null&&(f=""),u instanceof RegExp&&(f+=u.ignoreCase?"i":"",f+=u.multiline?"m":"",u=u.source),f.indexOf("g")===-1&&(f+="g"),new n(function(){var n;return new i(function(){n=new RegExp(u,f)},function(){var i=n.exec(t);return i?this.Yield(i):!1},r.Blank)})};n.Range=function(t,i,r){return r==null&&(r=1),n.ToInfinity(t,r).Take(i)};n.RangeDown=function(t,i,r){return r==null&&(r=1),n.ToNegativeInfinity(t,r).Take(i)};n.RangeTo=function(t,i,r){return r==null&&(r=1),t<i?n.ToInfinity(t,r).TakeWhile(function(n){return n<=i}):n.ToNegativeInfinity(t,r).TakeWhile(function(n){return n>=i})};n.Repeat=function(t,u){return u!=null?n.Repeat(t).Take(u):new n(function(){return new i(r.Blank,function(){return this.Yield(t)},r.Blank)})};n.RepeatWithFinalize=function(r,u){return r=t.CreateLambda(r),u=t.CreateLambda(u),new n(function(){var n;return new i(function(){n=r()},function(){return this.Yield(n)},function(){n!=null&&(u(n),n=null)})})};n.Generate=function(u,f){return f!=null?n.Generate(u).Take(f):(u=t.CreateLambda(u),new n(function(){return new i(r.Blank,function(){return this.Yield(u())},r.Blank)}))};n.ToInfinity=function(t,u){return t==null&&(t=0),u==null&&(u=1),new n(function(){var n;return new i(function(){n=t-u},function(){return this.Yield(n+=u)},r.Blank)})};n.ToNegativeInfinity=function(t,u){return t==null&&(t=0),u==null&&(u=1),new n(function(){var n;return new i(function(){n=t+u},function(){return this.Yield(n-=u)},r.Blank)})};n.Unfold=function(u,f){return f=t.CreateLambda(f),new n(function(){var t=!0,n;return new i(r.Blank,function(){return t?(t=!1,n=u,this.Yield(n)):(n=f(n),this.Yield(n))},r.Blank)})};n.prototype={CascadeBreadthFirst:function(r,u){var f=this;return r=t.CreateLambda(r),u=t.CreateLambda(u),new n(function(){var e,s=0,o=[];return new i(function(){e=f.GetEnumerator()},function(){for(;;){if(e.MoveNext())return o.push(e.Current()),this.Yield(u(e.Current(),s));var i=n.From(o).SelectMany(function(n){return r(n)});if(i.Any())s++,o=[],t.Dispose(e),e=i.GetEnumerator();else return!1}},function(){t.Dispose(e)})})},CascadeDepthFirst:function(r,u){var f=this;return r=t.CreateLambda(r),u=t.CreateLambda(u),new n(function(){var o=[],e;return new i(function(){e=f.GetEnumerator()},function(){for(;;){if(e.MoveNext()){var i=u(e.Current(),o.length);return o.push(e),e=n.From(r(e.Current())).GetEnumerator(),this.Yield(i)}if(o.length<=0)return!1;t.Dispose(e);e=o.pop()}},function(){try{t.Dispose(e)}finally{n.From(o).ForEach(function(n){n.Dispose()})}})})},Flatten:function(){var u=this;return new n(function(){var e,f=null;return new i(function(){e=u.GetEnumerator()},function(){for(;;){if(f!=null){if(f.MoveNext())return this.Yield(f.Current());f=null}if(e.MoveNext())if