<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="">Possible solution to do what you are wanting.&nbsp;<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class="">extension<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Array</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> map&lt;T&gt;(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> d: [<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span>:T] = [:], <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">@noescape</span> transform: (<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span>,<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Array</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Generator</span>.Element) <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">throws</span> -&gt; [<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span>:T]) <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">rethrows</span> -&gt; [<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>] {</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;<br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">for</span> (i,item) <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">enumerate</span>() {</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">try</span> d <span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">+=</span> transform(i,item)</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;<br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> d</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;<br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> += &lt;T&gt;(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">inout</span> lhs: [<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>], rhs: [<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>]) -&gt; [<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>] {</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">for</span> (k,v) <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span> rhs { lhs[k] = v }; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> lhs</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> names = [</span>"Jo"<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">,</span>"Jenna"<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">,</span>"Jake"<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">,</span>"Julie"<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">]</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> ages = [<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">32</span>,<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">20</span>,<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">46</span>,<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">39</span>]</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;<br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> namesInfo1 = <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">names</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">map</span> { [<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"</span>\<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">(</span>$0<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">)"</span>: $1] }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">namesInfo1</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>// ["2": "Jake", "1": "Jenna", "0": "Jo", "3": "Julie"]</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> namesInfo2 = <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">names</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">map</span>([<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"6"</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"Jim"</span>]) { [<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"</span>\<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">(</span>$0<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">)"</span>: $1] }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">namesInfo2</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>// ["2": "Jake", "1": "Jenna", "0": "Jo", "6": "Jim", "3": "Julie"]</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> namesInfo3 = <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">names</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">map</span> { [$1: <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">ages</span>[$0]] }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">namesInfo3</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>// ["Jenna": 20, "Jo": 32, "Jake": 46, "Julie": 39]</div><div class="">
<div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class="Apple-interchange-newline"><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class=""><font color="#8d8d8d" class="">Designer . Developer .  Nerd&nbsp;</font></span><font color="#464646" class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">Jo Albright</span></font><br style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""></div>
</div>
<br class=""><div><blockquote type="cite" class=""><div 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:</div><br class="Apple-interchange-newline"><div class=""><div 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;&nbsp;["John", "Mike", "Amy", "Kavin"].enumerate().reduce([Int: String]()) { (var dictionary, data) in<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dictionary[data.index] = data.element<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return dictionary<br class=""> &nbsp;&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;&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;&nbsp;dic.updateValue($0, forKey: index)<br class=""> &nbsp;&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=""></div></div></blockquote></div><br class=""></div></body></html>