-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.elm
56 lines (43 loc) · 1.37 KB
/
util.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
module Util exposing (..)
import Array exposing (..)
shuffle : List Float -> Array a -> Array a
shuffle randomFloats array =
if List.isEmpty randomFloats || isEmpty array then
empty
else
let
(result, newRandomFloats, newArray) =
let
index = (unsafeHead randomFloats) * (toFloat <| length array) |> floor
(result, newArray) = extract index array
in
(result, List.drop 1 randomFloats, newArray)
in
push result <| shuffle newRandomFloats newArray
extract : Int -> Array a -> (a, Array a)
extract index array =
if index >= 0 && index < (length array) then
let
prefix = slice 0 index array
element = unsafeUnwrap <| get index array
suffix = slice (index + 1) (length array) array
in
(element, append prefix suffix)
else
Debug.crash "Index out of range"
unsafeHead : List a -> a
unsafeHead list =
case List.head list of
Just something ->
something
Nothing ->
Debug.crash "Empty array"
unsafeUnwrap : Maybe a -> a
unsafeUnwrap maybe =
case maybe of
Just something ->
something
Nothing ->
Debug.crash "Maybe turned out to be nothing"
id : a -> () -> a
id a = \_ -> a