<div dir="ltr">I think this probably wants to be a `reduce`. Given the above example:<div><br></div><div> ["John", "Mike", "Amy", "Kavin"].enumerate().reduce([Int: String]()) { (var dictionary, data) in</div><div> dictionary[data.index] = data.element</div><div> return dictionary</div><div> }</div><div><br></div><div>Which means you create an immutable dictionary with `let`. This could probably look nicer, but I think it illustrates the idea, at least.</div><div><br></div><div>-Ian</div></div><br><div class="gmail_quote"><div dir="ltr">On Sun, Jan 10, 2016 at 2:46 PM Craig Cruden via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">There is no equivalent to something like <div><br></div><div>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.</div></div><div style="word-wrap:break-word"><div><br></div><div><br></div><div><br><div><blockquote type="cite"><div>On 2016-01-11, at 2:41:59, Ross O'Brien via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br><div><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">Or an enumeration followed by a forEach.<div>["John", "Mike", "Amy", Kavin"].enumerate().forEach {</div><div> dic[$0] = $1</div><div>}</div><div>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.</div><div><br></div></div><div class="gmail_extra" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br><div class="gmail_quote">On Sun, Jan 10, 2016 at 7:08 PM, Donnacha Oisín Kidney<span> </span><span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span><span> </span>wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word">I think that use of map is generally discouraged. forEach would probably be more explicit, or a for-loop.<div><br><div><blockquote type="cite"><div><div><div>On 10 Jan 2016, at 18:58, 肇鑫 via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br></div></div><div><div><div><div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">You can use dictionary in a map. You just ignore the return value of the map.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default"><div class="gmail_default"><font face="georgia, serif">var dic = [Int:String]()</font></div><div class="gmail_default"><font face="georgia, serif">var index = 0</font></div><div class="gmail_default"><font face="georgia, serif"><br></font></div><div class="gmail_default"><font face="georgia, serif">["John", "Mike", "Amy", "Kavin"].map {</font></div><div class="gmail_default"><font face="georgia, serif"> <span> </span>dic.updateValue($0, forKey: index)</font></div><div class="gmail_default"><font face="georgia, serif"> <span> </span>index += 1</font></div><div class="gmail_default"><font face="georgia, serif">}</font></div><div class="gmail_default"><font face="georgia, serif"><br></font></div><div class="gmail_default"><font face="georgia, serif">print(dic) // [2: "Amy", 0: "John", 1: "Mike", 3: "Kavin"]</font></div><div class="gmail_default"><font face="georgia, serif"><br></font></div><div class="gmail_default"><font face="georgia, serif">zhaoxin</font></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jan 11, 2016 at 1:50 AM, Kenny Leung via swift-evolution<span> </span><span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span><span> </span>wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Hi All.<br><br>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><br>Is there a name for this operation? Is this something that others would like to see added to the standard library?<br><br>-Kenny<br><br>_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></blockquote></div><br><br clear="all"><div><br></div>--<span> </span><br><div><div dir="ltr"><div><br>Owen Zhao<br></div></div></div></div></div></div><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=Lo8TP3b1oIn3yQXUt9zA1UCQfR-2BMBCuqnubTuDg47-2B3usexigrc8Iux9vXvgEAL0wRBdvtEFYxIxVfpyrhimQ4cGMsUZ-2B8-2BzVACTkBFoqIlPq4LVSf1ja8P72Sb-2FyyYt9BGH7M4C6MzLueO2GJhnZPKVrov-2Bnlo8QRW-2F1jqAtsX8ozJnv60diTlz-2FdZn7ZRo7T-2FlH8GS9OrtmYfgoyV-2BeWujH-2BMRrf9qDt7r2azAtgA-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0px!important;margin:0px!important;padding:0px!important"><span> </span>_______________________________________________<span><br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></span></div></blockquote></div><br></div><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=-2F-2BoYEebT9nJ6Tn1eM4dmwhL8-2BwH5ixcf4jim0iIuN5xY0W7ouOu9HVQQhQeUBIm7Ff0vlJE62NabEh2CJFpVjDiAMCySpKeEnznQbp1IUhSZS0bBHGGQmTHG2yEsYLDy3e35v3AiIK4CRVVKSX36LtRRBaTGjosxI2fqU1ANeI1Kf4CV8ZOB69-2F7RgbxsDmeT7Oguk3uh4IRZLyW5Ff5x2nfeNsVDtFzdFDNiuemqNc-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0px!important;margin:0px!important;padding:0px!important"></div><br>_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br><br></blockquote></div><br></div><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=CGU22LnxbYa2EM3wKvzuC6syQDwKa0tMs5IyT5gL1wIZxDzcT3i4fI-2FMaphBNyprxwvedabxfmRStEDzR5hkKaDGK2pZjqFdaQ13i-2FJTnJ-2FpEMcD8ekDHgGSyhcwkPGA0aY-2BBdU2Xs0pMurVlO795mX61v4lWlkOXlp47Utt8jVBHBXvSkEfYsDpCjG8JaF0N4Md1X02hsaFkBh30oVvhPh-2FeKzPPjO0vj55shY-2FdJg-3D" alt="" width="1" height="1" border="0" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;min-height:1px!important;width:1px!important;border-width:0px!important;margin:0px!important;padding:0px!important"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important"><span> </span>_______________________________________________</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">swift-evolution mailing list</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><a href="mailto:swift-evolution@swift.org" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank">swift-evolution@swift.org</a><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a></div></blockquote></div><br></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=t5fkNsX-2F7-2FAygTFNPNE08PALgkjkO6I8wpsdssDS0ujZdPKlAHBp-2BzoWrHuaH37LZR2O-2BLaenta-2FGDrWN5IuVOZLmgKH-2Bop3Lbogy7Td76UjZaDg5aJOJ773UGD5UL-2Fgi-2Bd-2FguwI0ToAPo6j6-2Fq3YQleChbGuM8nIBFQe5SRzNZk7lQWti4UQEZUZkrAEc-2FUdrqNQqyOfwgYhWAq1zb2Lo5d5W98bYyTFdiuNKGw3HU-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important">
</div>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div>