<br><br><div class="gmail_quote">On Tue, Nov 2, 2010 at 11:50 AM, Eliot Miranda <span dir="ltr">&lt;<a href="mailto:eliot.miranda@gmail.com">eliot.miranda@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br><br><div class="gmail_quote"><div class="im">On Tue, Nov 2, 2010 at 11:31 AM, Levente Uzonyi <span dir="ltr">&lt;<a href="mailto:leves@elte.hu" target="_blank">leves@elte.hu</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div>On Tue, 2 Nov 2010, Eliot Miranda wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi Levente,<br>
<br>
On Tue, Nov 2, 2010 at 10:16 AM, Levente Uzonyi &lt;<a href="mailto:leves@elte.hu" target="_blank">leves@elte.hu</a>&gt; wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi,<br>
<br>
we have these two methods which do exactly the same thing. #reduce: was<br>
added by Andreas during the developement of Squeak 4.1. #fold was added by<br>
Eliot for Cog VMMaker compatibility. One of them is superfluous. I can image<br>
the following solutions:<br>
<br>
1) Replace senders of #fold: in VMMaker to use #reduce:, and remove #fold:<br>
from the image.<br>
<br>
2) Replace #fold:&#39;s implementation to self reduce: aBlock. I benchmarked<br>
the two methods and #reduce: is a bit faster on CogVM. There&#39;s no difference<br>
on SqueakVM.<br>
<br>
</blockquote>
<br>
I noticed the same thing.  I prefer the fold: implementation so I&#39;m bummed<br>
it&#39;s slightly slower ;)  Personally I like fold: as a name (it&#39;s shorter and<br>
</blockquote>
<br></div>
The difference is about 1%, but it&#39;s reproducible. And yes, the implementation of #fold: is a bit nicer. :)</blockquote><div><br></div></div><div>The difference is due to the instantiation &quot;Object new&quot;, and instantiation is slow on the current Cog because I haven&#39;t implemented the instantiation primitives in machine code, and this limitation is hopefully temporary.  So you might leave it be.  However, changing the code to use {} for teh instantiation speeds things up markedly:</div>

<div><br></div><div>Collection&gt;&gt;newFold: binaryBlock</div><div><span style="white-space:pre-wrap">        </span>| firstValue nextValue |</div><div><span style="white-space:pre-wrap">        </span>firstValue := nextValue := {}.</div>

<div><span style="white-space:pre-wrap">        </span>self do:</div><div><span style="white-space:pre-wrap">                </span>[:each |</div><div><span style="white-space:pre-wrap">                </span>nextValue := firstValue == nextValue</div>
<div><span style="white-space:pre-wrap">                                                </span>ifTrue: [each]</div><div><span style="white-space:pre-wrap">                                                </span>ifFalse: [binaryBlock value: nextValue value: each]].</div>
<div><span style="white-space:pre-wrap">        </span>^nextValue == firstValue</div><div><span style="white-space:pre-wrap">                </span>ifTrue: [self errorEmptyCollection]</div><div><span style="white-space:pre-wrap">                </span>ifFalse: [nextValue]</div>

<div><br></div><div><div>| c r |</div><div>c := #(&#39;if&#39; &#39;it&#39; &#39;is&#39; &#39;to&#39; &#39;be&#39; &#39;it&#39; &#39;is&#39; &#39;up&#39; &#39;to&#39; &#39;me&#39;).</div><div>{ Time millisecondsToRun: [1 to: 1000000 do: [:i| c fold: [:a :b | a, &#39; &#39;, b]]].</div>

<div><span style="white-space:pre-wrap">        </span>Time millisecondsToRun: [1 to: 1000000 do: [:i| c newFold: [:a :b | a, &#39; &#39;, b]]].</div><div><span style="white-space:pre-wrap">                </span>Time millisecondsToRun: [1 to: 1000000 do: [:i| c reduce: [:a :b | a, &#39; &#39;, b]]] }</div>

<div><span style="white-space:pre-wrap">                </span></div><div><span style="white-space:pre-wrap">                </span></div><div>#(5076 5008 5052)</div><div><br></div><div><br></div><div>| c |</div><div>
c := #(&#39;if&#39;).</div><div>{ Time millisecondsToRun: [1 to: 1000000 do: [:i| c fold: [:a :b | a, &#39; &#39;, b]]].</div><div>   Time millisecondsToRun: [1 to: 1000000 do: [:i| c newFold: [:a :b | a, &#39; &#39;, b]]].</div>

<div>   Time millisecondsToRun: [1 to: 1000000 do: [:i| c reduce: [:a :b | a, &#39; &#39;, b]]] }</div><div> #(247 226 220)</div></div><div><br></div><div><br></div><div>(N.B.  the above are rough measurements!!).</div><div>

<br></div><div>So how about changing fold: to replace Object new with {} and keep it?</div></div></blockquote><div><br></div><div>And of course one can consider using thisContext instead of Object new or {}, since thisContext has to be instantiated anyway for the closure in fold:, and so results in less memory pressure.  This needs careful measurement; you have the test bed set up for that; my workspace hacks are showing too much variability to distinguish.</div>
<div><br></div><div>This is too ugly to contemplate, but at first blush it seems faster:</div><div><br></div><div><div>fold: binaryBlock</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>&quot;Evaluate the block with the first two elements of the receiver,</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span> then with the result of the first evaluation and the next element,</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> and so on.  Answer the result of the final evaluation. If the receiver</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span> is empty, raise an error. If the receiver has a single element, answer</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> that element.&quot;</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>&quot;#(&#39;if&#39; &#39;it&#39; &#39;is&#39; &#39;to&#39; &#39;be&#39; &#39;it&#39; &#39;is&#39; &#39;up&#39; &#39;to&#39; &#39;me&#39;) fold: [:a :b | a, &#39; &#39;, b]&quot;</div>
<div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>| firstValue nextValue |</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>firstValue := nextValue := &quot;something that can&#39;t be in the receiver&quot;</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>[:each |</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>nextValue := firstValue == nextValue</div><div><span class="Apple-tab-span" style="white-space:pre">                                                </span>ifTrue: [each]</div>
<div><span class="Apple-tab-span" style="white-space:pre">                                                </span>ifFalse: [binaryBlock value: nextValue value: each]].</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>self do: firstValue.</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>^nextValue == firstValue</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>ifTrue: [self errorEmptyCollection]</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>ifFalse: [nextValue]</div>
</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="gmail_quote"><div><br></div><div><br></div><div>2¢</div><div>Eliot</div><div class="im">
<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
more cuddly) and since we don&#39;t have map: (we have collect:) I don&#39;t find<br>
the need to use reduce: compelling.  But that&#39;s my preference.  I won&#39;t<br>
object if you replace fold: with reduce:/  I do note that Gilad used fold:<br>
in Newspeak.<br>
<br>
What do you prefer?<br>
</blockquote>
<br></div>
I prefer the second option, because both names are widely used, but some people are only aware of the one which their previously used languages have. So this way we can avoid questions like &quot;Why isn&#39;t there a<br>


method for folding in Squeak?&quot;.<br><font color="#888888">
<br>
<br>
Levente</font><div><div></div><div><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
What do others prefer?<br>
<br>
I know, choices, choices :)<br>
<br>
best<br>
Eliot<br>
<br>
<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<br>
Cheers,<br>
Levente<br>
<br>
<br>
</blockquote>
<br>
</blockquote>
<br>
</div></div></blockquote></div></div><br>
</blockquote></div><br>