[swift-evolution] [Pitch] Add `mapValues` method to Dictionary

Brent Royal-Gordon brent at architechies.com
Tue May 24 00:59:14 CDT 2016


> I have a small remark though, wouldn’t it be better to let transform be of type (Key, Value) throws -> T instead of (Value) throws -> T? You can just ignore the key (with _) if you don’t need it, but I think it might come in handy in some cases.

The problem is, that closes the door to writing many simple maps in functional style. For instance, this:

	dictionaryOfNumbers.mapValues(abs)

Would have to become this:

	dictionaryOfNumbers.mapValues { _, v in abs(v) }

(It *might* be possible to do it with `$1`, but I'm not sure; there are some limitations around that.)

A value-value map is just simpler and cleaner, while almost always giving you what you need.

-- 
Brent Royal-Gordon
Architechies



More information about the swift-evolution mailing list