<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 <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> 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=""><<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>></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 "&+" - 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> 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="">
<<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a> <mailto:<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>>> wrote:<br class="">
<br class="">
I'm +1 for adding mapValues. Very useful functionality and trivial to<br class="">
implement.<br class="">
<br class="">
> > I.e. I suggest to implement and mapKeys() also. It could be also<br class="">
useful in some situations.<br class="">
> `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="">
><br class="">
> extension Dictionary {<br class=""></span>
> func mapValues__(_ valueTransform: @noescape Value throws<span class=""><br class="">
->OutValue) rethrows ->[Key: OutValue] { … }<br class="">
><br class=""></span>
> func mapKeys__(_ keyTransform: @noescape Key throws ->OutKey)<span class=""><br class="">
rethrows ->[OutKey: [Value]] { … }<br class="">
><br class="">
> // Possibly flatMap variants, too?<br class="">
> }<br class="">
><br class="">
> extension Dictionary where Value: Sequence {<br class=""></span>
> func reduceValues__(_ initial: OutValue, combine: @noescape<span class=""><br class="">
(OutValue, Value.Iterator.Element) throws ->OutValue) rethrows ->[Key:<br class="">
OutValue] {<br class="">
> return mapValues { $0.reduce(initial, combine: combine) }<br class="">
> }<br class="">
> }<br class="">
><br class="">
> Which you would end up using like this:<br class="">
><br class="">
> let wordFrequencies: [String: Int] = …<br class="">
> let firstLetterFrequencies: [Character: Int] = wordFrequencies.mapKeys { $0.characters.first! }.reduceValues(0, combine: +)<br class="">
><br class="">
> --<br class="">
> Brent Royal-Gordon<br class="">
> Architechies<br class="">
><br class="">
><br class="">
><br class=""></span>
>______<span class=""><br class="">
<br class="">
--<br class="">
Miguel Angel Quinones<br class="">
<br class="">
_______________________________________________<br class="">
swift-evolution mailing list<br class=""></span>
<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a> <mailto:<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><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>