This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Collection { | |
func dictionary<K, V>(transform:(_ element: Iterator.Element) -> [K : V]) -> [K : V] { | |
var dictionary = [K : V]() | |
self.forEach { element in | |
for (key, value) in transform(element) { | |
dictionary[key] = value | |
} | |
} | |
return dictionary | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Defines a filter that can be applied on a `Sequence` instance but stored (and passed around) independently. | |
Useful if switching filters frequently, creating dynamic subsets of a given dataset and passing between abstraction levels (i.e. View level could create this and pass to the Model) | |
Example: | |
``` | |
let strings = ["c", "b", "b", "b", "a"] | |
var predicate: Filter<String, [String]> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For-in but only enumerate values of a certain type | |
let things = [1, 2, "three"] | |
for case let number as Int in things { | |
print(number) | |
} | |
// Prints: | |
// 1 | |
// 2 |