<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 class="">A brief draft is below... I had mostly written this up before I saw the thread and Gwendal's similar contribution -- happy to hear feedback and fold in comments/revisions!</div><div class=""><div class=""><br class=""></div><div class="">Nate</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">---</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><b class="">Introduction</b><br class=""><br class="">The Dictionary type should allow initialization from a sequence of (Key, Value) tuples.<br class=""><br class=""><b class="">Motivation</b><br class=""><br class="">Array and Set both have initializers that create a new instance from a sequence of elements. The Array initializer is useful for converting other sequences and collections to the "standard" collection type, but the Set initializer is essential for recovering set operations after performing any functional operations on a set. For example, filtering a set produces a collection without any kind of set operations available:</div><div class=""><br class=""></div><div class=""><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> numberSet = <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Set</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span> ... <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">100</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> fivesOnly = <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">numberSet</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">lazy</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">filter</span> { $0 % <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">5</span> == <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span> }</div></div><div class=""><br class=""></div><div class="">"fivesOnly" is a LazyFilterCollection<Set<Int>> instead of a Set -- sending that back through the Set sequence initializer restores the expected methods:</div><div class=""><br class=""></div><div class=""><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> fivesOnlySet = <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Set</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">numberSet</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">lazy</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">filter</span> { $0 % <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">5</span> == <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span> })</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class="">fivesOnlySet<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">isSubsetOf</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span>numberSet<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">) </span><span style="color: rgb(0, 132, 0);" class="">// true</span></div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="color: rgb(0, 132, 0);" class=""><br class=""></span></div><div class="">Dictionary, on the other hand, has no such initializer, so a similar operation leaves no room except for building a mutable Dictionary via iteration or functional methods with dubious performance. These techniques also don't support type inference from the source sequence, increasing verbosity:</div><div class=""><br class=""></div><div class=""><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="">var</span> viaIteration: [<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span>: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</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> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">evenOnly</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: #4f8187" class="">viaIteration</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; 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> viaFunction: [<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span>: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span>] = <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">evenOnly</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">reduce</span>([:]) { (cumulative, keyValue) <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</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="">var</span> mutableDictionary = cumulative</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> mutableDictionary[keyValue.<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span>] = keyValue.<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</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="">return</span> mutableDictionary</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div><div class=""><br class=""></div><div class=""><b class="">Proposed solution</b><br class=""><br class="">The proposed solution would add an initializer to Dictionary that accepts any sequence of (Key, Value) tuple pairs, matching the Dictionary's element type when treated as a sequence:</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="">init</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><S: </span>SequenceType<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">where</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">S</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span>Generator<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span>Element<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> == </span>Generator<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span>Element<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">>(</span><span style="color: rgb(187, 44, 162);" class="">_</span><span style="color: rgb(0, 0, 0);" class=""> </span><span style="color: rgb(0, 0, 0);" class="">sequence: </span><span style="color: rgb(79, 129, 135);" class="">S</span><span style="color: rgb(0, 0, 0);" class="">)</span></div></div><div class=""><br class=""></div><div class="">Instead of the techniques for recovering a Dictionary shown above, the proposed initializer would allow a much cleaner syntax to be written:</div><div class=""><br class=""></div><div class=""><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> viaProposed = <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Dictionary</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">evenOnly</span>)</div></div><div class=""><br class=""></div><div class="">Moreover, this new initializer would allow for some convenient uses that aren't currently possible.</div><div class=""><br class=""></div><div class="">👍🏼 Initializing from an array of tuples: </div><div class=""><br class=""></div><div class=""><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> dictFromArray = <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Dictionary</span>([(<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"a"</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="">"b"</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="">"c"</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">3</span>), (<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"d"</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">4</span>)])</div></div><div class=""><br class=""></div><div class="">👏🏼 Initializing from a DictionaryLiteral (the type, not an actual literal): </div><div class=""><br class=""></div><div class=""><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> literal: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">DictionaryLiteral</span> = [<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"a"</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="">"b"</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="">"c"</span>: <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">3</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"d"</span>: <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">4</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> dictFromDL = <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Dictionary</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">literal</span>)</div></div><div class=""><br class=""></div><div class="">🎉 Initializing from a pair of zipped sequences (examples abound): </div><div class=""><br class=""></div><div class=""><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> letters = <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"abcdefghij"</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">characters</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">lazy</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">map</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span>($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: #bb2ca2" class="">let</span> dictFromZip = <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Dictionary</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">zip</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">letters</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span>...<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">10</span>))</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">// ["b": 2, "a": 1, "i": 9, "j": 10, "c": 3, "e": 5, "f": 6, "g": 7, "d": 4, "h": 8]</div></div><div class=""><br class=""></div><div class=""><b class="">Potential pitfalls</b></div><div class=""><br class=""></div><div class=""><div class="">One caveat is that the new initializer doesn't prevent using a sequence with multiple identical keys. In such a case, the last key/value would "win" and exist in the dictionary. Such an initialization is a compile-time error with a dictionary literal, but succeeds under the new initializer:</div></div><div class=""><br class=""></div><div class=""><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> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span> = [<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"z"</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="">"z"</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="">"z"</span>: <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">3</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"z"</span>: <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">4</span>]</div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class="">// fatal error: Dictionary literal contains duplicate keys</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Dictionary</span>([(<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"z"</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="">"z"</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="">"z"</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">3</span>), (<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"z"</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">4</span>)])</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">// ["z": 4]</div></div><div class=""><br class=""></div><div class="">This behavior is particularly troublesome when used in conjunction with a mapping operation that modifies a dictionary's keys, since dictionaries have no particular guaranteed order:</div><div class=""><br class=""></div><div class=""><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> overlapping = <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Dictionary</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">dictFromArray</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">lazy</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">map</span> { (<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span>, value) <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span> (<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"z"</span>, value) })</div></div><div class=""><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">// ["z": ???]</div></div></div><div class=""><br class=""></div><div class="">While a pitfall, this behavior is less a symptom of the proposed API and more an inherent problem with recovering a dictionary after modifying its keys. The current ways of rebuilding a dictionary (as shown above) are just as susceptible to silently dropping values. Moreover, the sequence-based initializer for Set exhibits the same behavior, though slightly less problematic in most cases:</div><div class=""><br class=""></div><div class=""><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> dividedNumbers = <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Set</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">numberSet</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">map</span> { $0 / <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">20</span> })</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">// {4, 5, 2, 0, 1, 3}</div></div><div class=""><br class=""></div><div class="">Given the potential lossiness of the initializer, should it use a parameter name for the sequence? I would suggest not, to match the syntax of Array.init(_:) and Set.init(_:), but a parameter like "collapsingKeys" would make the risk clear to users.</div><div class=""><br class=""></div><div class=""><b class="">Detailed design</b><br class=""><br class="">The implementation is simple enough to show in the proposal:</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; color: rgb(0, 132, 0);" class=""> /// Creates a dictionary with the keys and values in the given sequence.</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="">Generator</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Element</span>>(<span style="color: rgb(187, 44, 162);" class="">_</span> sequence: <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> sequence {</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="">updateValue</span>(value, forKey: key)</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="">}</div></div><div class=""><br class=""></div><div class="">(As part of the standard library, this could use the <span style="color: rgb(51, 51, 51); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; white-space: pre; background-color: rgb(255, 255, 255);" class="">nativeUpdateValue</span> method.)</div><div class=""><br class=""></div><div class=""><b class="">Impact on existing code</b><br class=""><br class="">As a new API, this will have no impact on existing code.</div><div class=""><br class=""><b class="">Alternatives considered</b><br class=""><br class="">As suggested in the thread below, a method could be added to SequenceType that would build a dictionary. This approach seems less of a piece with the rest of the standard library, and overly verbose when used with a Dictionary that is only passing through filtering or mapping operations. I don't think the current protocol extension system could handle a passthrough case (i.e., something like "extension SequenceType where Generator.Element == (Key, Value)").</div><div class=""><br class=""></div><div class="">Alternately, the status quo could be maintained. Which would be sad.<br class=""><br class=""><div class=""><br class=""></div><div class=""><div class=""><br class=""></div><div class=""><br class=""></div><div><blockquote type="cite" class=""><div class="">On Jan 13, 2016, at 11:55 AM, Gwendal Roué via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div 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="" class=""> </span>Dictionary<span style="" 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="" 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="" 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="" 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=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><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=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></blockquote><br class="">
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=4GlEpM11j-2BGED-2FudNhzzUKGouoeTKtIEqwsP-2B6t6PzlLS9cRtns4icBGsYUTnCmj8LnkoNBzVOCJ45QQriZM4lEAvlKyxUI6Wazz4JQdRVnDirAMgxjHISu7M26QEXtGrlkHJ4Qzgd6uLqNV8Y2-2BGyHiljSVgsocdgmsopqmmHUUJczcYH0bcJ4cjIZ8yCLPhirBc0lxD9OrxylPhh32PpidsY7MprU7CLI-2FsbX89Vo-3D" alt="" width="1" height="1" border="0" style="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;" class="">
</div>
_______________________________________________<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></blockquote></div><br class=""></div></div></div></body></html>