Ah, exactly what I was looking for, thanks!<br><br>On Friday, June 16, 2017, Ben Cohen <<a href="mailto:ben_cohen@apple.com">ben_cohen@apple.com</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space">Hi – yes, there are a couple of things you can do:<div><br></div><div>You don’t have to rely on ExpressibleByArrayLiteral. RangeReplaceableCollection guarantees an empty init, so instead of [], you can write Value().</div><div><br></div><div>And the new 4.0 subscript that takes a default value allows you do avoid the if d[k] == nil dance for the initial value.</div><div><br></div><div>So you can write this as:</div><div><br></div><div><div style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="color:#ba2da2">extension</span> <span style="color:#703daa">Dictionary</span> <span style="color:#ba2da2">where</span> Value: RangeReplaceableCollection {</div><div style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">mutating</span> <span style="color:#ba2da2">func</span> append(value: <span style="color:#703daa">Value</span>.<span style="color:#703daa">Element</span>, for key: <span style="color:#703daa">Key</span>) {</div><div style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> <span style="color:#ba2da2">self</span>[key, default: <span style="color:#703daa">Value</span>()].<span style="color:#3e1e81">append</span>(value)</div><div style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"> }</div><div style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">}</div></div><div><br></div><div><br></div><div><br></div><div><br></div><div><div><blockquote type="cite"><div>On Jun 15, 2017, at 3:12 PM, Jens Persson via swift-users <<a href="javascript:_e(%7B%7D,'cvml','swift-users@swift.org');" target="_blank">swift-users@swift.org</a>> wrote:</div><br><div><div dir="ltr"><div>let question = """</div><div> Please see the extension to Dictionary below.</div><div> Is there a simpler or better way to accomplish the same thing?</div><div> """</div><div>extension Dictionary where</div><div> Value: RangeReplaceableCollection,</div><div> Value: ExpressibleByArrayLiteral,</div><div> Value.Element == Value.ArrayLiteralElement</div><div>{</div><div> mutating func append(value: Value.Element, for key: Key) {</div><div> if self[key]?.append(value) == nil {</div><div> self[key] = [value]</div><div> }</div><div> }</div><div>}</div><div>func usageExample() {</div><div> var characterIndexMap = [Character : [Int]]()</div><div> for (ci, c) in question.characters.<wbr>enumerated() {</div><div> characterIndexMap.append(<wbr>value: ci, for: c)</div><div> }</div><div> for (character, indices) in characterIndexMap</div><div> .sorted(by: { $0.1.count > $1.1.count })</div><div> {</div><div> print("\(character.<wbr>debugDescription) occurs at indices:", indices)</div><div> }</div><div>}</div><div>usageExample()</div><div><br></div><div><br></div><div>/* Will print:</div><div><div>" " occurs at indices: [6, 10, 14, 24, 27, 38, 48, 54, 56, 64, 67, 74, 78, 81, 92, 96, 101]</div><div>"e" occurs at indices: [2, 5, 8, 9, 13, 15, 18, 40, 51, 53, 62, 69, 72, 95, 100]</div><div>"t" occurs at indices: [11, 17, 25, 31, 49, 70, 71, 79, 93, 102]</div><div>"s" occurs at indices: [4, 7, 20, 47, 57, 90, 97]</div><div>"o" occurs at indices: [22, 26, 33, 42, 65, 80, 85]</div><div>"i" occurs at indices: [21, 29, 32, 58, 89, 104]</div><div>"a" occurs at indices: [3, 35, 55, 76, 82, 98]</div><div>"h" occurs at indices: [12, 50, 91, 94, 103]</div><div>"r" occurs at indices: [36, 52, 63, 66, 73]</div><div>"l" occurs at indices: [1, 41, 61, 88]</div><div>"n" occurs at indices: [19, 23, 34, 105]</div><div>"m" occurs at indices: [59, 86, 99]</div><div>"c" occurs at indices: [30, 83, 84]</div><div>"p" occurs at indices: [60, 87]</div><div>"w" occurs at indices: [43, 75]</div><div>"y" occurs at indices: [37, 77]</div><div>"b" occurs at indices: [39, 68]</div><div>"." occurs at indices: [44]</div><div>"\n" occurs at indices: [45]</div><div>"I" occurs at indices: [46]</div><div>"x" occurs at indices: [16]</div><div>"?" occurs at indices: [107]</div><div>"g" occurs at indices: [106]</div><div>"D" occurs at indices: [28]</div><div>"P" occurs at indices: [0]</div></div><div>*/</div><div><br></div></div>
______________________________<wbr>_________________<br>swift-users mailing list<br><a href="javascript:_e(%7B%7D,'cvml','swift-users@swift.org');" target="_blank">swift-users@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br></div></blockquote></div><br></div></div></blockquote>