-
Notifications
You must be signed in to change notification settings - Fork 57
/
stream.min.js
7 lines (5 loc) · 5.91 KB
/
stream.min.js
1
2
3
4
5
6
7
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Stream = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
"use strict";function Lazy(t){this.has_evaluated=!1,this.func=t,this.value=null}function Eager(t){this.func=t}function Stream(t,e,r){"undefined"==typeof r&&(r=Lazy),this.wrapper=r,"undefined"!=typeof t&&(this.headValue=t),"undefined"==typeof e&&(e=function(){return this.create()}),"function"==typeof e&&(e=new r(e)),this.tailPromise=e}function _continually(t,e){return Stream.iterate(t(),t,e)}function _iterate(t,e,r){return new Stream(t,function(){return _iterate(e(t),e,r)},r)}function EagerStream(t,e){return Stream.call(this,t,e,Eager)}Lazy.prototype={eval:function(){return this.has_evaluated?this.value:(this.value=this.func(),this.has_evaluated=!0,this.value)}},Eager.prototype={eval:function(){return this.func()}},Stream.prototype={create:function(t,e){return new Stream(t,e,this.wrapper)},empty:function(){return"undefined"==typeof this.headValue},head:function(){if(this.empty())throw new Error("Cannot get the head of the empty stream.");return this.headValue},tail:function(){if(this.empty())throw new Error("Cannot get the tail of the empty stream.");return this.tailPromise.eval()},item:function(t){if(this.empty())throw new Error("Cannot use item() on an empty stream.");for(var e=this;0!=t;){--t;try{e=e.tail()}catch(t){throw new Error("Item index does not exist in stream.")}}try{return e.head()}catch(t){throw new Error("Item index does not exist in stream.")}},length:function(){for(var t=this,e=0;!t.empty();)++e,t=t.tail();return e},add:function(t){return this.zip(function(t,e){return t+e},t)},append:function(t){if(this.empty())return t;var e=this;return this.create(e.head(),function(){return e.tail().append(t)})},zip:function(){var t=Array.prototype.slice.call(arguments,0),e=t[0];t.shift();var r=[this].concat(t),n=this;return r.filter(function(t){return t.empty()}).length>0?new Stream:new Stream(e.apply(null,r.map(function(t){return t.head()})),function(){var r=n.tail();return r.zip.apply(r,[e].concat(t.map(function(t){return t.tail()})))})},map:function(t){if(this.empty())return this;var e=this;return this.create(t(this.head()),function(){return e.tail().map(t)})},concatmap:function(t){return this.reduce(function(e,r){return e.append(t(r))},this.create())},reduce:function(){var t,e,r=arguments[0];if(arguments.length<2){if(this.empty())throw new TypeError("Array length is 0 and no second argument");t=this.head(),e=this.tail()}else t=arguments[1],e=this;return e.empty()?t:e.tail().reduce(r,r(t,e.head()))},sum:function(){return this.reduce(function(t,e){return t+e},0)},walk:function(t){this.map(function(e){return t(e),e}).force()},force:function(){for(var t=this;!t.empty();)t=t.tail()},scale:function(t){return this.map(function(e){return t*e})},filter:function(t){if(this.empty())return this;var e=this.head(),r=this.tail();return t(e)?this.create(e,function(){return r.filter(t)}):r.filter(t)},take:function(t){if(this.empty())return this;if(0==t)return this.create();var e=this;return this.create(this.head(),function(){return e.tail().take(t-1)})},takeWhile:function(t){if(this.empty())return new Stream;for(var e=this,r=[];t(e.head());)r.push(e.head()),e=e.tail();return Stream.fromArray(r)},drop:function(t){for(var e=this;t-- >0;){if(e.empty())return this.create();e=e.tail()}return this.create(e.headValue,e.tailPromise)},dropWhile:function(t){if(this.empty())return new Stream;for(var e=this;t(e.head());)if(e=e.tail(),e.empty())return e;return new Stream(e.headValue,e.tailPromise)},member:function(t){for(var e=this;!e.empty();){if(e.head()==t)return!0;e=e.tail()}return!1},print:function(t){var e;e="undefined"!=typeof t?this.take(t):this,e.walk(function(t){console.log(t)})},toString:function(){return"[stream head: "+this.head()+"; tail: "+this.tail()+"]"}},Stream.continually=function(t){return _continually(t,Lazy)},Stream.continuallyEager=function(t){return _continually(t,Eager)},Stream.repeat=function(t){return Stream.continually(function(){return t})},Stream.makeOnes=function(){return new Stream(1,Stream.makeOnes)},Stream.makeNaturalNumbers=function(){return new Stream(1,function(){var t=Stream.makeNaturalNumbers();return t.add(Stream.makeOnes())})},Stream.make=function(){if(0==arguments.length)return new Stream;var t=Array.prototype.slice.call(arguments,1);return new Stream(arguments[0],function(){return Stream.make.apply(null,t)})},Stream.fromArray=function(t){return 0==t.length?new Stream:new Stream(t[0],function(){return Stream.fromArray(t.slice(1),Eager)},Eager)},Stream.range=function(t,e){return"undefined"==typeof t&&(t=1),t==e?Stream.make(t):new Stream(t,function(){return Stream.range(t+1,e)})},Stream.equals=function(t,e){return t instanceof Stream&&(e instanceof Stream&&(!(!t.empty()||!e.empty())||!t.empty()&&!e.empty()&&(t.head()===e.head()?Stream.equals(t.tail(),e.tail()):void 0)))},Stream.iterate=function(t,e){return _iterate(t,e,Lazy)},Stream.iterateEager=function(t,e){return _iterate(t,e,Eager)},Stream.cycle=function(t){var e=function(t,r){return r>=t.length&&(r=0),function(){return new Stream(t[r],e(t,r+1),Eager)}};return new Stream(t[0],e(t,1),Eager)},EagerStream.prototype=Stream.prototype,Stream.Eager=EagerStream,module.exports=Stream;
},{}]},{},[1])(1)
});
//# sourceMappingURL=stream.min.js.map