In Swift, an efficient function that separates an array into 2 arrays based on a predicate

后端 未结 7 1114
太阳男子
太阳男子 2020-12-15 04:40

Note: I\'m currently still using Swift 2.2, but open to Swift 3 solutions as well

I\'m looking to create a function that operates very closely to filter

7条回答
  •  青春惊慌失措
    2020-12-15 05:01

    let objects: [Int] = Array(1..<11)
    let split = objects.reduce(([Int](), [Int]())) { (value, object) -> ([Int], [Int]) in
    
        var value = value
    
        if object % 2 == 0 {
            value.1.append(object)
        } else {
            value.0.append(object)
        }
    
        return value
    }
    

提交回复
热议问题