Skip to content

Instantly share code, notes, and snippets.

@ryanguill
Last active May 31, 2017 22:46
Show Gist options
  • Save ryanguill/62ff904d4500519e3144fc9564d2bce7 to your computer and use it in GitHub Desktop.
Save ryanguill/62ff904d4500519e3144fc9564d2bce7 to your computer and use it in GitHub Desktop.
<cfscript>
function arrayDistinct (required array data) {
var output = arrayNew(1);
output.addAll(createObject("java", "java.util.HashSet").init(arguments.data));
return output;
}
data = [1,2,3,4,5,4,3,2,1];
result = arrayDistinct(data);
assertTrue(isArray(result), "returns an array");
assertTrue(arrayLen(result) == 5, "removes duplicates");
arraySort(result, "numeric"); //you can use it like a CFML array
assertTrue(arrayToList(result) == "1,2,3,4,5", "returns expected data");
//utility function
function assertTrue (required boolean condition, string message = "") {
if (!condition) {
writeoutput('<pre style="font-weight:bold; color:red;">Fail! #message#</pre>');
} else {
writeoutput('<pre style="color:green;">Pass: #message#</pre>');
}
}
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment