Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
504478e
commit d8c12a5
Showing
2 changed files
with
47 additions
and
4 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
let swap = (items, i, j) => { | ||
/* find the values that need to be swapped */ | ||
let i_item = List.nth(items, i); | ||
let j_item = List.nth(items, j); | ||
|
||
/* partial application of mapi to create swapper function */ | ||
let swapper = | ||
List.mapi((index, item) => | ||
switch (index) { | ||
| x when x == i => j_item | ||
| x when x == j => i_item | ||
| _ => item | ||
} | ||
); | ||
|
||
/* swap the items in the items list */ | ||
swapper(items); | ||
}; | ||
|
||
/* Fisher-Yates Shuffle | ||
https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle */ | ||
let shuffle = (items: list('a)) => { | ||
Random.init(int_of_float(Unix.time())); | ||
|
||
let len = List.length(items); | ||
let boxed_items = ref(items); | ||
|
||
for (i in len - 1 downto 1) { | ||
let j = Random.int(i); | ||
boxed_items := swap(boxed_items^, i, j); | ||
}; | ||
|
||
boxed_items^; | ||
}; |
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