[squeak-dev] The Inbox: Collections-mt.852.mcz

Marcel Taeumel marcel.taeumel at hpi.de
Wed Nov 20 08:13:18 UTC 2019


Thanks! Still, I have to compute the numbers:

(Random new next: 100) collect: [:ea | (ea * 100) truncated]


Best,
Marcel
Am 20.11.2019 08:58:13 schrieb Nicolas Cellier <nicolas.cellier.aka.nice at gmail.com>:
Random new next: 100


Le mer. 20 nov. 2019 à 08:49, Marcel Taeumel <marcel.taeumel at hpi.de [mailto:marcel.taeumel at hpi.de]> a écrit :

Hi, all.

What's the "most idiomatic" way to create an array with 100 random numbers > 0 < 100?

(1 to: 100) collect: [:ea | 100 atRandom]
Array streamContents: [:s | 100 timesRepeat: [s nextPut: 100 atRandom]].
...

Best,
Marcel
Am 20.11.2019 07:08:34 schrieb Chris Muller <asqueaker at gmail.com [mailto:asqueaker at gmail.com]>:
On Tue, Nov 19, 2019 at 10:58 PM Chris Muller <asqueaker at gmail.com [mailto:asqueaker at gmail.com]> wrote:

They share the same selector API, but for ArrayedCollection's, it culls integers 1 to: n, while for non-Arrayed, it culls nil over and over.

Woops, I got that wrong, sorry.  It is the index in both cases, not nil. That's better but, still, I'm sure I would just write collect:as: without remembering this.

 - Chris

 
  This means the sender will have to know whether it's Arrayed or not anyway (otherwise you'd need a nil check inside the loop).  And so, if you have to know that, one could just write classic Smalltalk.


   (1 to: 20) collect: [ :i | ... ]

(with or without an "as:" converter on the end).

OTOH, if you were expecting a non-arrayed collection, then simply:


    Array streamContents: [ : stream | ... ] 

I do appreciate the readability for people less experienced with Smalltalk, but for the long run, classic, readable, portable Smalltalk may actually be the better way for them to read it.


Collecting nil over and over could be a sign of something not fully optimized here.

-1 on this one, even though I'm really enjoying many of your other contributions, Christoph.


Best,
  Chris

On Fri, Sep 6, 2019 at 9:57 AM <commits at source.squeak.org [mailto:commits at source.squeak.org]> wrote:

A new version of Collections was added to project The Inbox:
http://source.squeak.org/inbox/Collections-mt.852.mcz [http://source.squeak.org/inbox/Collections-mt.852.mcz]

==================== Summary ====================

Name: Collections-mt.852
Author: mt
Time: 6 September 2019, 4:57:45.245608 pm
UUID: 3582be1c-a006-ed46-a517-3d2d570db6cc
Ancestors: Collections-mt.851

Proposal: Initialize a new collection with a computation block without needing an existing collection to #collect: from.

OrderedCollection new: 20 filledWith: [100 atRandom].

Thanks to Christoph (ct) for the idea.

If we want this in Trunk, there will be tests. :-)

=============== Diff against Collections-mt.851 ===============

Item was added:
+ ----- Method: ArrayedCollection class>>new:filledWith: (in category 'instance creation') -----
+ new: size filledWith: aBlock
+       "Similar to #collect:as: and #fillFrom:with: but uses only the interval (1 to: size) to fill the collection. Different compared to #new:withAll: because aBlock can return different values for each index."
+
+       | result |
+       result := self new: size.
+       1 to: size do: [:each | result at: each put: (aBlock cull: each)].
+       ^ result!

Item was added:
+ ----- Method: Collection class>>new:filledWith: (in category 'instance creation') -----
+ new: size filledWith: aBlock
+       "Similar to #collect:as: and #fillFrom:with: but uses only the interval (1 to: size) to fill the collection. Different compared to #new:withAll: because aBlock can return different values for each index."
+
+       | result |
+       result := self new: size.
+       1 to: size do: [:each | result add: (aBlock cull: each)].
+       ^ result!



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20191120/10964b5a/attachment.html>


More information about the Squeak-dev mailing list