The Inbox: Collections-mt.832.mcz

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

The Inbox: Collections-mt.832.mcz

commits-2
A new version of Collections was added to project The Inbox:
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.!

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!

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!


Reply | Threaded
Open this post in threaded view
|

Re: The Inbox: Collections-mt.832.mcz

Nicolas Cellier
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, <[hidden email]> a écrit :
A new version of Collections was added to project The Inbox:
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!




Reply | Threaded
Open this post in threaded view
|

Re: The Inbox: Collections-mt.832.mcz

marcel.taeumel
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 <[hidden email]>:

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, <[hidden email]> a écrit :
A new version of Collections was added to project The Inbox:
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!




Reply | Threaded
Open this post in threaded view
|

Re: The Inbox: Collections-mt.832.mcz

Nicolas Cellier


Le ven. 10 mai 2019 à 08:04, Marcel Taeumel <[hidden email]> a écrit :
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.


| count |
count := 0.
(1 to: 11) combinations: 4 atATimeDo:
    [:p |
    (p asSet size = p size)
        ifFalse: [self halt: 'duplicate'].
    count := count + 1].
^count

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


I don't think so.

Best,
Marcel

Am 09.05.2019 16:08:47 schrieb Nicolas Cellier <[hidden email]>:

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, <[hidden email]> a écrit :
A new version of Collections was added to project The Inbox:
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!





Reply | Threaded
Open this post in threaded view
|

Re: The Inbox: Collections-mt.832.mcz

K K Subbu
In reply to this post by marcel.taeumel
On 10/05/19 11:34 AM, Marcel Taeumel wrote:
> Hmmm...
>
> (Set withAll: (1 to: 11)) take: 4 "-> e.g. #(1 2 3 4)"
> (Set withAll: (1 to: 11)) size take: 4 "-> 330"

May I suggest using #slice: or #copyFirst: for collections?

   slice: 10
   copyFirst: 10


Regards .. Subbu

Reply | Threaded
Open this post in threaded view
|

Re: The Inbox: Collections-mt.832.mcz

marcel.taeumel
I think that "first" in the name would not work for an arbitrary collection, which might not have any order at all. "copy" sounds too low-level.

I do like #slice:. The name #take: is inspired from libraries for other languages and database frameworks. Yet, #slice: might also be better for randomized sampling (if desired) than #take:.

Then, we have #anyOne. So, what about #some:? ... or #anySome:? :-D

Best,
Marcel

Am 10.05.2019 09:22:12 schrieb K K Subbu <[hidden email]>:

On 10/05/19 11:34 AM, Marcel Taeumel wrote:
> Hmmm...
>
> (Set withAll: (1 to: 11)) take: 4 "-> e.g. #(1 2 3 4)"
> (Set withAll: (1 to: 11)) size take: 4 "-> 330"

May I suggest using #slice: or #copyFirst: for collections?

slice: 10
copyFirst: 10


Regards .. Subbu


Reply | Threaded
Open this post in threaded view
|

Re: The Inbox: Collections-mt.832.mcz

Nicolas Cellier


Le ven. 10 mai 2019 à 10:25, Marcel Taeumel <[hidden email]> a écrit :
I think that "first" in the name would not work for an arbitrary collection, which might not have any order at all. "copy" sounds too low-level.

Agree, we already have #first: for the Sequenceable kind...

I do like #slice:. The name #take: is inspired from libraries for other languages and database frameworks. Yet, #slice: might also be better for randomized sampling (if desired) than #take:.

Then, we have #anyOne. So, what about #some:? ... or #anySome:? :-D

Yes, that's what I thought when you posted; just #any:
But #take: is a good name

Best,
Marcel

Am 10.05.2019 09:22:12 schrieb K K Subbu <[hidden email]>:

On 10/05/19 11:34 AM, Marcel Taeumel wrote:
> Hmmm...
>
> (Set withAll: (1 to: 11)) take: 4 "-> e.g. #(1 2 3 4)"
> (Set withAll: (1 to: 11)) size take: 4 "-> 330"

May I suggest using #slice: or #copyFirst: for collections?

slice: 10
copyFirst: 10


Regards .. Subbu



Reply | Threaded
Open this post in threaded view
|

Re: The Inbox: Collections-mt.832.mcz

marcel.taeumel
So, there are two questions:

1) Name it #take: or #any: or #slice:?
2) Signal an Error (or Notification) if there are not enough elements in the collection or just compensate (less elements or duplicate elements)?

Best,
Marcel

Am 10.05.2019 11:45:59 schrieb Nicolas Cellier <[hidden email]>:



Le ven. 10 mai 2019 à 10:25, Marcel Taeumel <[hidden email]> a écrit :
I think that "first" in the name would not work for an arbitrary collection, which might not have any order at all. "copy" sounds too low-level.

Agree, we already have #first: for the Sequenceable kind...

I do like #slice:. The name #take: is inspired from libraries for other languages and database frameworks. Yet, #slice: might also be better for randomized sampling (if desired) than #take:.

Then, we have #anyOne. So, what about #some:? ... or #anySome:? :-D

Yes, that's what I thought when you posted; just #any:
But #take: is a good name

Best,
Marcel

Am 10.05.2019 09:22:12 schrieb K K Subbu <[hidden email]>:

On 10/05/19 11:34 AM, Marcel Taeumel wrote:
> Hmmm...
>
> (Set withAll: (1 to: 11)) take: 4 "-> e.g. #(1 2 3 4)"
> (Set withAll: (1 to: 11)) size take: 4 "-> 330"

May I suggest using #slice: or #copyFirst: for collections?

slice: 10
copyFirst: 10


Regards .. Subbu



cbc
Reply | Threaded
Open this post in threaded view
|

Re: The Inbox: Collections-mt.832.mcz

cbc


On Fri, May 10, 2019 at 5:53 AM Marcel Taeumel <[hidden email]> wrote:
So, there are two questions:

1) Name it #take: or #any: or #slice:?
2) Signal an Error (or Notification) if there are not enough elements in the collection or just compensate (less elements or duplicate elements)?

I think the point of the original commit was to NOT signal an Error if there weren't enough elements - otherwise #first: would have worked (mostly).  The idea is "I want 4, or whatever you have if you don't have that many".
-cbc

Best,
Marcel


Reply | Threaded
Open this post in threaded view
|

Re: The Inbox: Collections-mt.832.mcz

Tobias Pape

> On 10.05.2019, at 17:46, Chris Cunningham <[hidden email]> wrote:
>
>
>
> On Fri, May 10, 2019 at 5:53 AM Marcel Taeumel <[hidden email]> wrote:
> So, there are two questions:
>
> 1) Name it #take: or #any: or #slice:?
> 2) Signal an Error (or Notification) if there are not enough elements in the collection or just compensate (less elements or duplicate elements)?
>
> I think the point of the original commit was to NOT signal an Error if there weren't enough elements - otherwise #first: would have worked (mostly).  The idea is "I want 4, or whatever you have if you don't have that many".

but #first does not work for Sets, Dictionaries and the like.

I'm in for an Error-throwing #any:.

Reason:
what we have as #first on Sequenceable is more generally available on Collection as #anyOne .
what we have as #first: on Sequenceable could be more generally available on Collection as #any:.

Best regards
        -Tobias