forked from mohsenasm/swarm-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtil.elm
63 lines (46 loc) · 1.29 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
57
58
59
60
61
62
63
module Util exposing (..)
import Dict exposing (Dict)
import Set exposing (Set)
complement : (a -> Bool) -> a -> Bool
complement fn =
\x -> not (fn x)
isJust : Maybe a -> Bool
isJust x =
Maybe.withDefault False (Maybe.map (\x -> True) x)
iff : Bool -> a -> a -> a
iff condition true false =
if condition then
true
else
false
groupBy : (a -> comparable) -> List a -> Dict comparable (List a)
groupBy key =
let
cons new =
Maybe.withDefault []
>> (::) new
>> Just
reducer item =
Dict.update (key item) (cons item)
in
List.foldl reducer Dict.empty
indexBy : (a -> comparable) -> List a -> Dict comparable a
indexBy key =
let
cons new =
Maybe.withDefault new
>> Just
reducer item =
Dict.update (key item) (cons item)
in
List.foldl reducer Dict.empty
unique : List comparable -> List comparable
unique =
Set.fromList >> Set.toList
indexedFoldl : (Int -> a -> b -> b) -> b -> List a -> b
indexedFoldl indexedReducer init list =
let
reducer item ( idx, accumulator ) =
( idx + 1, indexedReducer idx item accumulator )
in
list |> (List.foldl reducer ( 0, init ) >> Tuple.second)