Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move some/all array extensions to Collection/Sequence/Indexable protocols #213

Open
piv199 opened this issue Sep 27, 2016 · 2 comments
Open
Milestone

Comments

@piv199
Copy link
Collaborator

piv199 commented Sep 27, 2016

.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 piv199 changed the title Move some array extensions to Collection/Sequence protocol Move some/all array extensions to Collection/Sequence/Indexable protocols Sep 27, 2016
@piv199
Copy link
Collaborator Author

piv199 commented Sep 27, 2016

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
Copy link
Collaborator Author

piv199 commented Sep 27, 2016

    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))]
    }

@piv199 piv199 modified the milestone: 1.9 Dec 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants