<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=""><div><blockquote type="cite" class=""><div class="">On Apr 13, 2016, at 12:02 PM, Jacob Bandes-Storch via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">To adhere to the API Design Guidelines, I think it should be named "mappingValues", right?</div></div></blockquote><blockquote type="cite" class=""><br class=""></blockquote><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_extra">
<div class="gmail_quote">On Wed, Apr 13, 2016 at 4:23 AM, Vladimir.S via swift-evolution <span dir="ltr" class="">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">As for mapKeys and many values into a single key. I believe we should have a choice - do we expect multiply values for the same key or not. Just like "+" and integer overflow : by default it raises the error, but if "&amp;+" - we expect the overflow. I can imagine situations when it is ok for me to have different values for the same key(if I don't care which of values should be for that key in result dictionary).<br class="">
So my proposal is some additional mapKey(allowMultiplyValues: true) {...} or in any other form/name.<span class=""><br class=""></span></blockquote></div></div></div></div></blockquote><div><br class=""></div><div>There's a proposal (awaiting merging) to add Dictionary initializers and methods that work with key/value pairs. These provide different ways of dealing with the duplicate key issue after a call to the regular Collection.map method.</div></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div><div><a href="https://github.com/natecook1000/swift-evolution/blob/natecook-dictionary-merge/proposals/0000-add-sequence-based-init-and-merge-to-dictionary.md" class="">https://github.com/natecook1000/swift-evolution/blob/natecook-dictionary-merge/proposals/0000-add-sequence-based-init-and-merge-to-dictionary.md</a></div></div></blockquote><div><div><br class=""></div><div>I'd be interested in a `mapValues` or `transformValues` method that would modify values in place while leaving keys alone.</div><div><br class=""></div><div>Another useful method (that could be used to build mapValues more efficiently) would be `Dictionary.updateValue(value: Value, at index: DictionaryIndex)`, so you could write:</div><div><br class=""></div><div>var dict = ["a": 1, "b": 2, "c": 3]</div><div>if let i = dict.index(where: { $0.value == 3 }) {</div><div>&nbsp; &nbsp; dict.updateValue(100, at: i)</div><div>}</div><div>// dict == ["a": 1, "b": 2, "c": 100]</div><div><br class=""></div><div>-Nate</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">

On 13.04.2016 13:38, Ross O'Brien via swift-evolution wrote:<br class="">
</span><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">
+1 on mapValues.<br class="">
<br class="">
DictionaryLiteral already throws an exception if it includes duplicate<br class="">
keys, so I'd expect mapKeys to throw an error if multiple source keys<br class="">
mapped to the same destination key.<br class="">
<br class="">
On Wed, Apr 13, 2016 at 11:28 AM, Miguel Angel Quinones via swift-evolution<br class=""></span><span class="">
&lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a> &lt;mailto:<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt;&gt; wrote:<br class="">
<br class="">
&nbsp; &nbsp; I'm +1 for adding mapValues. Very useful functionality and trivial to<br class="">
&nbsp; &nbsp; implement.<br class="">
<br class="">
&nbsp; &nbsp; &nbsp;&gt; &gt; I.e. I suggest to implement and mapKeys() also. It could be also<br class="">
&nbsp; &nbsp; useful in some situations.<br class="">
&nbsp; &nbsp; &gt; `mapKeys` is much more dangerous, because you could end up mapping many values into a single key. You kind of need to combine the values somehow. Perhaps:<br class="">
&nbsp; &nbsp; &gt;<br class="">
&nbsp; &nbsp; &gt; extension Dictionary {<br class=""></span>
&nbsp; &nbsp; &nbsp;&gt; func mapValues__(_ valueTransform: @noescape Value throws<span class=""><br class="">
&nbsp; &nbsp; -&gt;OutValue) rethrows -&gt;[Key: OutValue] { … }<br class="">
&nbsp; &nbsp; &nbsp;&gt;<br class=""></span>
&nbsp; &nbsp; &nbsp;&gt; func mapKeys__(_ keyTransform: @noescape Key throws -&gt;OutKey)<span class=""><br class="">
&nbsp; &nbsp; rethrows -&gt;[OutKey: [Value]] { … }<br class="">
&nbsp; &nbsp; &gt;<br class="">
&nbsp; &nbsp; &gt; // Possibly flatMap variants, too?<br class="">
&nbsp; &nbsp; &gt; }<br class="">
&nbsp; &nbsp; &gt;<br class="">
&nbsp; &nbsp; &gt; extension Dictionary where Value: Sequence {<br class=""></span>
&nbsp; &nbsp; &nbsp;&gt; func reduceValues__(_ initial: OutValue, combine: @noescape<span class=""><br class="">
&nbsp; &nbsp; (OutValue, Value.Iterator.Element) throws -&gt;OutValue) rethrows -&gt;[Key:<br class="">
&nbsp; &nbsp; OutValue] {<br class="">
&nbsp; &nbsp; &gt; return mapValues { $0.reduce(initial, combine: combine) }<br class="">
&nbsp; &nbsp; &gt; }<br class="">
&nbsp; &nbsp; &gt; }<br class="">
&nbsp; &nbsp; &gt;<br class="">
&nbsp; &nbsp; &gt; Which you would end up using like this:<br class="">
&nbsp; &nbsp; &gt;<br class="">
&nbsp; &nbsp; &gt; let wordFrequencies: [String: Int] = …<br class="">
&nbsp; &nbsp; &gt; let firstLetterFrequencies: [Character: Int] = wordFrequencies.mapKeys { $0.characters.first! }.reduceValues(0, combine: +)<br class="">
&nbsp; &nbsp; &gt;<br class="">
&nbsp; &nbsp; &gt; --<br class="">
&nbsp; &nbsp; &gt; Brent Royal-Gordon<br class="">
&nbsp; &nbsp; &gt; Architechies<br class="">
&nbsp; &nbsp; &gt;<br class="">
&nbsp; &nbsp; &gt;<br class="">
&nbsp; &nbsp; &gt;<br class=""></span>
&nbsp; &nbsp; &gt;______<span class=""><br class="">
<br class="">
&nbsp; &nbsp; --<br class="">
&nbsp; &nbsp; Miguel Angel Quinones<br class="">
<br class="">
&nbsp; &nbsp; _______________________________________________<br class="">
&nbsp; &nbsp; swift-evolution mailing list<br class=""></span>
&nbsp; &nbsp; <a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a> &lt;mailto:<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt;<br class="">
&nbsp; &nbsp; <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><span class=""><br class="">
<br class="">
<br class="">
<br class="">
<br class="">
_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
<br class="">
</span></blockquote><div class="HOEnZb"><div class="h5">
_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</div></div></blockquote></div><br class=""></div></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>