<html><head><style id="css_styles" type="text/css"><!--blockquote.cite { margin-left: 5px; margin-right: 0px; padding-left: 10px; padding-right:0px; border-left: 1px solid #cccccc }
blockquote.cite2 {margin-left: 5px; margin-right: 0px; padding-left: 10px; padding-right:0px; border-left: 1px solid #cccccc; margin-top: 3px; padding-top: 0px; }
a img { border: 0px; }
table { border-collapse: collapse; }
li[style='text-align: center;'], li[style='text-align: center; '], li[style='text-align: right;'], li[style='text-align: right; '] { list-style-position: inside;}
body { font-family: 'Segoe UI'; font-size: 12pt; }
.quote { margin-left: 1em; margin-right: 1em; border-left: 5px #ebebeb solid; padding-left: 0.3em; }
--></style></head><body><div><div id="x5f828a370a7543edb988a37ed5109907"><div>Hi Mark,</div><div><br /></div><div>just for fun I tried to "make the code shorter":</div><div><br /></div><div>Collatz class >> gather: anInt seq: aSeq</div><div> "populates an OrderedCollection with the Collatz sequence for a given positive integer"</div><div><br /></div><div> anInt = 1 ifTrue: [^aSeq].</div><div> ^self gather: (aSeq addLast: (self next: anInt)) seq: aSeq</div><div><br /></div><div><br /></div><div>Collatz class >> sequence: anInteger</div><div> "answers an OrderedCollection containing the Collatz sequence for a given positive integer"</div><div><br /></div><div> ^self gather: anInteger seq: (OrderedCollection with: anInteger)</div><div><br /></div><div>The #next methods remains the same.</div><div><br /></div><div>You could even get rid of the second parameter of #gather:seq: method byt making the sequence a class instance variable.</div><div><br /></div><div>I'm aware readability may degrade this way so take it just as a fun exercise :)</div><div><br /></div><div>Re your simplified code under the Integer class I'd remove the return from ^seq at the end - it's redundant.</div><div><br /></div><div>regards,</div><div>Jaromir</div></div></div>
<div style="clear:both"><br /></div>
<div><br /></div>
<div>
<div>On 05-Jun-24 8:16:57 PM, "Mark Volkmann via Cuis-dev" <<a href="mailto:cuis-dev@lists.cuis.st">cuis-dev@lists.cuis.st</a>> wrote:</div></div><div><br /></div>
<div id="x1fc1b16815aa4f5"><blockquote cite="CAFfRWnWtEqSo1Y+CsW9s_4M2B8U0EdBU__8+rbtgKo1FdsjVDQ@mail.gmail.com" type="cite" class="cite2">
<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">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>
</blockquote></div>
</body></html>