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

Marcel Taeumel marcel.taeumel at hpi.de
Fri May 10 06:04:08 UTC 2019


Hmmm...

(Set withAll: (1 to: 11)) take: 4 "-> e.g. #(1 2 3 4)"
(Set withAll: (1 to: 11)) size take: 4 "-> 330"

Well, the difference is that Integer >> #take: considers duplicate elements,too.

Anyway, could such false polymorphism be a problem in a way that it would be hard to debug? 

Best,
Marcel
Am 09.05.2019 16:08:47 schrieb Nicolas Cellier <nicolas.cellier.aka.nice at gmail.com>:
I note false polymorphism with Integer>>take:
of course both are related, since Integer>>take: (11 take: 4) tells how many different ways you can #take: 4 elements from a Set of size 11...


Le jeu. 9 mai 2019 à 15:27, <commits at source.squeak.org [mailto:commits at source.squeak.org]> a écrit :

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

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

Name: Collections-mt.832
Author: mt
Time: 9 May 2019, 3:27:06.069782 pm
UUID: b3c087f4-c2bc-d542-91d7-9c214ae3a97f
Ancestors: Collections-nice.831

Adds #take: to create a sub-collection from any collection by specifying the number of elements. Works like #first: for sequenceable collections but does not fail if collection is too small.

=============== Diff against Collections-nice.831 ===============

Item was added:
+ ----- Method: CharacterSet>>take: (in category 'accessing') -----
+ take: n
+
+       self shouldNotImplement.!

Why? a simple do: loop seems feasible.


Item was added:
+ ----- Method: Collection>>take: (in category 'accessing') -----
+ take: n
+       "Enumerate this collection and return the first n elements or less."
+
+       | index result |
+       index := 1.     
+       result := self species new: (n min: self size).
+       self associationsDo: [:each |
+               result add: each.
+               (index := index + 1) > n ifTrue: [^ result]].

+       ^ result!

First reaction: I did not understand associationsDo:... Why not just do:?
After verification: I see that default Behavior of associationsDo: is to do: and this avoids a redefinition of take: in Dictionary...
I don't find it unsurprising, but if it works...



Item was added:
+ ----- Method: SequenceableCollection>>take: (in category 'accessing') -----
+ take: n
+
+       ^ self first: (n min: self size)!

Item was added:
+ ----- Method: Stream>>take: (in category 'accessing') -----
+ take: n
+
+       ^ self next: n!


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


More information about the Squeak-dev mailing list