Created
March 7, 2018 12:45
-
-
Save jinahadam/f687bba387830ad9dba1d68b102753c1 to your computer and use it in GitHub Desktop.
#swift #array #collections
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
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