Last active
January 6, 2021 16:40
-
-
Save mikeacjones/84d559ba4a18eb93e58f0026697b2089 to your computer and use it in GitHub Desktop.
Function to shuffle an array
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
%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