The Inbox: Collections-mt.942.mcz

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

The Inbox: Collections-mt.942.mcz

commits-2
A new version of Collections was added to project The Inbox:
http://source.squeak.org/inbox/Collections-mt.942.mcz

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

Name: Collections-mt.942
Author: mt
Time: 15 April 2021, 10:10:36.948715 am
UUID: 663ded23-ec3c-44bf-9e67-8fe2384b94cc
Ancestors: Collections-ul.940, Collections-mt.938

Updates the proposal with "as:" to let clients configure the collection class if #species does not fit. Thanks Levente (ul) for the tip!

(Also merges from Collections-ul.940, which is current in Trunk.)

=============== Diff against Collections-ul.940 ===============

Item was added:
+ ----- Method: Collection>>collect:as:separatedBy: (in category 'enumerating') -----
+ collect: elementBlock as: aClass separatedBy: separatorBlock
+ "Evaluate the elementBlock for all elements in the receiver, and evaluate the separatorBlock between. Collect the resulting values of both blocks into a collection like the receiver. Answer the new collection."
+
+ | newCollection |
+ newCollection := aClass new.
+ self
+ do: [:each | newCollection add: (elementBlock value: each)]
+ separatedBy: [:each | newCollection add: separatorBlock value].
+ ^ newCollection!

Item was added:
+ ----- Method: Collection>>collect:separatedBy: (in category 'enumerating') -----
+ collect: elementBlock separatedBy: separatorBlock
+
+ ^ self
+ collect: elementBlock
+ as: self species
+ separatedBy: separatorBlock!

Item was added:
+ ----- Method: SequenceableCollection>>collect:as:separatedBy: (in category 'enumerating') -----
+ collect: elementBlock as: aClass separatedBy: separatorBlock
+ "Evaluate the elementBlock for all elements in the receiver, and evaluate the separatorBlock between. Collect the resulting values of both blocks into a collection like the receiver. Answer the new collection."
+
+ | newCollection |
+ newCollection := aClass ofSize: self size *2 -1.
+ 1 to: self size do: [:index |
+ index = 1 ifFalse: [newCollection at: (index-1 *2) put: separatorBlock value].
+ newCollection at: (index-1 *2)+1 put: (elementBlock value: (self at: index))].
+ ^ newCollection!