[Proposal]Make .map return the calling collection type


(Yogev Sitton) #1

Map is great for transforming an array of objects - it is very useful and I use it a lot.
I would love to use Map with Dictionaries and other collections as well.

As an example - let’s say I have a dictionary [String : Person] - that maps a person to an ID.
I want to use the map function a get a [String : String] - a dictionary that maps a person’s name to an ID.
Currently - the map method is defined to always return an array so that is not possible to do.

Obviously there are a lot of ways to accomplish the same task - but it would be very nice to be able to use map for other types than Array.


(Dmitri Gribenko) #2

Hi,

We can discuss adding special-case functions to individual collections
where it makes sense, but it does not make sense to do it for
collections in general, please see
https://github.com/apple/swift/blob/master/docs/StdlibRationales.rst#high-order-functions-on-collections-return-arrays
for the rationale.

Dmitri

···

On Tue, Apr 12, 2016 at 1:55 AM, Yogev Sitton via swift-evolution <swift-evolution@swift.org> wrote:

Map is great for transforming an array of objects - it is very useful and I use it a lot.
I would love to use Map with Dictionaries and other collections as well.

As an example - let’s say I have a dictionary [String : Person] - that maps a person to an ID.
I want to use the map function a get a [String : String] - a dictionary that maps a person’s name to an ID.
Currently - the map method is defined to always return an array so that is not possible to do.

--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr@gmail.com>*/