Home     About     Archive

Lazy Thoughts

Why I Love Swift

Because.

This would take a lot more lines in Objective-C, and be less clear.

Here’s the extension for it:

extension Array {
    func partitionBy(predicate: (Element -> Bool)) -> ([Element], [Element]) {
        var left = [Element]()
        var right = [Element]()
        
        for elem in self {
            if predicate(elem) {
                left.append(elem)
            } else {
                right.append(elem)
            }
        }
        
        return (left, right)
    }
  }
comments powered by Disqus