Skip to content

Instantly share code, notes, and snippets.

@mikeacjones
Last active January 6, 2021 16:40
Show Gist options
  • Save mikeacjones/84d559ba4a18eb93e58f0026697b2089 to your computer and use it in GitHub Desktop.
Save mikeacjones/84d559ba4a18eb93e58f0026697b2089 to your computer and use it in GitHub Desktop.
Function to shuffle an array
%dw 2.0
fun splice(arr: Array, index: Number) =
if (index == 0) arr[1 to -1]
else (arr[0 to index-1] default []) ++ (arr[index+1 to -1] default [])
fun shuffle(arr: Array) =
(((0 to sizeOf(arr)-1) as Array) reduce (i, s = {res:[], items: arr}) -> do {
var ri = floor(random() * sizeOf(s.items))
---
{
res: s.res << s.items[ri],
items: s.items splice ri
}
}).res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment