Skip to content

Instantly share code, notes, and snippets.

@jpmcglone
Last active March 14, 2019 17:38
Show Gist options
  • Save jpmcglone/28a1a0083c15fc72a188 to your computer and use it in GitHub Desktop.
Save jpmcglone/28a1a0083c15fc72a188 to your computer and use it in GitHub Desktop.
array.filter into multiple arrays in one pass
public struct ConditionalArrayFilter <Element> {
let condition: (Element) -> Bool // the condition to meet
private var _array = Array<Element>()
var array: Array<Element> {
get { return _array }
}
init(condition: (Element) -> Bool) { self.condition = condition }
}
private extension ConditionalArrayFilter {
mutating func append(element: Element) {
if condition(element) {
_array.append(element)
}
}
}
extension Array {
func filter(filters:[ConditionalArrayFilter<Element>]) {
for element in self {
for var filter in filters {
filter.append(element)
}
}
}
}
/****************************/
/** Demo of the above code **/
/****************************/
class ConditionalArrayLoaderTest {
func test() {
// What's great is this is done in O(n)
let even = ConditionalArrayFilter<Int> { int in
return int % 2 == 0
}
let odd = ConditionalArrayFilter<Int> { int in
return int % 2 == 1
}
let by5 = ConditionalArrayFilter<Int> { int in
return int % 5 == 0
}
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
numbers.filter([even, odd, by5])
print(even.array)
print(odd.array)
print(by5.array)
}
}
@jpmcglone
Copy link
Author

It seems if the ratio c:i is large, the savings shrink and are maybe ignorable. But hey, this is just as easy to use as .filter 3 times, so any savings is better to me :)

@jpmcglone
Copy link
Author

Another bonus is, if you hold on to the ConditionalArrayFilter object, you can use the .condition closure to test another Element (of the same type) against it.

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