/
Utilities.elm
executable file
·89 lines (64 loc) · 2.09 KB
/
Utilities.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
module Utilities (..) where
import List exposing (..)
import Dict as D
import Maybe as M
import Array as A
--Lists (Note: array is better at these things)
(!): List a -> Int -> Maybe a
li ! i = head (drop i li)
modifyAt: Int -> List a -> List a -> List a
modifyAt i li2 li = (take i li) ++ li2 ++ (drop (i+1) li)
modify:Int -> a -> List a -> List a
modify i x li = modifyAt i [x] li
getFirstNonzero : List Int -> Int
getFirstNonzero li = case li of
[] -> 0
x::rest -> if x/=0 then x else getFirstNonzero rest
listprod: List a -> List b -> List (a,b)
listprod l1 l2 = concat
(map (\x ->
(map (\y -> (x,y)) l2))
l1)
--array
getWithDefault: a -> Int -> A.Array a -> a
getWithDefault x i ar = M.withDefault x (A.get i ar)
--inclusive
range: Int -> Int -> List Int
range x y = if (x>y) then [] else x::(range (x+1) y)
--(!!): List Int -> Int -> Int
--li !! i = if (i<length li) then (li!i) else 0
--tuples
smult:Int -> (Int,Int)-> (Int,Int)
smult c (x,y) = (c*x,c*y)
--mapping
zip : List a -> List b -> List (a,b)
zip l1 l2 = map2 (,) l1 l2
zipMap : (a -> b -> c) -> List a -> List b -> List c
zipMap f al bl = map (\(x,y) -> f x y) (zip al bl)
--folding
foldlRot: (a -> b -> b) -> List a -> b -> b
foldlRot f x y = foldl f y x
while: (b -> Bool) -> b -> (b -> b) -> b
while f x0 update = if (f x0)
then (while f (update x0) update)
else x0
for : List a -> b -> (a -> b -> b) -> b
for li x0 f = foldl f x0 li
forA : A.Array a -> b -> (a -> b -> b) -> b
forA li x0 f = A.foldl f x0 li
--getting from dicts and lists
--def is default
getD:comparable -> a -> D.Dict comparable a -> a
getD str def d = M.withDefault def (D.get str d)
getA: Int -> A.Array a -> a
getA i arr =
case A.get i arr of
Just x -> x
getL: List a -> Int -> a -> a
getL li i def = M.withDefault def (li!i)
--if (i<length li) then (li!i) else def
--Maybe
maybe: b -> (a -> b) -> Maybe a -> b
maybe def f mx = case mx of
Nothing -> def
Just x -> f x