<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">2014-11-27 9:21 GMT+01:00 Frank Lesser <span dir="ltr">&lt;<a href="mailto:frank-lesser@lesser-software.com" target="_blank">frank-lesser@lesser-software.com</a>&gt;</span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">hmm<br>
( Set withAll: #( 1 -1 )) collect: [  : e | e abs ] a Set(1)<br>
<br></blockquote><div><br></div><div>IMO, these snippets have no sense...<br></div><div>In somes cases, we will want to preserve uniqueness, in some cases we will want to preserve the size...<br></div><div>#collect:as: answers to most use cases, explicitely.<br><br></div><div>Answering an Array is a bit against the current trend which is to preserve as far as possible the collection properties when selecting/collecting.<br></div><div>For example, Dictionary collect: now answer a Dictionary, what it did not in the past.<br></div><div>In most cases (except maybe Interval) this is possible when selecting, but not necessarily when collecting  due to restrictions on element type (ByteArray WordArray String IdentitySet...), or due to Identity or non weak-references properties that have absolutely no guaranty to be preserved once transformed.<br>Note that #species was originally used for both selecting and collecting, but we started to distinguish these two, that might be the reason for the current non-uniformity of its usage.<br>Some collection change of species when collecting, some other just fail, obviously because we expect aString collect: #asUppercase to answer a String, not an Array...<br></div><div>In all these cases, collect:as: is a generic answer.<br></div><div><br></div><div>Anyway, a 1-to-1 mapping with something unordered is quite useless...<br>How are we going to map elements of the transformed collection with the original?<br>We cannot iterate like this: (aSet with: anOrderedCollection do: [:a :b | ]), nor with a Stream or any sequenceable protocol on an unordered collection.<br></div><div><br></div></div><div class="gmail_quote">The only guaranty you have is that the block will be evaluated once with each item (if there is no block return or exception raised...).<br>Even this expectation might be questionable, I once suggested to iterate only once per different value in a Bag, or once per run in a RunArray when collecting/selecting...<br></div><div class="gmail_quote">i did not, because too many code rely on the side effects like <br>   | i |<br>   i := 0. aSet collect: [:each | i := i + 1. each transformWithRank: i].<br></div><div class="gmail_quote"><div>IMO, this is bad style.<br><br></div><div>Nicolas<br></div><div> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
-----Ursprüngliche Nachricht-----<br>
Von: <a href="mailto:squeak-dev-bounces@lists.squeakfoundation.org">squeak-dev-bounces@lists.squeakfoundation.org</a><br>
[mailto:<a href="mailto:squeak-dev-bounces@lists.squeakfoundation.org">squeak-dev-bounces@lists.squeakfoundation.org</a>] Im Auftrag von Bert<br>
Freudenberg<br>
Gesendet: Donnerstag, 27. November 2014 08:57<br>
<span class="im">An: The general-purpose Squeak developers list<br>
Betreff: Re: [squeak-dev] IdentitySet&gt;&gt;collect:<br>
<br>
</span><div class=""><div class="h5">On 27.11.2014, at 02:23, Florin Mateoc &lt;<a href="mailto:florin.mateoc@gmail.com">florin.mateoc@gmail.com</a>&gt; wrote:<br>
&gt;<br>
&gt; On 11/26/2014 7:14 PM, Levente Uzonyi wrote:<br>
&gt;&gt; Your example hides the problem of ordering - what Tobias is asking about<br>
- so here&#39;s another:<br>
&gt;&gt;<br>
&gt;&gt; (IdentitySet withAll: #(1 1.0)) collect: [ :each | each class ]<br>
&gt;&gt;<br>
&gt;&gt; If IdentitySet &gt;&gt; #collect: were returning an Array, then what would be<br>
the answer?<br>
&gt;&gt;<br>
&gt;&gt; { SmallInteger. Float } or { Float. SmallInteger } ?<br>
&gt; snip<br>
&gt;&gt; Levente<br>
&gt;<br>
&gt;<br>
&gt; Then why does Dictionary&gt;&gt;keys return an Array? Where does the order come<br>
from in that example?<br>
&gt; Similarly, where does the order come from when you invoke collect:as: on a<br>
set with Array as an argument?<br>
&gt; The answer is quite simple: it is the iteration order. collect: is part of<br>
the _iteration_ protocol.<br>
&gt;<br>
&gt; I agree with Frank here, for me a more important aspect of collect: is<br>
preserving the _mapping_ between the original<br>
&gt; elements and the collected values.<br>
&gt; There is no obvious mapping if there are fewer collected values than<br>
elements.<br>
&gt;<br>
&gt; Florin<br>
<br>
I agree. The order is unimportant, it&#39;s a Set after all. What *is* important<br>
is getting a collection of the same size back - by default. For other needs<br>
we have collect:as:.<br>
<br>
- Bert -<br>
<br>
<br>
<br>
<br>
<br>
</div></div></blockquote></div><br></div></div>