[swift-evolution] map-like operation that returns a dictionary

Ross O'Brien narrativium+swift at gmail.com
Sun Jan 10 13:41:59 CST 2016


Or an enumeration followed by a forEach.
["John", "Mike", "Amy", Kavin"].enumerate().forEach {
    dic[$0] = $1
}
That said, it requires creating a dictionary var first, not a let. If there
was an initialiser for Dictionary which took an Array or EnumerateSequence,
that might be useful. I'm not sure how I'd attempt to write such an
initialiser though.


On Sun, Jan 10, 2016 at 7:08 PM, Donnacha Oisín Kidney <
swift-evolution at swift.org> wrote:

> I think that use of map is generally discouraged. forEach would probably
> be more explicit, or a for-loop.
>
> On 10 Jan 2016, at 18:58, 肇鑫 via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> You can use dictionary in a map. You just ignore the return value of the
> map.
>
> var dic = [Int:String]()
> var index = 0
>
> ["John", "Mike", "Amy", "Kavin"].map {
>     dic.updateValue($0, forKey: index)
>     index += 1
> }
>
> print(dic) // [2: "Amy", 0: "John", 1: "Mike", 3: "Kavin"]
>
> zhaoxin
>
> On Mon, Jan 11, 2016 at 1:50 AM, Kenny Leung via swift-evolution <
> swift-evolution at swift.org> wrote:
>
>> Hi All.
>>
>> I find that instead of using map() on arrays, I more often use an
>> operation that returns a dictionary from an array. A common case is
>> fetching an array of data, then creating a local cache of it indexed by ID.
>>
>> Is there a name for this operation? Is this something that others would
>> like to see added to the standard library?
>>
>> -Kenny
>>
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>
>
>
>
> --
>
> Owen Zhao
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
>
> _______________________________________________
> 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/20160110/31066e51/attachment.html>


More information about the swift-evolution mailing list