CTMacUser
(Daryle Walker)
1
Have a helper method:
extension MutableCollection {
fileprivate mutating func overwrite<I: IteratorProtocol>(
prefixUsing source: inout I,
_ transform: (I.Element) -> Element
) -> Index {
var current = startIndex
let end = endIndex
while current < end, let seed = source.next() {
self[current] = transform(seed)
formIndex(after: ¤t)
}
return current
}
}
Which is used by:
extension MutableCollection {
@discardableResult
public mutating func overwrite<I: IteratorProtocol>(
prefixUsing source: inout I
) -> Index where I.Element == Element {
return overwrite(prefixUsing: &source, { $0 })
}
}
Instead of "{ $0 }," I wanted to use "\Element.self," but the compiler said there was no conversion between the (writable) key path and an (Element) -> Element function. Using the latest Xcode. Isn't this supposed to work? (I added an "as KeyPath" once, and it made no difference, so the problem isn't the mutability.)