Hi
I have #(#Object #subclass: #Point #instanceVariableNames: 'x y' #classVariableNames: '' #package: 'Kernel-BasicObjects') and I would like to select on the second elements. I was thinking that #(#Object #subclass: #Point #instanceVariableNames: 'x y' #classVariableNames: '' #package: 'Kernel-BasicObjects') selectEvery: [ :i | i \\ 2 = 0 ] could be a nice iterator. What do you think? Stef |
2017-04-19 15:43 GMT-03:00 Stephane Ducasse <[hidden email]>:
> Hi > > I have > > #(#Object #subclass: #Point #instanceVariableNames: 'x y' > #classVariableNames: '' #package: 'Kernel-BasicObjects') and I would like to > select on the second elements. > > I was thinking that > > #(#Object #subclass: #Point #instanceVariableNames: 'x y' > #classVariableNames: '' #package: 'Kernel-BasicObjects') selectEvery: [ :i | > i \\ 2 = 0 ] > could be a nice iterator. > > What do you think? I think I wouldn't add a new enumerating selector to the core classes. If it needs to be there, I would replace it by #keysSelect: So it would work to select based on the keys of the receiver (indexes for sequenced collections) but also would work as it does for #keysAndValuesDo: implementors. Regards, -- Esteban. |
why? Iterators are powerful and avoid that we all reinvent the wheel in our own corners. About keySelect: I do not see the point to convert a large collection into a dictionary then do yet another pass. To me it looks like a hack. I implemented selectEvery: (selectFirst selectSecond) as helpers. and also unzip all in one pass. Now I have no problem to keep them for me but to me this is the wrong attitude. Stef testSelectEveryFirst self assert: (#(#Object #subclass: #Point #instanceVariableNames: 'x y' #classVariableNames: '' #package: 'Kernel-BasicObjects') selectEveryFirst) asArray equals: #(#Object #Point 'x y' '' 'Kernel-BasicObjects') testSelectEverySecond
self assert: (#(#Object #subclass: #Point #instanceVariableNames: 'x y' #classVariableNames: '' #package: 'Kernel-BasicObjects') selectEverySecond) asArray equals: #(#subclass: #instanceVariableNames: #classVariableNames: #package:)
testUnzip | uz | uz := #(#Object #subclass: #Point #instanceVariableNames: 'x y' #classVariableNames: '' #package: 'Kernel-BasicObjects') unzip. self assert: uz first asArray equals: #(#Object #Point 'x y' '' 'Kernel-BasicObjects'). self assert: uz second asArray equals: #(#subclass: #instanceVariableNames: #classVariableNames: #package:) On Wed, Apr 19, 2017 at 9:05 PM, Esteban A. Maringolo <[hidden email]> wrote: 2017-04-19 15:43 GMT-03:00 Stephane Ducasse <[hidden email]>: |
2017-04-19 16:17 GMT-03:00 Stephane Ducasse <[hidden email]>:
> why? > Iterators are powerful and avoid that we all reinvent the wheel in our own > corners. > > About keySelect: I do not see the point to convert a large collection into a > dictionary then do yet another pass. > To me it looks like a hack. keySelect: would do a select based on the key (index in the case of SequenceableCollection) of the element, no need to create a Dictionary. keySelect: aBlock | result | result := self species new. self keysAndValuesDo: [:key :value | (aBlock value: key) ifTrue: [result add: value] ]. ^result You could implement #selectEverySecond or #selectEveryFirst in terms of the above. The name sounds weird though, but I'm not a native English speaker. Regarding #unzip it's a different story, I wouldn't use 'zip' in a selector for non Zip (compression) related methods. But do as you please, Pharo is yours as well ;) Regards! > I implemented > selectEvery: > (selectFirst selectSecond) as helpers. > > and also unzip all in one pass. > Now I have no problem to keep them for me but to me this is the wrong > attitude. > > Stef > > > testSelectEveryFirst > self assert: (#(#Object #subclass: #Point #instanceVariableNames: 'x y' > #classVariableNames: '' #package: 'Kernel-BasicObjects') selectEveryFirst) > asArray equals: #(#Object #Point 'x y' '' 'Kernel-BasicObjects') > testSelectEverySecond self assert: (#(#Object #subclass: #Point > #instanceVariableNames: 'x y' #classVariableNames: '' #package: > 'Kernel-BasicObjects') selectEverySecond) asArray equals: #(#subclass: > #instanceVariableNames: #classVariableNames: #package:) > testUnzip > | uz | > uz := #(#Object #subclass: #Point #instanceVariableNames: 'x y' > #classVariableNames: '' #package: 'Kernel-BasicObjects') unzip. > self assert: uz first asArray equals: #(#Object #Point 'x y' '' > 'Kernel-BasicObjects'). > self assert: uz second asArray equals: #(#subclass: #instanceVariableNames: > #classVariableNames: #package:) Esteban A. Maringolo |
In reply to this post by Stephane Ducasse-3
I like this one.
Doru > On Apr 19, 2017, at 8:43 PM, Stephane Ducasse <[hidden email]> wrote: > > Hi > > I have > > #(#Object #subclass: #Point #instanceVariableNames: 'x y' #classVariableNames: '' #package: 'Kernel-BasicObjects') and I would like to select on the second elements. > > I was thinking that > > #(#Object #subclass: #Point #instanceVariableNames: 'x y' #classVariableNames: '' #package: 'Kernel-BasicObjects') selectEvery: [ :i | i \\ 2 = 0 ] > could be a nice iterator. > > What do you think? > > > Stef > -- www.tudorgirba.com www.feenk.com "Innovation comes in the least expected form. That is, if it is expected, it already happened." |
There are already #pairsDo: and #pairsCollect: that could be used:
#(a b c d e f g h) pairsCollect: [ :x :y | y ] => #(#b #d #f #h) > On 19 Apr 2017, at 22:12, Tudor Girba <[hidden email]> wrote: > > I like this one. > > Doru > > >> On Apr 19, 2017, at 8:43 PM, Stephane Ducasse <[hidden email]> wrote: >> >> Hi >> >> I have >> >> #(#Object #subclass: #Point #instanceVariableNames: 'x y' #classVariableNames: '' #package: 'Kernel-BasicObjects') and I would like to select on the second elements. >> >> I was thinking that >> >> #(#Object #subclass: #Point #instanceVariableNames: 'x y' #classVariableNames: '' #package: 'Kernel-BasicObjects') selectEvery: [ :i | i \\ 2 = 0 ] >> could be a nice iterator. >> >> What do you think? >> >> >> Stef >> > > -- > www.tudorgirba.com > www.feenk.com > > "Innovation comes in the least expected form. > That is, if it is expected, it already happened." > > |
In reply to this post by Esteban A. Maringolo
We already have
* #withIndexCollect: * #withIndexDo: So why not #withIndexSelect: ? #(#Object #subclass: #Point #instanceVariableNames:) withIndexSelect: [ :each :i | i \\ 2 = 0 ] Peter p.s. for every second you could also do aCollection pairsCollect: [ :first :second | second ] On Wed, Apr 19, 2017 at 04:35:07PM -0300, Esteban A. Maringolo wrote: > 2017-04-19 16:17 GMT-03:00 Stephane Ducasse <[hidden email]>: > > why? > > Iterators are powerful and avoid that we all reinvent the wheel in our own > > corners. > > > > About keySelect: I do not see the point to convert a large collection into a > > dictionary then do yet another pass. > > To me it looks like a hack. > > keySelect: would do a select based on the key (index in the case of > SequenceableCollection) of the element, no need to create a > Dictionary. > > keySelect: aBlock > | result | > result := self species new. > self keysAndValuesDo: [:key :value | > (aBlock value: key) ifTrue: [result add: value] > ]. > ^result > > > You could implement #selectEverySecond or #selectEveryFirst in terms > of the above. The name sounds weird though, but I'm not a native > English speaker. > > Regarding #unzip it's a different story, I wouldn't use 'zip' in a > selector for non Zip (compression) related methods. > > But do as you please, Pharo is yours as well ;) > > Regards! > > > > I implemented > > selectEvery: > > (selectFirst selectSecond) as helpers. > > > > and also unzip all in one pass. > > Now I have no problem to keep them for me but to me this is the wrong > > attitude. > > > > Stef > > > > > > testSelectEveryFirst > > self assert: (#(#Object #subclass: #Point #instanceVariableNames: 'x y' > > #classVariableNames: '' #package: 'Kernel-BasicObjects') selectEveryFirst) > > asArray equals: #(#Object #Point 'x y' '' 'Kernel-BasicObjects') > > testSelectEverySecond self assert: (#(#Object #subclass: #Point > > #instanceVariableNames: 'x y' #classVariableNames: '' #package: > > 'Kernel-BasicObjects') selectEverySecond) asArray equals: #(#subclass: > > #instanceVariableNames: #classVariableNames: #package:) > > testUnzip > > | uz | > > uz := #(#Object #subclass: #Point #instanceVariableNames: 'x y' > > #classVariableNames: '' #package: 'Kernel-BasicObjects') unzip. > > self assert: uz first asArray equals: #(#Object #Point 'x y' '' > > 'Kernel-BasicObjects'). > > self assert: uz second asArray equals: #(#subclass: #instanceVariableNames: > > #classVariableNames: #package:) > > > Esteban A. Maringolo > |
I second the Peter’s idea. I don’t think that we need a dedicated index-based select when we already have withIndex*. Also one year after adding withIndexSelect: we can mine public repos and see how many projects use the method and the selection block does not read the first (each) parameter.
Uko > On 20 Apr 2017, at 00:07, Peter Uhnak <[hidden email]> wrote: > > We already have > > * #withIndexCollect: > * #withIndexDo: > > > So why not #withIndexSelect: ? > > #(#Object #subclass: #Point #instanceVariableNames:) withIndexSelect: [ :each :i | i \\ 2 = 0 ] > > Peter > > p.s. for every second you could also do > aCollection pairsCollect: [ :first :second | second ] > > > On Wed, Apr 19, 2017 at 04:35:07PM -0300, Esteban A. Maringolo wrote: >> 2017-04-19 16:17 GMT-03:00 Stephane Ducasse <[hidden email]>: >>> why? >>> Iterators are powerful and avoid that we all reinvent the wheel in our own >>> corners. >>> >>> About keySelect: I do not see the point to convert a large collection into a >>> dictionary then do yet another pass. >>> To me it looks like a hack. >> >> keySelect: would do a select based on the key (index in the case of >> SequenceableCollection) of the element, no need to create a >> Dictionary. >> >> keySelect: aBlock >> | result | >> result := self species new. >> self keysAndValuesDo: [:key :value | >> (aBlock value: key) ifTrue: [result add: value] >> ]. >> ^result >> >> >> You could implement #selectEverySecond or #selectEveryFirst in terms >> of the above. The name sounds weird though, but I'm not a native >> English speaker. >> >> Regarding #unzip it's a different story, I wouldn't use 'zip' in a >> selector for non Zip (compression) related methods. >> >> But do as you please, Pharo is yours as well ;) >> >> Regards! >> >> >>> I implemented >>> selectEvery: >>> (selectFirst selectSecond) as helpers. >>> >>> and also unzip all in one pass. >>> Now I have no problem to keep them for me but to me this is the wrong >>> attitude. >>> >>> Stef >>> >>> >>> testSelectEveryFirst >>> self assert: (#(#Object #subclass: #Point #instanceVariableNames: 'x y' >>> #classVariableNames: '' #package: 'Kernel-BasicObjects') selectEveryFirst) >>> asArray equals: #(#Object #Point 'x y' '' 'Kernel-BasicObjects') >>> testSelectEverySecond self assert: (#(#Object #subclass: #Point >>> #instanceVariableNames: 'x y' #classVariableNames: '' #package: >>> 'Kernel-BasicObjects') selectEverySecond) asArray equals: #(#subclass: >>> #instanceVariableNames: #classVariableNames: #package:) >>> testUnzip >>> | uz | >>> uz := #(#Object #subclass: #Point #instanceVariableNames: 'x y' >>> #classVariableNames: '' #package: 'Kernel-BasicObjects') unzip. >>> self assert: uz first asArray equals: #(#Object #Point 'x y' '' >>> 'Kernel-BasicObjects'). >>> self assert: uz second asArray equals: #(#subclass: #instanceVariableNames: >>> #classVariableNames: #package:) >> >> >> Esteban A. Maringolo >> > |
On Thu, Apr 20, 2017 at 12:17:35AM +0300, Yuriy Tymchuk wrote:
> I second the Peter???s idea. I don???t think that we need a dedicated index-based select when we already have withIndex*. Also one year after adding withIndexSelect: we can mine public repos and see how many projects use the method and the selection block does not read the first (each) parameter. > > Uko > > > On 20 Apr 2017, at 00:07, Peter Uhnak <[hidden email]> wrote: > > > > We already have > > > > * #withIndexCollect: > > * #withIndexDo: > > > > > > So why not #withIndexSelect: ? > > > > #(#Object #subclass: #Point #instanceVariableNames:) withIndexSelect: [ :each :i | i \\ 2 = 0 ] > > > > Peter +1 |
The first thing I thought of was: | anArray | anArray := #(#Object #subclass: #Point #instanceVariableNames: 'x y' #classVariableNames: '' #package: 'Kernel-BasicObjects'). (1 to: anArray size by: 2) collect: [:i | anArray at: i] All the best, Ron Teitelbaum On Thu, Apr 20, 2017 at 2:04 AM Alistair Grant <[hidden email]> wrote: On Thu, Apr 20, 2017 at 12:17:35AM +0300, Yuriy Tymchuk wrote: |
In reply to this post by Stephane Ducasse-3
On Thu, Apr 20, 2017 at 3:17 AM, Stephane Ducasse <[hidden email]> wrote:
selectEveryFirst seems a strange name, not indicating the skip amount. The first of every three? or four? As it stand, technically I'd think its result would be equals: (#(#Object #subclass: #Point #instanceVariableNames: 'x y' #classVariableNames: '' #package: 'Kernel-BasicObjects') +1 to Peter's suggested #withIndexSelect: cheers -ben |
2017-04-20 17:06 GMT+02:00 Ben Coman <[hidden email]>:
On the other hand, we have #keysAndValuesDo: which competes with #withIndexDo: and is a bit more portable across dialects (it's just that it turns the bloc parameters the other way around, [:index :element | ]) So maybe this should have been #keysAndValuesSelect: #keysAndValuesCollect: Reminder, keys does not mean Dictionary, in an IndexedCollection (or maybe a SequenceableCollection) the keys are indices. Or the other way around, a Dictionary is indexed by arbitrary keys (not just positive integers). |
In reply to this post by Sven Van Caekenberghe-2
This is what I was looking for. On Wed, Apr 19, 2017 at 10:54 PM, Sven Van Caekenberghe <[hidden email]> wrote: There are already #pairsDo: and #pairsCollect: that could be used: |
In reply to this post by Peter Uhnak
Yes this is probably what I was also looking for. On Wed, Apr 19, 2017 at 11:07 PM, Peter Uhnak <[hidden email]> wrote: We already have |
In reply to this post by Peter Uhnak
Peter can you send an implementation and a bunch of tests :) Stef On Wed, Apr 19, 2017 at 11:07 PM, Peter Uhnak <[hidden email]> wrote: We already have |
In reply to this post by Nicolas Cellier
Hi nicolas Yes but it looks strange :) but indeed having a #keysAndValuesSelect: #keysAndValuesCollect: And it would be good to have #withIndexDo: following the same orders for argument. On Thu, Apr 20, 2017 at 6:31 PM, Nicolas Cellier <[hidden email]> wrote:
|
In reply to this post by Nicolas Cellier
2017-04-20 13:31 GMT-03:00 Nicolas Cellier <[hidden email]>:
> > > 2017-04-20 17:06 GMT+02:00 Ben Coman <[hidden email]>: > On the other hand, we have #keysAndValuesDo: which competes with > #withIndexDo: and is a bit more portable across dialects > (it's just that it turns the bloc parameters the other way around, [:index > :element | ]) > > So maybe this should have been #keysAndValuesSelect: #keysAndValuesCollect: +1 > Reminder, keys does not mean Dictionary, in an IndexedCollection (or maybe a > SequenceableCollection) the keys are indices. Or the other way around, a > Dictionary is indexed by arbitrary keys (not just positive integers). This is what I was trying to explain at the beginning. :) Esteban A. Maringolo |
In reply to this post by Nicolas Cellier
On Fri, Apr 21, 2017 at 12:31 AM, Nicolas Cellier
<[hidden email]> wrote: > > > 2017-04-20 17:06 GMT+02:00 Ben Coman <[hidden email]>: >> >> >> >> On Thu, Apr 20, 2017 at 3:17 AM, Stephane Ducasse >> <[hidden email]> wrote: >>> >>> why? >>> Iterators are powerful and avoid that we all reinvent the wheel in our >>> own corners. >>> >>> About keySelect: I do not see the point to convert a large collection >>> into a dictionary then do yet another pass. >>> To me it looks like a hack. >>> >>> I implemented >>> selectEvery: >>> (selectFirst selectSecond) as helpers. >>> >>> and also unzip all in one pass. >>> Now I have no problem to keep them for me but to me this is the wrong >>> attitude. >>> >>> Stef >>> >>> >>> testSelectEveryFirst >>> self assert: (#(#Object #subclass: #Point #instanceVariableNames: 'x y' >>> #classVariableNames: '' #package: 'Kernel-BasicObjects') selectEveryFirst) >>> asArray equals: #(#Object #Point 'x y' '' 'Kernel-BasicObjects') >> >> >> >> selectEveryFirst seems a strange name, not indicating the skip amount. >> The first of every three? or four? As it stand, technically I'd think its >> result would >> be equals: (#(#Object #subclass: #Point #instanceVariableNames: 'x y' >> #classVariableNames: '' #package: 'Kernel-BasicObjects') >> >> +1 to Peter's suggested #withIndexSelect: >> >> cheers -ben > > > On the other hand, we have #keysAndValuesDo: which competes with > #withIndexDo: and is a bit more portable across dialects > (it's just that it turns the bloc parameters the other way around, [:index > :element | ]) > > So maybe this should have been #keysAndValuesSelect: #keysAndValuesCollect: > > Reminder, keys does not mean Dictionary, in an IndexedCollection (or maybe a > SequenceableCollection) the keys are indices. Or the other way around, a > Dictionary is indexed by arbitrary keys (not just positive integers). Sounds like something I should know, but I'm not quite getting it. Could you provide an example? cheers -ben |
On Fri, Apr 21, 2017 at 09:05:15AM +0800, Ben Coman wrote:
> On Fri, Apr 21, 2017 at 12:31 AM, Nicolas Cellier > <[hidden email]> wrote: > > > > On the other hand, we have #keysAndValuesDo: which competes with > > #withIndexDo: and is a bit more portable across dialects > > (it's just that it turns the bloc parameters the other way around, [:index > > :element | ]) > > > > So maybe this should have been #keysAndValuesSelect: #keysAndValuesCollect: > > > > Reminder, keys does not mean Dictionary, in an IndexedCollection (or maybe a > > SequenceableCollection) the keys are indices. Or the other way around, a > > Dictionary is indexed by arbitrary keys (not just positive integers). > > Sounds like something I should know, but I'm not quite getting it. > Could you provide an example? Is this what you meant? | results | results := OrderedCollection new. #('a' 'b' 'c' 'd') keysAndValuesDo: [ :k :v | results add: { k. v. } ]. results Cheers, Alistair |
On Fri, Apr 21, 2017 at 1:55 PM, Alistair Grant <[hidden email]> wrote: On Fri, Apr 21, 2017 at 09:05:15AM +0800, Ben Coman wrote: I thought you meant "something" else and wasn't sure what. That clears it up. thx. cheers -ben |
Free forum by Nabble | Edit this page |