Swift中集合类型的扩展,用于查找对象之后的所有对象

Swift中集合类型的扩展,用于查找对象之后的所有对象,swift,generics,collections,functional-programming,Swift,Generics,Collections,Functional Programming,我想在Swift中的CollectionType上编写一个扩展,它将在数组中的一个对象之后查找x对象。显然,即使项目后面没有对象,也需要保护它才能工作 在我的脑海里,签名是这样的: func itemsAfterItem(item: T, limit: Int?) -> [T] 我不知道如何实现它,有人能帮忙吗?我想你可以试试这个: func itemsAfterItem(item: T, limit: Int?) -> [T] { var counter: Int = 0

我想在Swift中的CollectionType上编写一个扩展,它将在数组中的一个对象之后查找x对象。显然,即使项目后面没有对象,也需要保护它才能工作

在我的脑海里,签名是这样的:

func itemsAfterItem(item: T, limit: Int?) -> [T]

我不知道如何实现它,有人能帮忙吗?

我想你可以试试这个:

func itemsAfterItem(item: T, limit: Int?) -> [T] {
    var counter: Int = 0
    var isAfter: Bool = false
    let array = [T]()
    let newLimit = limit != nil ? limit : myArray.count
    for tmpItem in myArray {
        if tmpItem == T {
            isAfter = true
        }
        if isAfter && counter < limit {
            array.append(tmpItem)
            counter += 1
        }
    }
}
func itemsAfterItem(item:T,limit:Int?)->[T]{
变量计数器:Int=0
var isAfter:Bool=false
让数组=[T]()
让newLimit=limit!=nil?limit:myArray.count
对于myArray中的tmpItem{
如果tmpItem==T{
isAfter=true
}
如果isAfter&&counter<限制{
array.append(tmpItem)
计数器+=1
}
}
}
此函数将把
T
项放在数组的开头


我还没有测试这个功能

我想你可以试试这个:

func itemsAfterItem(item: T, limit: Int?) -> [T] {
    var counter: Int = 0
    var isAfter: Bool = false
    let array = [T]()
    let newLimit = limit != nil ? limit : myArray.count
    for tmpItem in myArray {
        if tmpItem == T {
            isAfter = true
        }
        if isAfter && counter < limit {
            array.append(tmpItem)
            counter += 1
        }
    }
}
func itemsAfterItem(item:T,limit:Int?)->[T]{
变量计数器:Int=0
var isAfter:Bool=false
让数组=[T]()
让newLimit=limit!=nil?limit:myArray.count
对于myArray中的tmpItem{
如果tmpItem==T{
isAfter=true
}
如果isAfter&&counter<限制{
array.append(tmpItem)
计数器+=1
}
}
}
此函数将把
T
项放在数组的开头


我没有仅仅因为喜欢这个挑战而测试这个函数;)

结果是

arr.itemsAfterItem(2)             // [4, 6, 9]
arr.itemsAfterItem(2, limit: 2)   // [4, 6]
arr.itemsAfterItem(2, limit: 100) // [4, 6, 9]
arr.itemsAfterItem(9, limit: 2)   // []
arr.itemsAfterItem(3, limit: 100) // []

只是因为我喜欢这个挑战;)

结果是

arr.itemsAfterItem(2)             // [4, 6, 9]
arr.itemsAfterItem(2, limit: 2)   // [4, 6]
arr.itemsAfterItem(2, limit: 100) // [4, 6, 9]
arr.itemsAfterItem(9, limit: 2)   // []
arr.itemsAfterItem(3, limit: 100) // []

一个可能的实现任意集合的
equalable
元素(解释内联)。主要 挑战在于正确地获取参数类型和约束

extension CollectionType where Generator.Element: Equatable,
                         SubSequence.Generator.Element == Generator.Element {

    func itemsAfterItem(item: Generator.Element, limit: Index.Distance?) -> [Generator.Element] {
        if let idx = indexOf(item) where idx != endIndex {
            // Start after the given item:
            let from = idx.advancedBy(1)
            // Up to min(from + limit, endIndex):
            let to = limit.map { from.advancedBy($0, limit: endIndex) } ?? endIndex
            // Return slice as an array:
            return Array(self[from..<to])
        } else {
            // Item not found, or only at the last position.
            return []
        }
    }
}
该部分留给读者作为练习:)

示例:

[1, 2, 3, 4, 5, 6].itemsAfterItem(2, limit: 2)    // [3, 4]
["x", "y", "z"].itemsAfterItem("y", limit: 4)     // ["z"]
[1, 2, 3].itemsAfterItem(7, limit: 4)             // []
[1.1, 2.2, 3.3].itemsAfterItem(1.1, limit: nil)   // [2.2, 3.3]
非数组集合示例:

"abcdef".characters.itemsAfterItem("b", limit: 2) // ["c", "d"]

一个可能的实现任意集合的
equalable
元素(解释内联)。主要 挑战在于正确地获取参数类型和约束

extension CollectionType where Generator.Element: Equatable,
                         SubSequence.Generator.Element == Generator.Element {

    func itemsAfterItem(item: Generator.Element, limit: Index.Distance?) -> [Generator.Element] {
        if let idx = indexOf(item) where idx != endIndex {
            // Start after the given item:
            let from = idx.advancedBy(1)
            // Up to min(from + limit, endIndex):
            let to = limit.map { from.advancedBy($0, limit: endIndex) } ?? endIndex
            // Return slice as an array:
            return Array(self[from..<to])
        } else {
            // Item not found, or only at the last position.
            return []
        }
    }
}
该部分留给读者作为练习:)

示例:

[1, 2, 3, 4, 5, 6].itemsAfterItem(2, limit: 2)    // [3, 4]
["x", "y", "z"].itemsAfterItem("y", limit: 4)     // ["z"]
[1, 2, 3].itemsAfterItem(7, limit: 4)             // []
[1.1, 2.2, 3.3].itemsAfterItem(1.1, limit: nil)   // [2.2, 3.3]
非数组集合的示例:

"abcdef".characters.itemsAfterItem("b", limit: 2) // ["c", "d"]

你有没有尝试过与签名相去甚远的东西?你有没有尝试过与签名相去甚远的东西?我想我们基本上有相同的想法:)–注意,
equalable
就足够了,你可以简化为
let index=self.indexOf(item)
@MartinR谢谢!您的解决方案看起来更干净一些。。。我的swift有点生疏了,由于所有的常规工作和所有与口袋妖怪go相关的东西,我无法编写很多程序:我想我们基本上有相同的想法:)–注意,
equalable
就足够了,你可以简化为
let index=self.indexOf(item)
@MartinR谢谢!您的解决方案看起来更干净一些。。。我的swift有点生疏了,由于所有的常规工作和所有与口袋妖怪围棋相关的东西,无法编写很多程序:P