Skip to content

Instantly share code, notes, and snippets.

@jinahadam
Created March 7, 2018 12:45
Show Gist options
  • Save jinahadam/f687bba387830ad9dba1d68b102753c1 to your computer and use it in GitHub Desktop.
Save jinahadam/f687bba387830ad9dba1d68b102753c1 to your computer and use it in GitHub Desktop.
#swift #array #collections
let names = ["Paul", "Elena", "Zoe"]
extension Sequence {
func last(where predicate: (Iterator.Element) -> Bool) -> Iterator.Element? {
for element in reversed() where predicate(element) {
return element
}
return nil
}
}
let match = names.last { $0.hasSuffix("a") }
match
extension Array {
func accumulate<Result>(_ initialResult: Result,
_ nextPartialRequest: (Result, Element) -> Result) -> [Result] {
var running = initialResult
return map { next in
running = nextPartialRequest(running, next)
return running
}
}
}
[1,2,3,4].accumulate(0, +)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment