[swift-evolution] [Draft] Dictionary & Set Enhancements

Xiaodi Wu xiaodi.wu at gmail.com
Fri Mar 31 11:16:51 CDT 2017

On Fri, Mar 31, 2017 at 8:28 AM, Nate Cook via swift-evolution <
swift-evolution at swift.org> wrote:

> Hello everyone!
> Here is a draft proposal to fill in some usability gaps in the standard
> library Dictionary and Set types. I'd love to hear any feedback from the
> list before submitting this as a PR.
> Thanks!
> Nate


Nate, in general, I like these changes very much. They would definitely be
worthwhile additions. The two things I'd like to comment on are as follows:

First, I wonder whether either `first(_:_:)` and `last(_:_:)` or the
proposed alternative holds their weight. Neither is more succinct than `{
$0 }` or `{ $1 }`, which does what it says very clearly *and* don't require
any discovery. I don't think users, in the moment, would be stumped as to
how to write such a closure, nor that any reader would be confused about
what the closure does:

let firstWins = Dictionary(merging: duplicates, resolvingCollisionsWith: {
$0 })

Second, although the proposed `Dictionary.filter` would be an improvement
on its own, I wonder if it is as valuable after implementation of part 1.
That is, with only implementation of part 1, you'd have the ability to
write `Dictionary(numbers.filter {...})!` to get a dictionary after filter.

There are optimization opportunities left on the floor here, certainly, but
I'd expect `Dictionary(numbers.lazy.filter {...})!` could recover those
opportunities if it matters. I just worry about the API confusion that
comes from two `filter` operations that return different types, the impact
on current source, as well as its inconsistency with `map` and `reduce`.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170331/01bd59a7/attachment.html>

More information about the swift-evolution mailing list