[swift-evolution] [Proposal] mapValues

Brent Royal-Gordon brent at architechies.com
Fri Apr 15 16:56:08 CDT 2016


> This is a clarification on what I meant (I haven't had much time to test it, but I think it's representative):
> 
> https://gist.github.com/therealbnut/c223d90a34bb14448b65fc6cc0ec70ac

There are a number of problems with this:

* Given just an `Index`, you need to be able to calculate the next `Index`, and check if it lies before the `endIndex`. The complexity of this is hidden in your example because you're relying on a traditional `DictionaryIndex` to perform these tasks. Thus, the `Index` type in your dictionary design would be some sort of wrapper-around-the-key, not the key itself. That's the source of the `subscript(_: Index) -> Value` which apparently confused you.

* Generic `SequenceType` and `CollectionType` operations now use only the values, not the keys and values. That means that `Array(newDictionary)` gets you an array of values, `filter` returns an array of values, `reduce` operates only on the values, etc; all of these operations throw away the keys. That's not necessarily wrong, but I'm not sure that you're aware of it.

* Your `map` is overloading `SequenceType.map` merely by return type. That means you can no longer assign it directly to a newly-declared variable, or use it in many other contexts where the type has to be inferred.

* Your `enumerate` is similarly overloading by return type. It's also further confusing an already confused semantic. `enumerate` does not pair elements with their indices; it pairs them with integers starting at zero. This *happens* to correspond to their array indices, but that's not necessarily true of any other Collection. (I'm going to start another thread about this.)

Basically, in this conception of Dictionary:

* The keys are disposable and the values are the important part—most operations on a Dictionary would throw away the keys and use only the values.
* Index still would not be a Key—it would be some kind of instance which wrapped or converted into a Key.
* You still would need to name Dictionary-creating `map` and similar functions differently from their Array-generating cousins.
* Iterating over keys and values together would require some kind of Dictionary-specific API, not something that was available on other collections or sequences.

Maybe that would be an improvement, but I'm skeptical.

-- 
Brent Royal-Gordon
Architechies



More information about the swift-evolution mailing list