-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHashMap.idr
More file actions
152 lines (131 loc) · 4.11 KB
/
Copy pathHashMap.idr
File metadata and controls
152 lines (131 loc) · 4.11 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
module Data.HashMap
import Data.HashMap.Internal
import Data.HashMap.Internal.HAMT
import public Data.Hashable
--------------------------------------------------------------------------------
-- Creating HashMaps
--------------------------------------------------------------------------------
||| Constructs an empty `HashMap`.
export
empty : Hashable key
=> Eq key
=> HashMap key val
empty =
Empty
--------------------------------------------------------------------------------
-- Lookups
--------------------------------------------------------------------------------
||| Looks up a key in the `HashMap`.
||| Returns `Just val` if found or `Nothing` if the key is absent.
export
lookup : key
-> HashMap key val
-> Maybe val
lookup key Empty =
Nothing
lookup key (Trie hamt) =
let lookup' = lookup (==)
key
hamt
in case lookup' of
Nothing =>
Nothing
Just (_ ** val') =>
Just val'
--------------------------------------------------------------------------------
-- Insertion
--------------------------------------------------------------------------------
||| Inserts a key–value pair into the `HashMap`.
||| Replaces any existing value for the key.
export
insert : key
-> val
-> HashMap key val
-> HashMap key val
insert key val Empty =
Trie ( singleton key val
)
insert key val (Trie hamt) =
Trie ( insert (==) key val hamt
)
--------------------------------------------------------------------------------
-- Deletion
--------------------------------------------------------------------------------
||| Deletes a key from the `HashMap`.
||| If the key is not present, returns the map unchanged.
export
delete : key
-> HashMap key val
-> HashMap key val
delete key Empty =
Empty
delete key (Trie hamt) =
case delete (==) key hamt of
Nothing =>
Empty
Just hamt' =>
Trie hamt'
--------------------------------------------------------------------------------
-- Maps/Folds
--------------------------------------------------------------------------------
||| Folds over all key–value pairs in the `HashMap`,
||| combining them with an accumulator.
export
foldWithKey : (f : k -> v -> acc -> acc)
-> acc
-> HashMap k v
-> acc
foldWithKey f z Empty =
z
foldWithKey f z (Trie hamt) =
foldWithKey f
z
hamt
--------------------------------------------------------------------------------
-- Creating Lists from HashMaps
--------------------------------------------------------------------------------
||| Converts the `HashMap` to a list of key–value pairs.
export
toList : HashMap k v
-> List (k, v)
toList hm =
foldWithKey (\key, val, acc => (key, val) :: acc)
[]
hm
--------------------------------------------------------------------------------
-- Creating HashMaps from Lists
--------------------------------------------------------------------------------
||| Constructs a `HashMap` from a list of key–value pairs.
export
fromList : Hashable k
=> Eq k
=> List (k, v)
-> HashMap k v
fromList xs =
foldr (\(k, v) => insert k v)
empty
xs
--------------------------------------------------------------------------------
-- Keys
--------------------------------------------------------------------------------
||| Returns a list of all keys in the `HashMap`.
export
keys : HashMap k v
-> List k
keys hm =
foldWithKey (\key, val, acc => key :: acc)
[]
hm
--------------------------------------------------------------------------------
-- Interfaces
--------------------------------------------------------------------------------
export
Functor (HashMap key) where
map f Empty =
Empty
map f (Trie hamt) =
Trie ( trieMap f hamt
)
export
Show key => Show val => Show (HashMap key val) where
show hm = "fromList \{show $ HashMap.toList hm}"