<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">I’ve been absorbed in the world of Monoids lately, so I find the suggestion below to be particularly brilliant. : )<div class="">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?</div><div class=""><br class=""></div><div class=""><div class=""><div><blockquote type="cite" class=""><div class="">On 15 Jan 2016, at 10:18, Nicola Salmoria via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">To handle the case of duplicate keys, why not allow to pass in a 'combine' function?<br class="">The function could default to a preconditionFailure to be consistent with the DictionaryLiteral behavior, but be overridden by the caller as needed.<br class=""><br class="">extension Dictionary {<br class=""> /// Creates a dictionary with the keys and values in the given sequence.<br class=""> init<S: SequenceType where S.Generator.Element == Generator.Element>(_ sequence: S, combine: (existing: Value, other: Value) -> Value = { preconditionFailure("Sequence contains duplicate keys"); return $1 } ) {<br class=""> self.init()<br class=""> for (key, value) in sequence {<br class=""> if let existing = updateValue(value, forKey: key) {<br class=""> updateValue(combine(existing: existing, other: value), forKey: key)<br class=""> }<br class=""> }<br class=""> }<br class="">}<br class=""><br class=""><br class="">usage examples:<br class=""><br class="">let samples = [("Rome", 40.2), ("New York", 35.1), ("Rome", 42.5), ("New York", 32.8)]<br class="">let minTemperatures = Dictionary(samples, combine: min)<br class="">// ["Rome": 40.2, "New York": 32.8]<br class="">let maxTemperatures = Dictionary(samples, combine: max)<br class="">// ["Rome": 42.5, "New York": 35.1]<br class=""><br class="">let probabilities = [("a", 0.25), ("b", 0.25), ("c", 0.25), ("a", 0.25)]<br class="">let stateProbabilities = Dictionary(probabilities, combine: +)<br class="">// ["b": 0.25, "a": 0.5, "c": 0.25]<br class=""><br class=""></div><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">Nicola</span></div></blockquote><br class=""></div><div><div class="">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 : )</div><div class=""><br class=""></div><div class="">Al<br class=""></div><div class=""><br class=""></div></div><br class=""></div></div></body></html>