This repository was archived by the owner on Mar 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 310
Expand file tree
/
Copy pathDictionary.swift
More file actions
421 lines (312 loc) · 10.4 KB
/
Dictionary.swift
File metadata and controls
421 lines (312 loc) · 10.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//
// Dictionary.swift
// ExSwift
//
// Created by pNre on 04/06/14.
// Copyright (c) 2014 pNre. All rights reserved.
//
import Foundation
import Swift
internal extension Dictionary {
/**
Difference of self and the input dictionaries.
Two dictionaries are considered equal if they contain the same [key: value] pairs.
:param: dictionaries Dictionaries to subtract
:returns: Difference of self and the input dictionaries
*/
func difference <V: Equatable> (dictionaries: [Key: V]...) -> [Key: V] {
var result = [Key: V]()
each {
if let item = $1 as? V {
result[$0] = item
}
}
// Difference
for dictionary in dictionaries {
for (key, value) in dictionary {
if result.has(key) && result[key] == value {
result.removeValueForKey(key)
}
}
}
return result
}
/**
Union of self and the input dictionaries.
:param: dictionaries Dictionaries to join
:returns: Union of self and the input dictionaries
*/
func union (dictionaries: Dictionary...) -> Dictionary {
var result = self
dictionaries.each { (dictionary) -> Void in
dictionary.each { (key, value) -> Void in
_ = result.updateValue(value, forKey: key)
}
}
return result
}
/**
Intersection of self and the input dictionaries.
Two dictionaries are considered equal if they contain the same [key: value] copules.
:param: values Dictionaries to intersect
:returns: Dictionary of [key: value] couples contained in all the dictionaries and self
*/
func intersection <K, V where K: Equatable, V: Equatable> (dictionaries: [K: V]...) -> [K: V] {
// Casts self from [Key: Value] to [K: V]
let filtered = mapFilter { (item, value) -> (K, V)? in
if (item is K) && (value is V) {
return (item as! K, value as! V)
}
return nil
}
// Intersection
return filtered.filter({ (key: K, value: V) -> Bool in
// check for [key: value] in all the dictionaries
dictionaries.all { $0.has(key) && $0[key] == value }
})
}
/**
Checks if a key exists in the dictionary.
:param: key Key to check
:returns: true if the key exists
*/
func has (key: Key) -> Bool {
return indexForKey(key) != nil
}
/**
Creates an Array with values generated by running
each [key: value] of self through the mapFunction.
:param: mapFunction
:returns: Mapped array
*/
func toArray <V> (map: (Key, Value) -> V) -> [V] {
var mapped = [V]()
each {
mapped.append(map($0, $1))
}
return mapped
}
/**
Creates a Dictionary with the same keys as self and values generated by running
each [key: value] of self through the mapFunction.
:param: mapFunction
:returns: Mapped dictionary
*/
func mapValues <V> (map: (Key, Value) -> V) -> [Key: V] {
var mapped = [Key: V]()
each {
mapped[$0] = map($0, $1)
}
return mapped
}
/**
Creates a Dictionary with the same keys as self and values generated by running
each [key: value] of self through the mapFunction discarding nil return values.
:param: mapFunction
:returns: Mapped dictionary
*/
func mapFilterValues <V> (map: (Key, Value) -> V?) -> [Key: V] {
var mapped = [Key: V]()
each {
if let value = map($0, $1) {
mapped[$0] = value
}
}
return mapped
}
/**
Creates a Dictionary with keys and values generated by running
each [key: value] of self through the mapFunction discarding nil return values.
:param: mapFunction
:returns: Mapped dictionary
*/
func mapFilter <K, V> (map: (Key, Value) -> (K, V)?) -> [K: V] {
var mapped = [K: V]()
each {
if let value = map($0, $1) {
mapped[value.0] = value.1
}
}
return mapped
}
/**
Creates a Dictionary with keys and values generated by running
each [key: value] of self through the mapFunction.
:param: mapFunction
:returns: Mapped dictionary
*/
func map <K, V> (map: (Key, Value) -> (K, V)) -> [K: V] {
var mapped = [K: V]()
self.each({
let (_key, _value) = map($0, $1)
mapped[_key] = _value
})
return mapped
}
/**
Loops trough each [key: value] pair in self.
:param: eachFunction Function to inovke on each loop
*/
func each (each: (Key, Value) -> ()) {
for (key, value) in self {
each(key, value)
}
}
/**
Constructs a dictionary containing every [key: value] pair from self
for which testFunction evaluates to true.
:param: testFunction Function called to test each key, value
:returns: Filtered dictionary
*/
func filter (test: (Key, Value) -> Bool) -> Dictionary {
var result = Dictionary()
for (key, value) in self {
if test(key, value) {
result[key] = value
}
}
return result
}
/**
Creates a dictionary composed of keys generated from the results of
running each element of self through groupingFunction. The corresponding
value of each key is an array of the elements responsible for generating the key.
:param: groupingFunction
:returns: Grouped dictionary
*/
func groupBy <T> (group: (Key, Value) -> T) -> [T: [Value]] {
var result = [T: [Value]]()
for (key, value) in self {
let groupKey = group(key, value)
// If element has already been added to dictionary, append to it. If not, create one.
if result.has(groupKey) {
result[groupKey]! += [value]
} else {
result[groupKey] = [value]
}
}
return result
}
/**
Similar to groupBy. Doesn't return a list of values, but the number of values for each group.
:param: groupingFunction Function called to define the grouping key
:returns: Grouped dictionary
*/
func countBy <T> (group: (Key, Value) -> (T)) -> [T: Int] {
var result = [T: Int]()
for (key, value) in self {
let groupKey = group(key, value)
// If element has already been added to dictionary, append to it. If not, create one.
if result.has(groupKey) {
result[groupKey]!++
} else {
result[groupKey] = 1
}
}
return result
}
/**
Checks if test evaluates true for all the elements in self.
:param: test Function to call for each element
:returns: true if test returns true for all the elements in self
*/
func all (test: (Key, Value) -> (Bool)) -> Bool {
for (key, value) in self {
if !test(key, value) {
return false
}
}
return true
}
/**
Checks if test evaluates true for any element of self.
:param: test Function to call for each element
:returns: true if test returns true for any element of self
*/
func any (test: (Key, Value) -> (Bool)) -> Bool {
for (key, value) in self {
if test(key, value) {
return true
}
}
return false
}
/**
Returns the number of elements which meet the condition
:param: test Function to call for each element
:returns: the number of elements meeting the condition
*/
func countWhere (test: (Key, Value) -> (Bool)) -> Int {
var result = 0
for (key, value) in self {
if test(key, value) {
result++
}
}
return result
}
/**
Recombines the [key: value] couples in self trough combine using initial as initial value.
:param: initial Initial value
:param: combine Function that reduces the dictionary
:returns: Resulting value
*/
func reduce <U> (initial: U, combine: (U, Element) -> U) -> U {
return Swift.reduce(self, initial, combine)
}
/**
Returns a copy of self, filtered to only have values for the whitelisted keys.
:param: keys Whitelisted keys
:returns: Filtered dictionary
*/
func pick (keys: [Key]) -> Dictionary {
return filter { (key: Key, _) -> Bool in
return keys.contains(key)
}
}
/**
Returns a copy of self, filtered to only have values for the whitelisted keys.
:param: keys Whitelisted keys
:returns: Filtered dictionary
*/
func pick (keys: Key...) -> Dictionary {
return pick(unsafeBitCast(keys, [Key].self))
}
/**
Returns a copy of self, filtered to only have values for the whitelisted keys.
:param: keys Keys to get
:returns: Dictionary with the given keys
*/
func at (keys: Key...) -> Dictionary {
return pick(keys)
}
/**
Removes a (key, value) pair from self and returns it as tuple.
If the dictionary is empty returns nil.
:returns: (key, value) tuple
*/
mutating func shift () -> (Key, Value)? {
if let key = keys.first {
return (key, removeValueForKey(key)!)
}
return nil
}
}
/**
Difference operator
*/
public func - <K, V: Equatable> (first: [K: V], second: [K: V]) -> [K: V] {
return first.difference(second)
}
/**
Intersection operator
*/
public func & <K, V: Equatable> (first: [K: V], second: [K: V]) -> [K: V] {
return first.intersection(second)
}
/**
Union operator
*/
public func | <K: Hashable, V> (first: [K: V], second: [K: V]) -> [K: V] {
return first.union(second)
}