[swift-evolution] Proposal: Add a sequence-based initializer to Dictionary

Thorsten Seitz tseitz42 at icloud.com
Fri Jan 15 08:48:29 CST 2016


+1

-Thorsten 

> Am 15.01.2016 um 15:32 schrieb Donnacha Oisín Kidney via swift-evolution <swift-evolution at swift.org>:
> 
> I think the idea of a combine closure is great - it lets users of the initialiser decide on whether or not the want duplicate keys to be an error. Also, the default “silently keep the last value” behaviour can be provided as the default closure. I also think that a closure (probably) gives enough flexibility to do most of the interesting things you might do with dictionaries (a “frequencies” function, or “categorise”, for instance).
> 
> extension Dictionary {
>   /// Creates a dictionary with the keys and values in the given sequence.
>   /// Takes a closure which is called on the values of duplicate keys. The
>   /// default behaviour is that the last value for a duplicate key is kept.
>   init<S: SequenceType where S.Generator.Element == (Key,Value)>
>     (_ sequence: S, combine: (Value,Value) -> Value = { (_,snd) in snd } ) {
>     self.init()
>     for (k,v) in sequence {
>       self[k] = self[k].map { fst in combine(fst,v) } ?? v
>     }
>   }
> }
> 
> extension SequenceType where Generator.Element: Hashable {
>   func frequencies() -> [Generator.Element:Int] {
>     return Dictionary(self.lazy.map { v in (v,1) }, combine: +)
>   }
> }
> 
> "hello".characters.frequencies() // ["e": 1, "o": 1, "l": 2, "h": 1]
> 
>  
>> On 15 Jan 2016, at 10:53, Alan Skipp via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> I’ve been absorbed in the world of Monoids lately, so I find the suggestion below to be particularly brilliant. : )
>> It solves the issue of arbitrarily choosing the value for duplicate keys rather nicely. Only thing I’m not too sure about is the idea of failing by default on duplicate keys?
>> 
>>> On 15 Jan 2016, at 10:18, Nicola Salmoria via swift-evolution <swift-evolution at swift.org> wrote:
>>> 
>>> To handle the case of duplicate keys, why not allow to pass in a 'combine' function?
>>> The function could default to a preconditionFailure to be consistent with the DictionaryLiteral behavior, but be overridden by the caller as needed.
>>> 
>>> extension Dictionary {
>>>     /// Creates a dictionary with the keys and values in the given sequence.
>>>     init<S: SequenceType where S.Generator.Element == Generator.Element>(_ sequence: S, combine: (existing: Value, other: Value) -> Value = { preconditionFailure("Sequence contains duplicate keys"); return $1 } ) {
>>>         self.init()
>>>         for (key, value) in sequence {
>>>             if let existing = updateValue(value, forKey: key) {
>>>                 updateValue(combine(existing: existing, other: value), forKey: key)
>>>             }
>>>         }
>>>     }
>>> }
>>> 
>>> 
>>> usage examples:
>>> 
>>> let samples = [("Rome", 40.2), ("New York", 35.1), ("Rome", 42.5), ("New York", 32.8)]
>>> let minTemperatures = Dictionary(samples, combine: min)
>>> // ["Rome": 40.2, "New York": 32.8]
>>> let maxTemperatures = Dictionary(samples, combine: max)
>>> // ["Rome": 42.5, "New York": 35.1]
>>> 
>>> let probabilities = [("a", 0.25), ("b", 0.25), ("c", 0.25), ("a", 0.25)]
>>> let stateProbabilities = Dictionary(probabilities, combine: +)
>>> // ["b": 0.25, "a": 0.5, "c": 0.25]
>>> 
>>> 
>>> Nicola
>> 
>> It’d be great if there was also an init that restricted the Values to Monoids, which would mean the combine function would be taken from the supplied Monoid values (I understand I’ve departed to fantasy island at this point, but one can dream : )
>> 
>> Al
>> 
>> 
>>  _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160115/13fb9c92/attachment.html>


More information about the swift-evolution mailing list