+1
I don't know if there is a practical use case for new:filledWith: but I think that the examples you give are instructive.
Suggestion - for the method comments, consider this:
"Evaluate aBlock with integers from 1 to: size as the block argument. Collect the resulting values into a new collection."
Dave
On Mon, Nov 18, 2019 at 03:00:34PM +0100, Marcel Taeumel wrote:
Hi, all.
Any thoughts on this matter? Here are more examples:
OrderedCollection new: 20 filledWith: [100 atRandom]. Set new: 20 filledWith: [:ea | (ea + 32) asCharacter]. Dictionary new: 20 filledWith: [:ea | ea -> (ea + 32) asCharacter].
Further reading (for related naming and concepts):
(all implementors of ...) #collect:as: #fillFrom:with: #newFrom: #new:withAll:
Best, Marcel Am 06.09.2019 16:57:53 schrieb commits@source.squeak.org commits@source.squeak.org: 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!