[swift-evolution] [Proposal] mapValues
Maximilian Hünenberger
m.huenenberger at me.com
Mon Apr 18 11:29:02 CDT 2016
I like the idea of having a mapping method which maps back to the actual type.
What about a more general approach? consider:
mappedSelf(transform: T -> U) -> Self<U>
filteredSelf(includeElement: T -> Bool) -> Self<U>
Which can be added to other collections like Set and sequences like a lazy number sequence.
Best regards
- Maximilian
> Am 13.04.2016 um 08:41 schrieb Jonathan Hull via swift-evolution <swift-evolution at swift.org>:
>
> I would really like to see something like the following added to the standard library:
>
> extension Dictionary {
>
> func mapValues<U>(transform:(Key,Value)->U)->[Key:U] {
> var output:[Key:U] = [:]
> for (k,v) in self {
> output[k] = transform(k,v)
> }
> return output
> }
>
> }
>
> It comes up enough that I have had to add it to pretty much every one of my projects. I also don’t feel comfortable adding it to my frameworks, since I figure a lot of people are also adding something like this to their projects, and I don’t want to cause a conflict with their version. Prime candidate for the standard library.
>
> I like calling it ‘mapValues' as opposed to providing an override for map, since it makes the specific behavior more clear. I would expect ‘map' to possibly map the keys as well (though there are issues where the new keys overlap). I suppose you could just have a bunch of overrides for map if the compiler becomes good enough at differentiating return types: (Value)->(Value), (Key,Value)->Value, (Key, Value)->(Key,Value)
>
> Thanks,
> Jon
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160418/2ded35b7/attachment.html>
More information about the swift-evolution
mailing list