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

Chris Muller asqueaker at gmail.com
Wed Nov 20 04:58:59 UTC 2019


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.  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> wrote:

> A new version of Collections was added to project The Inbox:
> 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/20191119/b6d2731d/attachment.html>


More information about the Squeak-dev mailing list