Skip to content

Instantly share code, notes, and snippets.

@JJC1138
Created October 21, 2016 02:13
Show Gist options
  • Save JJC1138/b2b9d95d299337e15e39d674cea85aa9 to your computer and use it in GitHub Desktop.
Save JJC1138/b2b9d95d299337e15e39d674cea85aa9 to your computer and use it in GitHub Desktop.
fileprivate extension Array {
func filterIntoTrueAndFalse(_ shouldBeIncludedInFirstResult: (Element) throws -> Bool) rethrows -> ([Element], [Element]) {
var trueResults = [Element]()
var falseResults = [Element]()
for i in self {
if try shouldBeIncludedInFirstResult(i) {
trueResults.append(i)
} else {
falseResults.append(i)
}
}
return (trueResults, falseResults)
}
}
@dfrib
Copy link

dfrib commented Nov 14, 2016

I just bounced in here by chance (from SO), and couldn't help writing another (probably slightly less performant and surely more try messy) alternative to the above, for the fun of it :)

func filterIntoTrueAndFalse(_ shouldBeIncludedInFirstResult: (Element) throws -> Bool) rethrows -> ([Element], [Element]) {
    return (try filter(shouldBeIncludedInFirstResult),
            try filter{!(try shouldBeIncludedInFirstResult($0))})
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment