是否有O(1)从Swift集合中删除零值的方法?

问题描述 投票:2回答:1

我正在考虑自定义Collection,类似:

for i in [1, 2, nil].nilValuesRemoved() {
     print(i)
}

将打印:

1
2

实际的.nilValuesRemoved()调用将是O(1),与.reverse()的工作方式相同。这可能吗?

arrays swift algorithm
1个回答
2
投票

这是一个懒惰的compactMap:

© www.soinside.com 2019 - 2024. All rights reserved.