Move some/all array extensions to Collection/Sequence/Indexable protocols
Opened this issue · 2 comments
piv199 commented
.reserved (need to think what where to move)
forEachEnumerated(_:)
-> Sequence
get(at:)
-> BidirectionalCollection
, can be Collection, but I don't think it's good enough, bidirectional is better
containsType(of:)
-> Collection
insertFirst(_:)
-> Array
, This fits array mostly
random(_:)
-> Collection
, think of Dictionary return tuple
reverseIndex(_:)
-> Collection
, stick with where Self.Index == Int
?
piv199 commented
Dict seems not to be good fitted for containsType(of:)
But Set fits great.
let dict: [String: Int?] = ["a": 1, "b": 2, "c": nil]
let hash = Set<String>.init(["a", "b", "c"])
let res1 = dict.containsType(of: "b") //false
let res11 = dict.containsType(of: 2) //false
let res123 = dict.containsType(of: ("a", 3)) //false
let res1234 = dict.containsType(of: (String, Int?)("a", 3)) //true
let res2 = hash.containsType(of: "c") //true
piv199 commented
public func random() -> Iterator.Element? {
// add guard for empty collections
let rnd = Int(arc4random_uniform(UInt32(underestimatedCount)))
return self[index(startIndex, offsetBy: count.advanced(by: -1 - rnd))]
}