<div dir="ltr">I took another shot at simplifying the code. I didn't need to use recursion. I can do this instead as an instance method added to the Integer class:<div><br></div><div><font face="monospace">collatz <br> "answers an OrderedCollection containing the Collatz sequence for this integer"<br><br> | next seq |<br> ^self < 1<br> ifTrue: nil<br></font><span style="font-family:monospace"> <span class="gmail-Apple-converted-space"> </span></span><font face="monospace">ifFalse: [<br></font><span style="font-family:monospace"> <span class="gmail-Apple-converted-space"> </span></span><font face="monospace">next := self.<br></font><span style="font-family:monospace"> <span class="gmail-Apple-converted-space"> </span></span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span style="font-family:monospace">seq := OrderedCollection new.</span></div><div><span style="font-family:monospace"> <span class="gmail-Apple-converted-space"> </span></span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span style="font-family:monospace">seq addLast: next.</span></div><div><span style="font-family:monospace"> <span class="gmail-Apple-converted-space"> </span></span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span style="font-family:monospace">[next = 1] whileFalse: [</span></div><div><span style="font-family:monospace"> <span class="gmail-Apple-converted-space"> </span></span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span style="font-family:monospace">next := next even ifTrue: [next / 2] ifFalse: [next * 3 + 1].</span></div><div><span style="font-family:monospace"> <span class="gmail-Apple-converted-space"> </span></span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span style="font-family:monospace">seq addLast: next</span></div><div><span style="font-family:monospace"> <span class="gmail-Apple-converted-space"> </span></span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span style="font-family:monospace">].</span></div><div><span style="font-family:monospace"> <span class="gmail-Apple-converted-space"> </span></span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span style="font-family:monospace">^seq</span></div><div><span style="font-family:monospace"> <span class="gmail-Apple-converted-space"> </span></span><font face="monospace">]</font></div><div><br></div><div>Beside the lack of memoization and the "orbit" concept, what would you change about this?</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jun 5, 2024 at 12:40 PM Mark Volkmann <<a href="mailto:r.mark.volkmann@gmail.com">r.mark.volkmann@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr">See my replies inline below with some parts snipped to focus my replies.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jun 5, 2024 at 12:19 PM Andres Valloud via Cuis-dev <<a href="mailto:cuis-dev@lists.cuis.st" target="_blank">cuis-dev@lists.cuis.st</a>> wrote:</div><div dir="ltr" class="gmail_attr"><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
Always be on the lookout for when a receiver is just self. Otherwise, <br>
the code is not using the knowledge it has of where it is implemented <br>
(and this leads to all sorts of problems later).<br></blockquote><div><br></div><div>Ah! I did not know that I could use `self` to refer to the class in a class method.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">Also, you can accomplish the same with much less code. Any time you <br>
have a class whose class methods offer a "service" but do not really <br>
have instances, you should suspect the code should be placed elsewhere.<br></blockquote><div><br></div><div>I see that I don't need the <font face="monospace">next:</font> method and can just move that code in the <font face="monospace">gather:seq:</font> method.</div><div>But it's not apparent to me what else I can do to make the code shorter.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">Side comment: I've been working on the Collatz problem for a *long* <br>
time, so I have quite a few opinions on how to go about writing <br>
specifically this code...<br></blockquote><div><br></div><div>I know I could add caching of results to make subsequent calls with different integers much faster.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">But in this particular case, what you're collecting is the orbit of an <br>
integer (so, anInteger, not aNumber).</blockquote><div><br></div><div>Excellent point! I changed every occurrence of `aNumber` to `anInteger` in my code.</div></div><div><br></div><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div><font face="arial, helvetica, sans-serif">R. Mark Volkmann</font></div><div><span style="font-size:12.8px"><font face="arial, helvetica, sans-serif">Object Computing, Inc.</font></span></div></div></div></div></div></div></div></div></div>
</blockquote></div><br clear="all"><div><br></div><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div><font face="arial, helvetica, sans-serif">R. Mark Volkmann</font></div><div><span style="font-size:12.8px"><font face="arial, helvetica, sans-serif">Object Computing, Inc.</font></span></div></div></div></div></div></div></div></div>