Created
April 8, 2020 09:47
-
-
Save azone/a4252b1791b8381fa41e6aa43b8437d2 to your computer and use it in GitHub Desktop.
Useful swift extensions for `Sequence` and `Optional` with `Collection` wrapper
This file contains 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
public extension Sequence { | |
func all(_ predicate: (Element) throws -> Bool) rethrows -> Bool { | |
try allSatisfy(predicate) | |
} | |
func any(_ predicate: (Element) throws -> Bool) rethrows -> Bool { | |
try contains(where: predicate) | |
} | |
} | |
public extension Sequence where Element: AdditiveArithmetic { | |
func sum() -> Element { | |
reduce(Element.zero, +) | |
} | |
} | |
public extension Optional where Wrapped: Collection { | |
var isEmpty: Bool { | |
guard case let .some(val) = self else { | |
return true | |
} | |
return val.isEmpty | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Now you can use: