From 4c22053e12024e4cb6a68e25660e1c8352ebd830 Mon Sep 17 00:00:00 2001 From: Alexander Date: Fri, 12 Feb 2016 15:51:27 +0300 Subject: [PATCH] add substream --- source/com/kemsky/Stream.as | 60 ++++++++++++++++++++++++++++++++ testSrc/com/kemsky/TestStream.as | 50 ++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) diff --git a/source/com/kemsky/Stream.as b/source/com/kemsky/Stream.as index 3d09da0..4e7f66f 100644 --- a/source/com/kemsky/Stream.as +++ b/source/com/kemsky/Stream.as @@ -103,11 +103,64 @@ package com.kemsky return new Iterator(this); } + /** + * Extracts sub stream from current stream. + * @param start index + * @param length size + * @return new sub stream + * @example + *
+         *     var s:Stream = new Stream([1, 2, 3]);
+         *     var sub:Stream = s.substream(1, 2);
+         *     trace(sub);
+         *     //Stream{2, 3}
+         * 
+ * @internal immutable + */ + public function substream(start:int = 0, length:uint = 0):Stream + { + var from:int; + if(start < 0) + { + from = source.length + start; + } + else + { + from = start; + } + + if(from < 0 || from >= source.length) + { + throw new StreamError("Start index is greater than stream length: " + from); + } + + var to:uint = from + (length == 0 ? source.length - from : length); + if(to > source.length) + { + throw new StreamError("Last index is greater than stream length: " + to); + } + + var result:Array = []; + var index:uint = 0; + for(var i:uint = from; i < to; i++) + { + result[index++] = source[i]; + } + return new Stream(result); + } + /** * Finds item which has maximum value (or callback(item) has maximum value) * @param callback an optional function to apply to each item * @param defaultValue value returned in case stream is empty * @return item with maximum value + * @example + *
+         *     var s:Stream = new Stream([1, 2, 3]);
+         *     var n:Number = s.max();
+         *     trace(n);
+         *     //3
+         * 
* @internal immutable */ public function max(callback:Function = null, defaultValue:* = undefined):* @@ -150,6 +203,13 @@ package com.kemsky * @param callback an optional function to apply to each item * @param defaultValue value returned in case stream is empty * @return item with minimum value + * @example + *
+         *     var s:Stream = new Stream([1, 2, 3]);
+         *     var n:Number = s.min();
+         *     trace(n);
+         *     //1
+         * 
* @internal immutable */ public function min(callback:Function = null, defaultValue:* = undefined):* diff --git a/testSrc/com/kemsky/TestStream.as b/testSrc/com/kemsky/TestStream.as index 204f3f4..5061b7f 100644 --- a/testSrc/com/kemsky/TestStream.as +++ b/testSrc/com/kemsky/TestStream.as @@ -40,6 +40,56 @@ package com.kemsky } + [Test] + public function testSubstream():void + { + var s:Stream = new Stream([1, 2, 3]); + + var full:Stream = s.substream(); + assertEquals(full.length, s.length); + assertEquals(full.first, s.first); + assertEquals(full.second, s.second); + assertEquals(full.third, s.third); + + var f:Stream = s.substream(0, 1); + assertEquals(f.length, 1); + assertEquals(f.first, s.first); + + var l:Stream = s.substream(2, 1); + assertEquals(l.length, 1); + assertEquals(l.first, s.third); + + var rev:Stream = s.substream(-3); + assertEquals(rev.length, s.length); + assertEquals(rev.first, s.first); + assertEquals(rev.second, s.second); + assertEquals(rev.third, s.third); + + try + { + s.substream(4, 1); + assertTrue(false); + } catch(e:Error) + { + } + + try + { + s.substream(0, 100); + assertTrue(false); + } catch(e:Error) + { + } + + try + { + s.substream(-4, 1); + assertTrue(false); + } catch(e:Error) + { + } + } + [Test] public function testMXML():void {