Skip to content

Instantly share code, notes, and snippets.

View TheNamesJamesW's full-sized avatar

James Wilkinson TheNamesJamesW

View GitHub Profile
// 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
@TheNamesJamesW
TheNamesJamesW / Filter.swift
Created November 12, 2016 20:25
A filter that can be applied on a Sequence but stored (and passed around) independently.
/**
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]>
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
}