<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=""><br class=""><div><blockquote type="cite" class=""><div class="">Am 06.06.2017 um 09:02 schrieb Jens Persson via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>>:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="">When compiling that from the command line, I get the following (after about 6 seconds):<br class=""></div><div class=""><br class=""></div><div class=""><div class="">test.swift:7:18: error: cannot assign through subscript: 'dp' is a 'let' constant</div><div class=""> dp[0][0] = 0</div><div class=""> ~~ ^</div><div class="">/.../</div></div><div class=""><br class=""></div><div class="">After fixing that (changing let dp to var dp), it will compile successfully, still taking a very long time though. This usually means that some expression(s) in the code happen to be a bit hard on the type checker (in its current state).</div><div class="">I tried the latest dev snapshot and it is a bit faster, perhaps 3 s instead of 6.</div><div class=""><br class=""></div><div class="">Anyway, here is a logically equivalent rewrite which will compile faster:</div><div class=""><br class=""></div><div class=""><div class="">class Solution {</div><div class=""> func rob(nums: [Int]) -> Int {</div><div class=""> guard nums.count > 0 else { return 0 }</div><div class=""> var dp = Array.init(repeating: Array.init(repeating: 0, count: nums.count),</div><div class=""> count: 2)</div><div class=""> dp[0][0] = 0</div><div class=""> dp[0][1] = nums[0]</div><div class=""> for i in 1 ..< nums.count {</div><div class=""> <span style="white-space:pre-wrap" class=""> </span>let dp_iMinus1_0 = dp[i - 1][0] </div><div class=""> <span style="white-space:pre-wrap" class=""> </span>let dp_iMinus1_1 = dp[i - 1][1] </div><div class=""> dp[i][0] = max(dp_iMinus1_0, dp_iMinus1_1)</div><div class=""> dp[i][1] = dp_iMinus1_0 + nums[i]</div></div></div></div></blockquote><div><br class=""></div><div>Just a nitpick: this isn’t functionally equivalent to the original code (dp[i][1] = dp[i][0] + nums[i]), because dp[i][0] might have actually changed on the previous line. But you can return this line to the original version without any effect on the compile time (less than 250ms on my machine).</div><div><br class=""></div><div>I think the compile time issue is that max() is a generic function that takes any Comparable, so the type checker seems to go berserk trying to ensure the term is satisfiable.</div><div><br class=""></div><div>If you create a non-generic replacement for max in the same file:</div><div><br class=""></div><div><div>private func myMax(_ x1: Int, _ x2: Int) -> Int {</div><div> return (x1 >= x2) ? x1 : x2</div><div>}</div></div><div><br class=""></div><div>… and replace the assignment to <i class="">max(dp[i - 1][0], ……)</i> in the for loop with <i class="">myMax(dp[i - 1][0], ….)</i></div><div><br class=""></div><div>The compile time will be just as fast as the one that swaps out the internal elements (on my machine it’s actually about 10% faster with the non-generic <i class="">max</i>).</div><div><br class=""></div><div>Regards,</div><div>Geordie</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class=""><div class=""> }</div><div class=""> return 0</div><div class=""> }</div><div class="">}</div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Mon, Jun 5, 2017 at 2:32 PM, Hbucius Smith via swift-users <span dir="ltr" class=""><<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class=""><div class="gmail_default" style="font-family:simsun,serif;font-size:small">Hi Swift-Users,</div><div class="gmail_default" style="font-family:simsun,serif;font-size:small"><br class=""></div><div class="gmail_default" style="font-family:simsun,serif;font-size:small"> when I compiled the code, Xcode cannot stop, I do not know why. It is very strange. Can anyone help ? Here is the code. I am using Xcode 8.1</div><div class="gmail_default" style="font-family:simsun,serif;font-size:small"><br class=""></div><div class="gmail_default" style="font-family:simsun,serif;font-size:small"><p class="m_9138524867132845885gmail-p1"><span class="m_9138524867132845885gmail-s2">class</span><span class="m_9138524867132845885gmail-s1"> Solution {</span><br class=""><span class="m_9138524867132845885gmail-s1"></span></p><p class="m_9138524867132845885gmail-p2"><span class="m_9138524867132845885gmail-s1"> </span><span class="m_9138524867132845885gmail-s2">func</span><span class="m_9138524867132845885gmail-s1"> rob(nums: [</span><span class="m_9138524867132845885gmail-s3">Int</span><span class="m_9138524867132845885gmail-s1">]) -> </span><span class="m_9138524867132845885gmail-s3">Int</span><span class="m_9138524867132845885gmail-s1"> {</span></p><p class="m_9138524867132845885gmail-p2"><span class="m_9138524867132845885gmail-s1"> </span><span class="m_9138524867132845885gmail-s2">guard</span><span class="m_9138524867132845885gmail-s1"> nums.count > </span><span class="m_9138524867132845885gmail-s4">0</span><span class="m_9138524867132845885gmail-s1"> </span><span class="m_9138524867132845885gmail-s2">else</span><span class="m_9138524867132845885gmail-s1"> { </span><span class="m_9138524867132845885gmail-s2">return</span><span class="m_9138524867132845885gmail-s1"> </span><span class="m_9138524867132845885gmail-s4">0</span><span class="m_9138524867132845885gmail-s1"> }</span></p><p class="m_9138524867132845885gmail-p2"><span class="m_9138524867132845885gmail-s1"> </span><span class="m_9138524867132845885gmail-s2">let</span><span class="m_9138524867132845885gmail-s1"> dp = Array.</span><span class="m_9138524867132845885gmail-s2">init</span><span class="m_9138524867132845885gmail-s1">(repeating: Array.</span><span class="m_9138524867132845885gmail-s2">init</span><span class="m_9138524867132845885gmail-s1">(repeating: </span><span class="m_9138524867132845885gmail-s4">0</span><span class="m_9138524867132845885gmail-s1">, count: nums.count),</span></p><p class="m_9138524867132845885gmail-p2"><span class="m_9138524867132845885gmail-s1"> count: </span><span class="m_9138524867132845885gmail-s4">2</span><span class="m_9138524867132845885gmail-s1">)</span></p><p class="m_9138524867132845885gmail-p2"><span class="m_9138524867132845885gmail-s1"> dp[</span><span class="m_9138524867132845885gmail-s4">0</span><span class="m_9138524867132845885gmail-s1">][</span><span class="m_9138524867132845885gmail-s4">0</span><span class="m_9138524867132845885gmail-s1">] = </span><span class="m_9138524867132845885gmail-s4">0</span></p><p class="m_9138524867132845885gmail-p2"><span class="m_9138524867132845885gmail-s1"> dp[</span><span class="m_9138524867132845885gmail-s4">0</span><span class="m_9138524867132845885gmail-s1">][</span><span class="m_9138524867132845885gmail-s4">1</span><span class="m_9138524867132845885gmail-s1">] = nums[</span><span class="m_9138524867132845885gmail-s4">0</span><span class="m_9138524867132845885gmail-s1">]</span></p><p class="m_9138524867132845885gmail-p2"><span class="m_9138524867132845885gmail-s1"> </span><span class="m_9138524867132845885gmail-s2">for</span><span class="m_9138524867132845885gmail-s1"> i </span><span class="m_9138524867132845885gmail-s2">in</span><span class="m_9138524867132845885gmail-s1"> </span><span class="m_9138524867132845885gmail-s4">1</span><span class="m_9138524867132845885gmail-s1"> ..< nums.count {</span></p><p class="m_9138524867132845885gmail-p2"><span class="m_9138524867132845885gmail-s1"> dp[i][</span><span class="m_9138524867132845885gmail-s4">0</span><span class="m_9138524867132845885gmail-s1">] = max(dp[i - </span><span class="m_9138524867132845885gmail-s4">1</span><span class="m_9138524867132845885gmail-s1">][</span><span class="m_9138524867132845885gmail-s4">0</span><span class="m_9138524867132845885gmail-s1">], dp[i - </span><span class="m_9138524867132845885gmail-s4">1</span><span class="m_9138524867132845885gmail-s1">][</span><span class="m_9138524867132845885gmail-s4">1</span><span class="m_9138524867132845885gmail-s1">])</span></p><p class="m_9138524867132845885gmail-p2"><span class="m_9138524867132845885gmail-s1"> dp[i][</span><span class="m_9138524867132845885gmail-s4">1</span><span class="m_9138524867132845885gmail-s1">] = dp[i - </span><span class="m_9138524867132845885gmail-s4">1</span><span class="m_9138524867132845885gmail-s1">][</span><span class="m_9138524867132845885gmail-s4">0</span><span class="m_9138524867132845885gmail-s1">] + nums[i]</span></p><p class="m_9138524867132845885gmail-p2"><span class="m_9138524867132845885gmail-s1"> }</span></p><p class="m_9138524867132845885gmail-p2"><span class="m_9138524867132845885gmail-s1"> </span><span class="m_9138524867132845885gmail-s2">return</span><span class="m_9138524867132845885gmail-s1"> </span><span class="m_9138524867132845885gmail-s4">0</span></p><p class="m_9138524867132845885gmail-p2"><span class="m_9138524867132845885gmail-s1"> }</span></p><p class="m_9138524867132845885gmail-p2"><span class="m_9138524867132845885gmail-s1">}</span></p></div><div class="gmail_default" style="font-family:simsun,serif;font-size:small"><br class=""></div><div class="gmail_default" style="font-family:simsun,serif;font-size:small"><br class=""></div><br clear="all" class=""><div class=""><div class="m_9138524867132845885gmail_signature"><i class=""><font face="georgia, serif" class="">best wishes for you </font></i></div></div>
</div>
<br class="">______________________________<wbr class="">_________________<br class="">
swift-users mailing list<br class="">
<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-users</a><br class="">
<br class=""></blockquote></div><br class=""></div>
_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></body></html>