Last active
August 29, 2015 14:03
-
-
Save bitops/96ab35529018278fa8c9 to your computer and use it in GitHub Desktop.
An example implementation of Ruby's 'partition' method in Swift.
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
extension Array { | |
func partition(iterator: (T) -> Bool) -> (Array<T>, Array<T>) { | |
var ifTrue = Array<T>() | |
var ifFalse = Array<T>() | |
self.each { | |
if iterator($0) == true { | |
ifTrue.append($0) | |
} else { | |
ifFalse.append($0) | |
} | |
} | |
return (ifTrue, ifFalse) | |
} | |
} | |
/* | |
Example usage: | |
var numbersToPart = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
var (even, odd) = numbersToPart.partition { $0 % 2 == 0 } | |
// even => [2, 4, 6, 8, 10] | |
// odd => [1, 3, 5, 7, 9 ] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Depends on this 'each' implentation: