<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 class=""><br class=""></div><div class="">I am definitely +1 for adding the initializer in any case. </div><div class=""><br class=""></div><div class="">I would like to see it (based on Nate’s suggestion) have a “merge" parameter which takes a closure of (Key, Value, Value)throws->Value, which would be called to choose a value whenever a repeated key is encountered. That parameter should have a default value which just traps. That way the default behavior is to trap when a key is repeated, but it can still be overridden with a more appropriate behavior for the situation (e.g. keeping the first value, keeping the last, averaging them, etc…)</div><div class=""><br class=""></div><div class="">That said, I would still also like to see the functionality of mapValues (whatever it ends up being called) in the standard library. It easily applies to 80-90% of my use cases, and allowing re-mapping of keys adds a lot of complexity which must be carefully considered (there is a lot more which can go wrong). As swift is a practical language, it would be nice to have a quick and foolproof way to do this common task.</div><div class=""><br class=""></div><div class="">Thanks,</div><div class="">Jon</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><pre style="white-space: pre-wrap; background-color: rgb(255, 255, 255);" class="">on Tue Apr 12 2016, Jonathan Hull <<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">swift-evolution at swift.org</a>> wrote:
><i class=""> I would really like to see something like the following added to the standard
</i>><i class=""> library:
</i>><i class="">
</i>><i class=""> extension Dictionary {
</i>><i class="">
</i>><i class=""> func mapValues<U>(transform:(Key,Value)->U)->[Key:U] {
</i>><i class=""> var output:[Key:U] = [:]
</i>><i class=""> for (k,v) in self {
</i>><i class=""> output[k] = transform(k,v)
</i>><i class=""> }
</i>><i class=""> return output
</i>><i class=""> }
</i>><i class="">
</i>><i class=""> }
</i>><i class="">
</i>><i class=""> It comes up enough that I have had to add it to pretty much every one of my
</i>><i class=""> projects. I also don’t feel comfortable adding it to my frameworks, since I
</i>><i class=""> figure a lot of people are also adding something like this to their projects,
</i>><i class=""> and I don’t want to cause a conflict with their version. Prime candidate for the
</i>><i class=""> standard library.
</i>><i class="">
</i>><i class=""> I like calling it ‘mapValues' as opposed to providing an override for map, since
</i>><i class=""> it makes the specific behavior more clear. I would expect ‘map' to possibly map
</i>><i class=""> the keys as well (though there are issues where the new keys overlap). I suppose
</i>><i class=""> you could just have a bunch of overrides for map if the compiler becomes good
</i>><i class=""> enough at differentiating return types: (Value)->(Value), (Key,Value)->Value,
</i>><i class=""> (Key, Value)->(Key,Value)
</i>
I agree that we need a way to do this, and was surprised when what I
tried didn't work. This should work:
Dictionary(d.lazy.map { (k, v) in (k, transform(v)) })
We should have a proposal that makes the constructor work* if we don't
already have one.
I'm inclined against building specialized variants of basic algorithms
into particular collections, though. Could be talked out of it if the
use-case is strong enough.</pre></blockquote><div class=""><br class=""></div></div></body></html>