-
Notifications
You must be signed in to change notification settings - Fork 1
/
Helpers.elm
63 lines (53 loc) · 1.45 KB
/
Helpers.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
57
58
59
60
61
62
63
module Helpers where
import Dict
import List exposing (..)
import Random exposing (..)
import Signal
import Graphics.Element exposing (..)
isJust : Maybe a -> Bool
isJust maybe =
case maybe of
Just something -> True
Nothing -> False
getOrFail : Maybe a -> a
getOrFail maybe =
case maybe of
Just something -> something
{- Unsafe List and Dict methods -}
headU l = getOrFail <| head l
tailU l = getOrFail <| tail l
maximumU l = getOrFail <| maximum l
minimumU l = getOrFail <| minimum l
getU key dict = getOrFail <| Dict.get key dict
(!!) : List a -> Int -> a
(!!) list idx = headU (drop idx list)
infixl 4 !!
tuple : a -> b -> (a, b)
tuple a b = (a, b)
without : Int -> List a -> List a
without i arr =
let before = take i arr
after = drop (i+1) arr
in
before ++ after
replaceAtIndex : Int -> a -> List a -> List a
replaceAtIndex i elt arr =
let before = take i arr
after = drop (i+1) arr
in
before ++ [elt] ++ after
shuffle : List a -> Seed -> List a
shuffle list seed =
if isEmpty list
then []
else
let generator = int 0 (length list - 1)
(i, newSeed) = generate generator seed
in
[list !! i] ++ shuffle (without i list) newSeed
filterOn : Signal a -> Signal Bool -> a -> Signal a
filterOn inputSignal conditionSignal default =
let joinedSignal = Signal.map2 tuple inputSignal conditionSignal
filteredSignal = Signal.filter snd (default, False) joinedSignal
in
Signal.map fst filteredSignal