Last active
May 31, 2017 22:46
-
-
Save ryanguill/62ff904d4500519e3144fc9564d2bce7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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