Skip to content

Commit

Permalink
deduplicate
Browse files Browse the repository at this point in the history
  • Loading branch information
kemsky committed Nov 17, 2015
1 parent 59a7be3 commit 5858d4b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
27 changes: 17 additions & 10 deletions source/com/kemsky/Stream.as
Original file line number Diff line number Diff line change
Expand Up @@ -1328,7 +1328,7 @@ package com.kemsky

/**
* Create new stream from original by removing duplicate items.
* @param callback is compare function <i>function(a:*, b:*):int</i>.
* @param callback is compare function <i>function(a:*, b:*):Boolean</i>.
* @return new stream without duplicate values.
* @example
* <pre>
Expand All @@ -1340,23 +1340,30 @@ package com.kemsky
*/
public function deduplicate(callback:Function = null):Stream
{
var result:Stream;
if(callback == null)
var result:Stream = this.concat();
var i:int;
var j:int;

if(callback != null)
{
var map:Dictionary = this.dictionary();
result = Stream.from(map);
for(i = 0; i < length - 1; i++)
{
for(j = i + 1; j < length; j++)
{
if(callback(result.getItem(i), result.getItem(j)))
{
result.removeItem(j);
}
}
}
}
else
{
//todo
result = this.concat();
var i:int;
var j:int;
for(i = 0; i < length - 1; i++)
{
for(j = i + 1; j < length; j++)
{
if(callback(result.getItem(i), result.getItem(j)) == 0)
if(result.getItem(i) === result.getItem(j))
{
result.removeItem(j);
}
Expand Down
3 changes: 2 additions & 1 deletion source/com/kemsky/filters/mapped.as
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ package com.kemsky.filters
return function (item:*):Boolean
{
var v:* = toValue(item, val1);
return v is String ? map.hasOwnProperty(v): !(map[v] == null || map[v] === undefined);
var value:Object = map[v];
return !(value == null);
};
}
}
16 changes: 14 additions & 2 deletions testSrc/com/kemsky/TestStream.as
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,22 @@ package com.kemsky
[Test]
public function testDeduplicate():void
{
var s:Stream = $(1, 2, 4, 3, 2, 1);
var s:Stream = $(1, 2, 2, 3, 1);

var d1:Stream = s.deduplicate();
//assertEquals(d1.length, 4);
assertEquals(d1.length, 3);
assertEquals(d1.first, 1);
assertEquals(d1.second, 2);
assertEquals(d1.third, 3);

var s1:Stream = $("test", "you", "now");
var d2:Stream = s1.deduplicate(function(a:String, b:String):Boolean
{
return a.length == b.length;
});
assertEquals(d2.length, 2);
assertEquals(d2.first, "test");
assertEquals(d2.second, "you");
}

[Test]
Expand Down

0 comments on commit 5858d4b

Please sign in to comment.