Skip to content

Commit

Permalink
add substream
Browse files Browse the repository at this point in the history
  • Loading branch information
kemsky committed Feb 12, 2016
1 parent 1fa4441 commit 4c22053
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
60 changes: 60 additions & 0 deletions source/com/kemsky/Stream.as
Original file line number Diff line number Diff line change
Expand Up @@ -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
* <pre>
* var s:Stream = new Stream([1, 2, 3]);
* var sub:Stream = s.substream(1, 2);
* trace(sub);
* //Stream{2, 3}
* </pre>
* @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
* <pre>
* var s:Stream = new Stream([1, 2, 3]);
* var n:Number = s.max();
* trace(n);
* //3
* </pre>
* @internal immutable
*/
public function max(callback:Function = null, defaultValue:* = undefined):*
Expand Down Expand Up @@ -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
* <pre>
* var s:Stream = new Stream([1, 2, 3]);
* var n:Number = s.min();
* trace(n);
* //1
* </pre>
* @internal immutable
*/
public function min(callback:Function = null, defaultValue:* = undefined):*
Expand Down
50 changes: 50 additions & 0 deletions testSrc/com/kemsky/TestStream.as
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 4c22053

Please sign in to comment.