Last active
November 6, 2017 23:33
-
-
Save jimmythai/20e819ca4324d797350354bb0ea67d4c to your computer and use it in GitHub Desktop.
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
| //////////////////////// | |
| // MARK: Array | |
| //////////////////////// | |
| // map function | |
| extension Array { | |
| func map<T>(_ transform: (Element) throws -> T) rethrows -> [T] { | |
| var result: [T] = [] | |
| result.reserveCapacity(count) | |
| for x in self { | |
| try result.append(transform(x)) | |
| } | |
| return result | |
| } | |
| } | |
| // Find a specific element from the last. | |
| extension Sequence { | |
| func last(where predicate: (Iterator.Element) -> Bool) -> Iterator.Element? { | |
| for element in reversed() where predicate(element) { | |
| return element | |
| } | |
| return nil | |
| } | |
| } | |
| extension Array { | |
| func accumulate<Result>(_ initialResult: Result, _ nextPartialResult: (Result, Element) -> Result) -> [Result] { | |
| var running = initialResult | |
| return map { next in | |
| running = nextPartialResult(running, next) | |
| return running | |
| } | |
| } | |
| } | |
| // filter | |
| extension Sequence { | |
| func filter(_ isIncluded: @escaping (Element) -> Bool) -> [Element] { | |
| var result: [Element] = [] | |
| for x in self where isIncluded(x) { | |
| result.append(x) | |
| } | |
| return result | |
| } | |
| } | |
| // Check all elements in sequence match. | |
| extension Sequence { | |
| func all(matching predicate: (Iterator.Element) -> Bool) -> Bool { | |
| return contains { !predicate($0) } | |
| } | |
| } | |
| // Reduce | |
| extension Sequence { | |
| func reduce<Result>(_ initialResult: Result, _ nextPartialResult: (Result, Element) -> Result) -> Result { | |
| var result = initialResult | |
| for x in self { | |
| result = nextPartialResult(result, x) | |
| } | |
| return result | |
| } | |
| } | |
| // flatMap | |
| extension Sequence { | |
| func flatMap<T>(_ transform: (Element) throws -> [T]) rethrows -> [T] { | |
| var result: [T] = [] | |
| for x in self { | |
| result.append(contentsOf: try transform(x)) | |
| } | |
| return result | |
| } | |
| } | |
| //////////////////////// | |
| // MARK: Dictionary | |
| //////////////////////// | |
| // Merge(Swift3) | |
| extension Dictionary { | |
| mutating func mergee<S: Sequence>(_ other: S) where S.Iterator.Element == (key: Key, valu: Value) { | |
| for (k, v) in other { | |
| self[k] = v | |
| } | |
| } | |
| } | |
| // Merge(Swift4) | |
| var dict = ["a": 1, "b": 2] | |
| dict.merge(["a": 2, "d": 4]) { (current, _) in current } | |
| dict.merge([("b", 3), ("c", 3)]) { (_, new) in new } | |
| print(dict) | |
| // Sequence to Dictionary(Swift4) | |
| let seqDict = Dictionary(uniqueKeysWithValues: [("a", 1), ("b", 2)]) | |
| print(seqDict) | |
| // Sequence to Dictionary with grouping(swift4) | |
| let groupedDict = Dictionary(grouping: ["Banana", "Apple", "Apricot"]) { $0.first! } | |
| print(groupedDict) | |
| // MapValues(swift4) | |
| let numDict = ["a": 3, "b": 4] | |
| let mappedDict = numDict.mapValues { $0 * 2 } | |
| print(mappedDict) | |
| //////////////////////// | |
| // MARK: Set | |
| //////////////////////// | |
| var appleSet: Set = ["iPod", "iPad", "iPhone", "AppleWatch", "Mac", "Mac"] | |
| print(appleSet) | |
| let apple2 = appleSet.subtracting(["iPod", "iPad"]) | |
| print(apple2) | |
| let apple3 = appleSet.intersection(["iPhone"]) | |
| print(apple3) | |
| // Create unique Sequence | |
| extension Sequence where Iterator.Element: Hashable { | |
| func unique() -> [Iterator.Element] { | |
| var seen: Set<Iterator.Element> = [] | |
| return filter { | |
| if seen.contains($0) { | |
| return false | |
| } else { | |
| seen.insert($0) | |
| return true | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment