Skip to content

Commit

Permalink
zip and zipWithIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
kemsky committed Dec 3, 2015
1 parent 56007dc commit 26859a8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
28 changes: 26 additions & 2 deletions source/com/kemsky/Stream.as
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,37 @@ package com.kemsky
* @return A new list of lists created from the items and their corresponding indices.
* @internal immutable
*/
public function zip():Stream
public function zipWithIndex():Stream
{
var result:Array = [];
result.length = source.length;
for(var i:int = 0; i < source.length; i++)
{
result[i] = new Stream([i, source[i]]);
result[i] = new Stream([source[i], i]);
}
return new Stream(result);
}

/**
* Creates a new list of lists created from the items and their corresponding items from another stream.
* @example
* <pre>
* var s1:Stream = $(1, 2, 3);
* var s2:Stream = $(1, 2, 3);
* var z:Stream = s1.zip(s2);
* trace(z);
* //Stream{Stream{1, 1}, Stream{2, 2}, Stream{3, 3}}
* </pre>
* @return A new list of lists created from the items and their corresponding items from another strea.
* @internal immutable
*/
public function zip(stream:Stream):Stream
{
var result:Array = [];
var size:uint = Math.min(length, stream.length);
for(var i:int = 0; i < size; i++)
{
result[i] = Stream.of(getItem(i), stream.getItem(i));
}
return new Stream(result);
}
Expand Down
26 changes: 21 additions & 5 deletions testSrc/com/kemsky/TestStream.as
Original file line number Diff line number Diff line change
Expand Up @@ -248,17 +248,33 @@ package com.kemsky

[Test]
public function testZip():void
{
var s1:Stream = $("1", "2");
var s2:Stream = $(1, 2);
var e:Stream = s1.zip(s2);
assertEquals(e.length, s1.length);
assertTrue(e.first is Stream);
assertTrue(e.second is Stream);
assertEquals(e.first.length, 2);
assertEquals(e.first.second, 1);
assertEquals(e.first.first, "1");
assertEquals(e.second.second, 2);
assertEquals(e.second.first, "2");
}

[Test]
public function testZipWithIndex():void
{
var s:Stream = $("1", "2");
var e:Stream = s.zip();
var e:Stream = s.zipWithIndex();
assertEquals(e.length, s.length);
assertTrue(e.first is Stream);
assertTrue(e.second is Stream);
assertEquals(e.first.length, 2);
assertEquals(e.first.first, 0);
assertEquals(e.first.second, "1");
assertEquals(e.second.first, 1);
assertEquals(e.second.second, "2");
assertEquals(e.first.second, 0);
assertEquals(e.first.first, "1");
assertEquals(e.second.second, 1);
assertEquals(e.second.first, "2");
}

[Test]
Expand Down

0 comments on commit 26859a8

Please sign in to comment.