[swift-evolution] Dictionary Enhancements

Ben Cohen ben_cohen at apple.com
Thu Feb 16 19:17:00 CST 2017


> On Feb 16, 2017, at 5:06 PM, David Sweeris <davesweeris at mac.com> wrote:
> 
>> 
>> On Feb 16, 2017, at 4:26 PM, Ben Cohen via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> Hi swift-evolution,
>> 
>> Following up on Ted’s post regarding the opening up of stage 2, I’m starting a thread to discuss improvements to the Dictionary type.
>> 
>> Here is a list of commonly requested changes/enhancements to Dictionary, all of which would probably be appropriate to put together into a single evolution proposal:
>> 
>> init from/merge in a Sequence of Key/Value pairs (already raised as SE-100: https://github.com/apple/swift-evolution/blob/master/proposals/0100-add-sequence-based-init-and-merge-to-dictionary.md <https://github.com/apple/swift-evolution/blob/master/proposals/0100-add-sequence-based-init-and-merge-to-dictionary.md>).
>> make the Values view collection a MutableCollection (as in this PR: https://github.com/apple/swift-evolution/pull/555 <https://github.com/apple/swift-evolution/pull/555>).
>> Add a defaulting subscript get (e.g. counts[key, default: 0] += 1 or grouped(key, default:[]].append(value)).
>> Add a group by-like init to create a Dictionary<K,[V]> from a sequence of V and a closure (V)->K.
>> Add Dictionary.filter to return a Dictionary.
>> Add Dictionary.mapValues to return a Dictionary (can be more efficiently implemented than composition as the storage layout remains the same).
>> Add capacity property and reserveCapacity() method.
>> Have Dictionary.removeAtIndex return the Index of the next entry.
>> (once we have conditional conformance) Make dictionaries with Equatable values Equatable.
>> Please reply here with any comments or questions on the above list, or any additions you believe are important that are missing from it.
>> 
> 
> If we added an @injective annotation (https://en.wikipedia.org/wiki/Injective_function <https://en.wikipedia.org/wiki/Injective_function>), we could add a .mapToDict function that returns another Dictionary with different keys instead of an array of tuples.
> 

Assuming Dictionary also acquires an initializer from a sequence of key/value pairs, being able to map both keys and values doesn’t buy you much over just constructing a new dictionary. The win with mapping only the values is that the underlying hash table can retain the same physical layout, just with different values in the slots, so it can be done faster.

> Come to think of it, we could give sets the same treatment, too.
> 
> - Dave Sweeris

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170216/bf22919a/attachment.html>


More information about the swift-evolution mailing list