Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

_unique() collection function implementation.

  • Loading branch information...
commit b3c083405bb3422fe5a9083c3a341978dfd3ef69 1 parent 6d59d18
@markmandel markmandel authored
View
7 README.md
@@ -58,6 +58,13 @@ Take an array/struct and group the data into a grouped struct. The closure shoul
* data - the array / struct
* grouping - the closure to return the group key.
+### _unique(any data, [function comparator]) : any ###
+
+Returns an array with all the duplicates removed (i.e. unique). For structs, this iterates through all the values, and returns an array from that, with duplicates removed.
+
+* data - the array/struct
+* comparator - optional comparator closure that takes 2 arguments for comparing of objects, and returns 0 if they are the same. If not supplied, then natural comparison will be used.
+
## Functions ##
Functions that allow you to manipulate other functions / closures
View
49 sesame/collections.cfm
@@ -127,4 +127,53 @@
return collection;
}
+ /**
+ * Returns an array with all the duplicates removed (i.e. unique). For structs, this iterates through all the
+ * values, and returns an array from that, with duplicates removed.
+ *
+ * @data the array/struct
+ * @comparator optional comparator closure that takes 2 arguments for comparing of objects, and returns 0 if they are the same. If not supplied, then natural comparison will be used.
+ */
+ public any function _unique(required any data, function comparator)
+ {
+ var collection = [];
+ if(isStruct(arguments.data))
+ {
+ var array = [];
+ array.addAll(arguments.data.values());
+ arguments.data = array;
+ }
+
+ if(structKeyExists(arguments, "comparator"))
+ {
+ var _comparator = arguments.comparator;
+
+ while(!arrayIsEmpty(arguments.data))
+ {
+ var item = arguments.data.remove(JavaCast("int", 0));
+ arrayAppend(collection, item);
+
+ var find = function(it)
+ {
+ if(_comparator(it, item) != 0)
+ {
+ return true;
+ }
+ return false;
+ };
+
+ arguments.data = ArrayFilter(arguments.data, find);
+ }
+ }
+ else
+ {
+ var set = createObject("java", "java.util.LinkedHashSet").init(ArrayLen(arguments.data));
+ ArrayEach(arguments.data, function(it) { set.add(it); } );
+
+ collection.addAll(set);
+ }
+
+ return collection;
+ }
+
</cfscript>
View
79 tests/cases/CollectionsTest.cfc
@@ -101,4 +101,83 @@ component extends="tests.AbstractTestCase"
assertEquals({1={a=1, c=3, e=5}, 0={b=2, d=4, f=6}}, grouped);
}
+
+ /**
+ * test unique collections, no comparatpr
+ */
+ public void function testUniqueNoComparatorArray()
+ {
+ var data = [1, 2, 3, 3, 4, 5, 1, 2, 9];
+ var expected = [1, 2, 3, 4, 5, 9];
+
+ var unique = _unique(data);
+
+ assertEquals(expected, unique);
+ }
+
+ /**
+ * test unique collections, no comparatpr
+ */
+ public void function testUniqueNoComparatorStruct()
+ {
+ var data = {a=1, b=2, c=3, d=3, e=4, f=5, g=1, h=2, i=9};
+ var expected = [1, 2, 3, 4, 5, 9];
+
+ var unique = _unique(data);
+ ArraySort(unique, "numeric");
+
+ assertEquals(expected, unique);
+ }
+
+ /**
+ * test unique collections with a comparator
+ */
+ public void function testUniqueComparatorArray()
+ {
+ var data = [2, 3, 3, 2, 4, 5, 6, 4, 6, 3];
+ var expected = [2, 3, 4, 5, 6];
+
+ var comparator = function(a, b)
+ {
+ if(a == b) { return 0; }
+ return 1;
+ };
+
+ assertEquals(0, comparator(1, 1));
+ assertEquals(1, comparator(2, 1));
+
+ var unique = _unique(data, comparator);
+
+ debug(unique);
+ debug(expected);
+
+ assertEquals(expected, unique);
+ }
+
+ /**
+ * test unique collections with a comparator
+ */
+ public void function testUniqueComparatorStruct()
+ {
+ var data = {a=2, b=3, c=3, d=2, e=4, f=5, g=6, h=4, i=6, j=3};
+ var expected = [2, 3, 4, 5, 6];
+
+ var comparator = function(a, b)
+ {
+ if(a == b) { return 0; }
+ return 1;
+ };
+
+ assertEquals(0, comparator(1, 1));
+ assertEquals(1, comparator(2, 1));
+
+ var unique = _unique(data, comparator);
+
+ arraySort(unique, "numeric");
+
+ debug(unique);
+ debug(expected);
+
+ assertEquals(expected, unique);
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.