-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDict.ark
More file actions
317 lines (295 loc) · 9.4 KB
/
Dict.ark
File metadata and controls
317 lines (295 loc) · 9.4 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# @brief Get a value from a given dictionary using a key, or nil if it doesn't exist
# @param _D dictionary
# @param _key key to get
# =begin
# (let data (dict "key" "value"))
# (print (dict:get data "key")) # value
# =end
# @author https://github.com/SuperFola
(let get (fun (_D _key) (builtin__dict:get _D _key)))
# @brief Adds or replaces an entry to a dictionary, given a (key, value) pair
# @details The dictionary is modified in place
# @param _D dictionary
# @param _key key to add (or replace)
# @param _value value for the given key
# =begin
# (let data (dict "key" "value"))
# (dict:add data "hello" "world")
# (dict:add data "key" "hole") # key:value will be replaced by key:hole
# (print data) # {key: hole, hello: world}
# =end
# @author https://github.com/SuperFola
(let add (fun (_D _key _value) (builtin__dict:add _D _key _value)))
# @brief Checks if the dictionary has a given key
# @param _D dictionary
# @param _key key to check for its presence in the dict
# =begin
# (let data (dict "key" "value"))
# (print (dict:contains? data "key")) # true
# (print (dict:contains? data "test")) # false
# =end
# @author https://github.com/SuperFola
(let contains? (fun (_D _key) (builtin__dict:contains _D _key)))
# @brief Get a value from a given dictionary using a key, or a default value if it doesn't exist
# @param _D dictionary
# @param _key key to get
# @param _default default value in case the key isn't in the dictionary
# =begin
# (let data (dict "key" "value"))
# (print (dict:getOrElse data "key" "hello")) # value
# (print (dict:getOrElse data "count" 5)) # 5
# =end
# @author https://github.com/SuperFola
(let getOrElse (fun (_D _key _default)
(if (contains? _D _key)
(get _D _key)
_default)))
# @brief Updates an entry or create it from a default value
# @details The dictionary is modified in place
# @param _D dictionary
# @param _key key to create or update
# @param _f function called with the existing value, returning an updated value
# @param _default default value to use if the key doesn't exist
# =begin
# (let data (dict "count" 0))
# (dict:updateOrDefault data "count" (fun (c) (+ c 1)) 1) # count = 1
# =end
# @author https://github.com/SuperFola
(let updateOrDefault (fun (_D _key _f _default)
(if (contains? _D _key)
(add _D _key (_f (get _D _key)))
(add _D _key _default))))
# @brief Deletes an entry from a dictionary, given a key
# @details The dictionary is modified in place
# @param _D dictionary
# @param _key key to delete
# =begin
# (let data (dict "key" "value"))
# (dict:remove data "key")
# (print (dict:get data "key")) # nil
# =end
# @author https://github.com/SuperFola
(let remove (fun (_D _key) (builtin__dict:remove _D _key)))
# @brief Returns a list of the keys of a dictionary
# @details Keys are returned in the order they were added, unless a removal occurred
# @param _D dictionary
# =begin
# (let data (dict "key" "value" 5 12))
# (print (dict:keys data)) # [key, 5]
# =end
# @author https://github.com/SuperFola
(let keys (fun (_D) (builtin__dict:keys _D)))
# @brief Returns a list of the values of a dictionary
# @param _D dictionary
# =begin
# (let data (dict "key" "value" 5 12))
# (print (dict:values data)) # [value, 12]
# =end
# @author https://github.com/SuperFola
(let values (fun (_D) {
(mut _output [])
(let _keys (keys _D))
(mut _i 0)
(while (< _i (len _keys)) {
(append! _output (get _D (@ _keys _i)))
(set _i (+ 1 _i)) })
_output }))
# @brief Returns a list of the entries of a dictionary
# @param _D dictionary
# =begin
# (let data (dict "key" "value" 5 12))
# (print (dict:entries data)) # [[key, value], [5, 12]]
# =end
# @author https://github.com/SuperFola
(let entries (fun (_D) {
(mut _output [])
(let _keys (keys _D))
(mut _i 0)
(while (< _i (len _keys)) {
(let key (@ _keys _i))
(let val (get _D key))
(append! _output [key val])
(set _i (+ 1 _i)) })
_output }))
# @brief Map each value in a dictionary with a given function
# @details The original dictionary is not modified
# @param _D dictionary
# @param _f function to apply to each value, taking both key and value as arguments
# =begin
# (let data (dict "key" "value"))
# (let new (dict:map data (fun (key value) (format "{}-{}" key value))))
# (print data) # {key: value}
# (print new) # {key: key-value}
# =end
# @author https://github.com/SuperFola
(let map (fun (_D _f) {
(let _new (dict))
(mut _i 0)
(let _keys (keys _D))
(while (< _i (len _keys)) {
(let _key (@ _keys _i))
(add _new _key (_f _key (get _D _key)))
(set _i (+ 1 _i)) })
_new }))
# @brief Map each value in a dictionary with a given function
# @details The original dictionary is updated in place
# @param _D dictionary
# @param _f function to apply to each value, taking both key and value as arguments
# =begin
# (let data (dict "key" "value"))
# (dict:map! data (fun (key value) (format "{}-{}" key value)))
# (print data) # {key: key-value}
# =end
# @author https://github.com/SuperFola
(let map! (fun (_D _f) {
(mut _i 0)
(let _keys (keys _D))
(while (< _i (len _keys)) {
(let _key (@ _keys _i))
(add _D _key (_f _key (get _D _key)))
(set _i (+ 1 _i)) }) }))
# @brief Iterate over the pairs of a dictionary with a given function
# @param _D dictionary
# @param _f function to call on each pair
# =begin
# (let data (dict "key" "value" 5 12))
# (dict:forEach data (fun (key value) (print (format "{}-{}" key value))))
# # key-value
# # 5-12
# =end
# @author https://github.com/SuperFola
(let forEach (fun (_D _f) {
(mut _i 0)
(let _keys (keys _D))
(while (< _i (len _keys)) {
(let _key (@ _keys _i))
(_f _key (get _D _key))
(set _i (+ 1 _i)) }) }))
# @brief Filter a dictionary with a predicate
# @details The original dictionary is not modified
# @param _D dictionary
# @param _f predicate, taking both key and value as arguments
# =begin
# (let data (dict "key" "value" "hello" "world"))
# (let new (dict:filter data (fun (key value) (> (len key) 3))))
# (print data) # {key: value, hello: world}
# (print new) # {hello: world}
# =end
# @author https://github.com/SuperFola
(let filter (fun (_D _f) {
(let _new (dict))
(mut _i 0)
(let _keys (keys _D))
(while (< _i (len _keys)) {
(let _key (@ _keys _i))
(let _val (get _D _key))
(if (_f _key _val) (add _new _key _val))
(set _i (+ 1 _i)) })
_new }))
# @brief Filter a dictionary with a predicate
# @details The original dictionary is updated in place
# @param _D dictionary
# @param _f predicate, taking both key and value as arguments
# =begin
# (let data (dict "key" "value" "hello" "world"))
# (dict:filter! data (fun (key value) (> (len key) 3)))
# (print data) # {hello: world}
# =end
# @author https://github.com/SuperFola
(let filter! (fun (_D _f) {
(mut _i 0)
(let _keys (keys _D))
(while (< _i (len _keys)) {
(let _key (@ _keys _i))
(let _val (get _D _key))
(if (not (_f _key _val)) (remove _D _key))
(set _i (+ 1 _i)) }) }))
# @brief Copy a dictionary
# @details The original dictionary is not modified
# @param _D dictionary to copy
# =begin
# (let data (dict "key" "value" "hello" "world"))
# (let new (dict:copy data))
# (dict:add data "test" 12)
# (print data) # {key: value, hello: world, test: 12}
# (print new) # {key: value, hello: world}
# =end
# @author https://github.com/SuperFola
(let copy (fun (_D) {
(let _new (dict))
(let _keys (keys _D))
(mut _i 0)
(while (< _i (len _keys)) {
(let _key (@ _keys _i))
(add _new _key (get _D _key))
(set _i (+ 1 _i)) })
_new }))
# @brief Update a dictionary with (key, value) pairs from a second dictionary
# @details The original dictionary is updated in place
# @param _D dictionary to update
# @param _D2 second dictionary
# =begin
# (let data (dict "key" "value" "hello" "world"))
# (let new (dict "key" "new value" 5 12))
# (dict:update! data new)
# (print data) # {key: new value, hello: world, 5: 12}
# =end
# @author https://github.com/SuperFola
(let update! (fun (_D _D2) {
(mut _i 0)
(let _keys (keys _D2))
(while (< _i (len _keys)) {
(let _key (@ _keys _i))
(add _D _key (get _D2 _key))
(set _i (+ 1 _i)) }) }))
(let _valAsJson (fun (_val)
(if (= "Dict" (type _val))
(asJson _val)
(if (= "String" (type _val))
(+ "\"" _val "\"")
(if (= "List" (type _val))
(_listAsJson _val)
(if (nil? _val)
"null"
(toString _val)))))))
(let _listAsJson (fun ((ref _L)) {
(mut _output "[")
(mut _i 0)
(while (< _i (len _L)) {
(set _output (+
_output
(_valAsJson (@ _L _i))
(if (= (len _L) (+ 1 _i))
""
", ")))
(set _i (+ 1 _i)) })
(+ _output "]") }))
# @brief Convert a given dictionary to its JSON representation
# @details The original dictionary is not modified
# @param _D dictionary to convert to JSON
# =begin
# (let data (dict "key" "value" "hello" nil "a" [1 true]))
# (let new (dict "key" "new value" 5 12))
# (print (dict:asJson data new)) # {"key": "value", "hello": null, "a": [1, true]}
# =end
# @author https://github.com/SuperFola
(let asJson (fun (_D) {
(mut _output [])
(let _keys (keys _D))
(mut _i 0)
(while (< _i (len _keys)) {
(let _key (@ _keys _i))
(let _val (_valAsJson (get _D _key)))
(append! _output (format "\"{}\": {}" _key _val))
(set _i (+ 1 _i)) })
(mut _as_str "")
(set _i 0)
(while (< _i (len _output)) {
(set _as_str (+
_as_str
(@ _output _i)
(if (= (len _output) (+ 1 _i))
""
", ")))
(set _i (+ 1 _i)) })
(+ "{" _as_str "}") }))