<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 style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">struct</span> Person {</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">var</span> id: <span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">Int</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">var</span> name: <span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">String</span></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">}</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">let</span> tom &nbsp; = <span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">Person</span>(id: <span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">1</span>, name: <span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">"Tom"</span>)</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">let</span> dick&nbsp; = <span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">Person</span>(id: <span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">2</span>, name: <span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">"Dick"</span>)</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">let</span> harry = <span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">Person</span>(id: <span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">3</span>, name: <span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">"Harry"</span>)</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">let</span> people = [<span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">tom</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">dick</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">harry</span>]</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150); min-height: 15px;" class=""><br class=""></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(181, 137, 1);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #839496" class=""> </span>SequenceType<span style="font-variant-ligatures: no-common-ligatures; color: #839496" class=""> {</span></div><p style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150); min-height: 15px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">&nbsp; &nbsp;&nbsp;<span style="color: rgb(133, 153, 1);" class="">func</span> toDict&lt;Key: <span style="color: rgb(181, 137, 1);" class="">Hashable</span>&gt;(<span style="color: rgb(133, 153, 1);" class="">@noescape</span> withKey extractKey: <span style="color: rgb(181, 137, 1);" class="">Self</span>.<span style="color: rgb(181, 137, 1);" class="">Generator</span>.<span style="color: rgb(181, 137, 1);" class="">Element</span> -&gt; <span style="color: rgb(181, 137, 1);" class="">Key</span>)&nbsp;</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>-&gt; [<span style="color: rgb(181, 137, 1);" class="">Key</span>:<span style="color: rgb(181, 137, 1);" class="">Self</span>.<span style="color: rgb(181, 137, 1);" class="">Generator</span>.<span style="color: rgb(181, 137, 1);" class="">Element</span>]</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">&nbsp; &nbsp; {</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">toDict</span> { element <span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">in</span> (extractKey(element), element) }</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">&nbsp; &nbsp; }</div><p style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150); min-height: 15px;" class="">&nbsp; &nbsp;&nbsp;<br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">func</span> toDict&lt;Key: <span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">Hashable</span>, Value&gt;(<span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">@noescape</span> mapping: <span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">Self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">Generator</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">Element</span> -&gt; (<span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">Key</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">Value</span>))</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">&nbsp; &nbsp; &nbsp; &nbsp; -&gt; [<span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">Key</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">Value</span>]</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">&nbsp; &nbsp; {</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">var</span> result: [<span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">Key</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">Value</span>] = [:]</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">for</span> element <span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">in</span> <span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">self</span> {</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">let</span> (key, value) = mapping(element)</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.<span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">updateValue</span>(value, forKey: key)</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">return</span> result</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class="">}</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150); min-height: 15px;" class=""><br class=""></div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">let</span> cache = <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">people</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">toDict</span> { $0.<span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">id</span> }</div><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(211, 54, 130);" class="">cache</div><div class=""><br class=""></div><div class="">I named the method(s) „toDict“ instead of „map“ because map normally returns a collection which is either the same as the receiver or a simple one.</div><div class="">The second version is more general and allows to do things like</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">let</span> dict = [<span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">"Tom"</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">"Dick"</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">"Harry"</span>].<span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">enumerate</span>().<span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">toDict</span> { (index, value) <span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">in</span> (index + <span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">1</span>, value) }</div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">-Thorsten</div><div class=""><br class=""></div><div class=""><br class=""></div><div><blockquote type="cite" class=""><div class="">Am 11.01.2016 um 17:56 schrieb Kenny Leung via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt;:</div><br class="Apple-interchange-newline"><div class=""><div class="">I would like to keep this lightweight for the consumer, so you could write:<br class=""><br class="">let people :[Person] = fetchPeople()<br class="">let cache :[Int:Person] = people.map {return $0.id}<br class=""><br class="">-Kenny<br class=""><br class=""><br class=""><blockquote type="cite" class="">On Jan 10, 2016, at 2:24 PM, Jo Albright via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class="">Possible solution to do what you are wanting. <br class=""><br class="">extension Array {<br class=""><br class=""> &nbsp;&nbsp;&nbsp;func map&lt;T&gt;(var d: [String:T] = [:], @noescape transform: (Int,Array.Generator.Element) throws -&gt; [String:T]) rethrows -&gt; [String:T] {<br class=""><br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (i,item) in self.enumerate() {<br class=""><br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try d += transform(i,item)<br class=""><br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br class=""><br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return d<br class=""><br class=""> &nbsp;&nbsp;&nbsp;}<br class=""><br class="">}<br class=""><br class="">func += &lt;T&gt;(inout lhs: [String:T], rhs: [String:T]) -&gt; [String:T] {<br class=""><br class=""> &nbsp;&nbsp;&nbsp;for (k,v) in rhs { lhs[k] = v }; return lhs<br class=""><br class="">}<br class=""><br class=""><br class="">let names = ["Jo","Jenna","Jake","Julie"]<br class="">let ages = [32,20,46,39]<br class=""><br class="">let namesInfo1 = names.map { ["\($0)": $1] }<br class="">namesInfo1 // ["2": "Jake", "1": "Jenna", "0": "Jo", "3": "Julie"]<br class=""><br class="">let namesInfo2 = names.map(["6":"Jim"]) { ["\($0)": $1] }<br class="">namesInfo2 // ["2": "Jake", "1": "Jenna", "0": "Jo", "6": "Jim", "3": "Julie"]<br class=""><br class="">let namesInfo3 = names.map { [$1: ages[$0]] }<br class="">namesInfo3 // ["Jenna": 20, "Jo": 32, "Jake": 46, "Julie": 39]<br class=""><br class="">Designer . Developer .  Nerd <br class="">Jo Albright<br class=""><br class=""><br class=""><blockquote type="cite" class="">On Jan 10, 2016, at 4:25 PM, Loïc Lecrenier via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class="">I think this is O(n^2) though, so it’s not really usable.<br class="">(It is destroying and copying the dictionary each time)<br class=""><br class="">There really isn’t anything like map for dictionaries. <br class="">I am not convinced we should add it to the standard library though.<br class=""><br class=""><blockquote type="cite" class="">On Jan 10, 2016, at 10:19 PM, Ian Ynda-Hummel via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class="">I think this probably wants to be a `reduce`. Given the above example:<br class=""><br class=""> &nbsp;&nbsp;["John", "Mike", "Amy", "Kavin"].enumerate().reduce([Int: String]()) { (var dictionary, data) in<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dictionary[data.index] = data.element<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return dictionary<br class=""> &nbsp;&nbsp;}<br class=""><br class="">Which means you create an immutable dictionary with `let`. This could probably look nicer, but I think it illustrates the idea, at least.<br class=""><br class="">-Ian<br class=""><br class="">On Sun, Jan 10, 2016 at 2:46 PM Craig Cruden via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class="">There is no equivalent to something like <br class=""><br class="">array.zipWithIndex.toMap (or in this case array.zipWithIndex.toDictionary // where zipWithIndex creates tuples with the index - a specialized case of zip for Arrays with indexes.<br class=""><br class=""><br class=""><br class=""><blockquote type="cite" class="">On 2016-01-11, at 2:41:59, Ross O'Brien via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class="">Or an enumeration followed by a forEach.<br class="">["John", "Mike", "Amy", Kavin"].enumerate().forEach {<br class=""> &nbsp;&nbsp;dic[$0] = $1<br class="">}<br class="">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.<br class=""><br class=""><br class="">On Sun, Jan 10, 2016 at 7:08 PM, Donnacha Oisín Kidney &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class="">I think that use of map is generally discouraged. forEach would probably be more explicit, or a for-loop.<br class=""><br class=""><blockquote type="cite" class="">On 10 Jan 2016, at 18:58, 肇鑫 via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class="">You can use dictionary in a map. You just ignore the return value of the map.<br class=""><br class="">var dic = [Int:String]()<br class="">var index = 0<br class=""><br class="">["John", "Mike", "Amy", "Kavin"].map {<br class=""> &nbsp;&nbsp;dic.updateValue($0, forKey: index)<br class=""> &nbsp;&nbsp;index += 1<br class="">}<br class=""><br class="">print(dic) // [2: "Amy", 0: "John", 1: "Mike", 3: "Kavin"]<br class=""><br class="">zhaoxin<br class=""><br class="">On Mon, Jan 11, 2016 at 1:50 AM, Kenny Leung via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class="">Hi All.<br class=""><br class="">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.<br class=""><br class="">Is there a name for this operation? Is this something that others would like to see added to the standard library?<br class=""><br class="">-Kenny<br class=""><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""><br class=""><br class=""><br class="">-- <br class=""><br class="">Owen Zhao<br class="">_______________________________________________<br class="">swift-evolution mailing list<br class="">swift-evolution@swift.org<br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></blockquote><br class=""><br class=""><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""><br class=""><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class="">swift-evolution@swift.org<br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></blockquote><br class=""><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class="">_______________________________________________<br class="">swift-evolution mailing list<br class="">swift-evolution@swift.org<br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></blockquote><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></blockquote><br class=""><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></blockquote><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></div></blockquote></div><br class=""></body></html>