<div dir="ltr">To adhere to the API Design Guidelines, I think it should be named "mappingValues", right?<div class="gmail_extra">
<br><div class="gmail_quote">On Wed, Apr 13, 2016 at 4:23 AM, Vladimir.S via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><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>
So my proposal is some additional mapKey(allowMultiplyValues: true) {...} or in any other form/name.<span class=""><br>
<br>
On 13.04.2016 13:38, Ross O'Brien via swift-evolution wrote:<br>
</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>
<br>
DictionaryLiteral already throws an exception if it includes duplicate<br>
keys, so I'd expect mapKeys to throw an error if multiple source keys<br>
mapped to the same destination key.<br>
<br>
On Wed, Apr 13, 2016 at 11:28 AM, Miguel Angel Quinones via swift-evolution<br></span><span class="">
<<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a> <mailto:<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>>> wrote:<br>
<br>
I'm +1 for adding mapValues. Very useful functionality and trivial to<br>
implement.<br>
<br>
> > I.e. I suggest to implement and mapKeys() also. It could be also<br>
useful in some situations.<br>
> `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>
><br>
> extension Dictionary {<br></span>
> func mapValues__(_ valueTransform: @noescape Value throws<span class=""><br>
->OutValue) rethrows ->[Key: OutValue] { … }<br>
><br></span>
> func mapKeys__(_ keyTransform: @noescape Key throws ->OutKey)<span class=""><br>
rethrows ->[OutKey: [Value]] { … }<br>
><br>
> // Possibly flatMap variants, too?<br>
> }<br>
><br>
> extension Dictionary where Value: Sequence {<br></span>
> func reduceValues__(_ initial: OutValue, combine: @noescape<span class=""><br>
(OutValue, Value.Iterator.Element) throws ->OutValue) rethrows ->[Key:<br>
OutValue] {<br>
> return mapValues { $0.reduce(initial, combine: combine) }<br>
> }<br>
> }<br>
><br>
> Which you would end up using like this:<br>
><br>
> let wordFrequencies: [String: Int] = …<br>
> let firstLetterFrequencies: [Character: Int] = wordFrequencies.mapKeys { $0.characters.first! }.reduceValues(0, combine: +)<br>
><br>
> --<br>
> Brent Royal-Gordon<br>
> Architechies<br>
><br>
><br>
><br></span>
>______<span class=""><br>
<br>
--<br>
Miguel Angel Quinones<br>
<br>
_______________________________________________<br>
swift-evolution mailing list<br></span>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a> <mailto:<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><span class=""><br>
<br>
<br>
<br>
<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br>
</span></blockquote><div class="HOEnZb"><div class="h5">
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</div></div></blockquote></div><br></div></div>