<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Doesn’t Swift prefer initializers?</div><div class=""><br class=""></div><div class="">So let’s build a Dictionary initializer that eats any sequence of (key, value) pairs:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>Dictionary<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="">init</span><S: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">SequenceType</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">where</span> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">S</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Generator</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Element</span> == (<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Key</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Value</span>)>(keyValueSequence s: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">S</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="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</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="">for</span> (key, value) <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span> s {</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="">self</span>[key] = value</div><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;" class=""> }</div><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="">do</span> {</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: #000000" class=""> </span>// From array of (key, value) pairs</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> input = [(<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"foo"</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span>), (<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"bar"</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">2</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> d = <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Dictionary</span>(keyValueSequence: input)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span>(d)</div><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;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">do</span> {</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: #000000" class=""> </span>// From another dictionary</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> input = [<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span>: <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"foo"</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">2</span>: <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"bar"</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> d = <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Dictionary</span>(keyValueSequence: input)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span>(d)</div><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;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">do</span> {</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: #000000" class=""> </span>// Reverse key and values</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> input = [<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span>: <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"foo"</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">2</span>: <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"bar"</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> d = <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Dictionary</span>(keyValueSequence: input.<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">map</span> { ($1, $0) })</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span>(d)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div><div class=""><br class=""></div><div class="">Gwendal</div><br class=""><blockquote type="cite" class="">Le 13 janv. 2016 à 18:41, Thorsten Seitz via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> a écrit :<br class=""><br class="">I'd prefer "mapToDict" otherwise it sounds like a dictionary gets mapped, at least for me.<br class=""><br class="">-Thorsten<br class=""><br class=""><blockquote type="cite" class="">Am 13.01.2016 um 17:13 schrieb Kenny Leung via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>>:<br class=""><br class="">This solution looks great! How do you feel about “mapDict”?<br class=""><br class="">-Kenny<br class=""><br class=""><br class=""><blockquote type="cite" class=""><blockquote type="cite" class="">On Jan 12, 2016, at 10:28 AM, Craig Cruden <<a href="mailto:ccruden@novafore.com" class="">ccruden@novafore.com</a>> wrote:<br class=""><br class=""><br class=""><br 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.<br class="">The second version is more general and allows to do things like<br class=""><br class="">let dict = ["Tom", "Dick", "Harry"].enumerate().toDict { (index, value) in (index + 1, value) }<br class=""></blockquote><br class="">Map would probably be a more correct mathematically speaking — but it would be inconsistent with the naming convention already chosen for Swift. So for Swift - toDict (or toDictionary) would be the best choice.<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="">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=""></body></html>