Skip to content

Instantly share code, notes, and snippets.

@to4iki
Last active October 24, 2016 17:06
Show Gist options
  • Save to4iki/33c36578e4860a190c59e1ca36a47993 to your computer and use it in GitHub Desktop.
Save to4iki/33c36578e4860a190c59e1ca36a47993 to your computer and use it in GitHub Desktop.
要素と位置(idx)をペアにしたコレクションを返す
import Foundation
extension Sequence {
func filterNot(_ isExcluded: (Self.Iterator.Element) throws -> Bool) rethrows -> [Self.Iterator.Element] {
var res: [Self.Iterator.Element] = []
for e in self where try !isExcluded(e) {
res.append(e)
}
return res
}
func zipWithIndex() -> Zip2Sequence<Self, CountableClosedRange<Int>> {
return zip(self, 0...Int.max)
}
}
let ary = Array(10...20)
let predicate = { (n1: Int, n2: Int) -> Bool in n1 % 2 == 0 }
let res = ary.zipWithIndex().filterNot(predicate)
print(res) // [(11, 1), (13, 3), (15, 5), (17, 7), (19, 9)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment