<div dir="ltr"><div>I&#39;m ambivalent about the preconditionFailure. Since there would otherwise be silent loss of data, I think it fits Swift&#39;s &quot;safe by default&quot; paradigm. It&#39;s also consistent with what the normal initialization from a DictionaryLiteral does.<br></div><div>However, I can also see how it might be more convenient to just pick the last value.<br></div><br>Nicola<br><br><div><div class="gmail_extra"><div class="gmail_quote">On Fri, Jan 15, 2016 at 11:53 AM, Alan Skipp <span dir="ltr">&lt;<a href="mailto:al_skipp@icloud.com" target="_blank">al_skipp@icloud.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">I’ve been absorbed in the world of Monoids lately, so I find the suggestion below to be particularly brilliant. : )<div>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><br></div><div><div><span class=""><div><blockquote type="cite"><div>On 15 Jan 2016, at 10:18, Nicola Salmoria via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br><div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">To handle the case of duplicate keys, why not allow to pass in a &#39;combine&#39; function?<br>The function could default to a preconditionFailure to be consistent with the DictionaryLiteral behavior, but be overridden by the caller as needed.<br><br>extension Dictionary {<br>    /// Creates a dictionary with the keys and values in the given sequence.<br>    init&lt;S: SequenceType where S.Generator.Element == Generator.Element&gt;(_ sequence: S, combine: (existing: Value, other: Value) -&gt; Value = { preconditionFailure(&quot;Sequence contains duplicate keys&quot;); return $1 } ) {<br>        self.init()<br>        for (key, value) in sequence {<br>            if let existing = updateValue(value, forKey: key) {<br>                updateValue(combine(existing: existing, other: value), forKey: key)<br>            }<br>        }<br>    }<br>}<br><br><br>usage examples:<br><br>let samples = [(&quot;Rome&quot;, 40.2), (&quot;New York&quot;, 35.1), (&quot;Rome&quot;, 42.5), (&quot;New York&quot;, 32.8)]<br>let minTemperatures = Dictionary(samples, combine: min)<br>// [&quot;Rome&quot;: 40.2, &quot;New York&quot;: 32.8]<br>let maxTemperatures = Dictionary(samples, combine: max)<br>// [&quot;Rome&quot;: 42.5, &quot;New York&quot;: 35.1]<br><br>let probabilities = [(&quot;a&quot;, 0.25), (&quot;b&quot;, 0.25), (&quot;c&quot;, 0.25), (&quot;a&quot;, 0.25)]<br>let stateProbabilities = Dictionary(probabilities, combine: +)<br>// [&quot;b&quot;: 0.25, &quot;a&quot;: 0.5, &quot;c&quot;: 0.25]<br><br></div><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">Nicola</span></div></blockquote><br></div></span><div><div>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><br></div><div>Al<br></div><div><br></div></div><br></div></div></div></blockquote></div><br></div></div></div>