when iterating over a collection how to determine the current objects index

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

when iterating over a collection how to determine the current objects index

Sanjay Minni
Hi

when iterating over a sequenced collection (array / ordered collection) how can I determine the current objects index value (without explicitly storing a counter or looking to match each time)

Typically I need it:
1. to display a serial number when printing a report.
2. to determine if I am on the last element.
3. To highlight every n-th element.

regards
Sanjay
cheers,
Sanjay
Reply | Threaded
Open this post in threaded view
|

Re: when iterating over a collection how to determine the current objects index

jtuchel
#doWithIndex: ?



> Am 10.03.2015 um 13:11 schrieb Sanjay Minni <[hidden email]>:
>
> Hi
>
> when iterating over a sequenced collection (array / ordered collection) how
> can I determine the current objects index value (without explicitly storing
> a counter or looking to match each time)
>
> Typically I need it:
> 1. to display a serial number when printing a report.
> 2. to determine if I am on the last element.
> 3. To highlight every n-th element.
>
> regards
> Sanjay
>
>
>
>
> -----
> ---
> Regards, Sanjay
> --
> View this message in context: http://forum.world.st/when-iterating-over-a-collection-how-to-determine-the-current-objects-index-tp4810920.html
> Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
>
>

Reply | Threaded
Open this post in threaded view
|

Re: when iterating over a collection how to determine the current objects index

Peter Uhnak
On Tue, Mar 10, 2015 at 1:23 PM, Joachim Tuchel <[hidden email]> wrote:
#doWithIndex: ?
 
doWithIndex: elementAndIndexBlock
"Use the new version with consistent naming"
^ self withIndexDo: elementAndIndexBlock
Reply | Threaded
Open this post in threaded view
|

Re: when iterating over a collection how to determine the current objects index

Thierry Goubier
And one has to guess that elementAndIndexBlock means

[:each :i | ... ] ? (and of course not [:i :each | ... ] )

I'm allways looking for senders with that type of code ;) arguments to blocks are usually not documented.

Thierry

2015-03-10 13:27 GMT+01:00 Peter Uhnák <[hidden email]>:
On Tue, Mar 10, 2015 at 1:23 PM, Joachim Tuchel <[hidden email]> wrote:
#doWithIndex: ?
 
doWithIndex: elementAndIndexBlock
"Use the new version with consistent naming"
^ self withIndexDo: elementAndIndexBlock

Reply | Threaded
Open this post in threaded view
|

Re: when iterating over a collection how to determine the current objects index

Sanjay Minni
In reply to this post by Peter Uhnak
Thanks Joachim and Peter ... Its worked regards Sanjay

cheers,
Sanjay
Reply | Threaded
Open this post in threaded view
|

Re: when iterating over a collection how to determine the current objects index

Sanjay Minni
In reply to this post by Thierry Goubier
Yes usually I have to open the code and see, use intuition or write a sample code to find out
incidentally here I also have to figure out which is the current and which is the deprecated version ... well thats it

sanjay 


---
Sanjay Minni
+91-9900-902902

On Tue, Mar 10, 2015 at 5:57 PM, Thierry Goubier [via Smalltalk] <[hidden email]> wrote:
And one has to guess that elementAndIndexBlock means

[:each :i | ... ] ? (and of course not [:i :each | ... ] )

I'm allways looking for senders with that type of code ;) arguments to blocks are usually not documented.

Thierry

2015-03-10 13:27 GMT+01:00 Peter Uhnák <[hidden email]>:
On Tue, Mar 10, 2015 at 1:23 PM, Joachim Tuchel <[hidden email]> wrote:
#doWithIndex: ?
 
doWithIndex: elementAndIndexBlock
"Use the new version with consistent naming"
^ self withIndexDo: elementAndIndexBlock




If you reply to this email, your message will be added to the discussion below:
http://forum.world.st/when-iterating-over-a-collection-how-to-determine-the-current-objects-index-tp4810920p4810931.html
To unsubscribe from when iterating over a collection how to determine the current objects index, click here.
NAML

cheers,
Sanjay
Reply | Threaded
Open this post in threaded view
|

Re: when iterating over a collection how to determine the current objects index

Ben Coman
In reply to this post by Thierry Goubier





2015-03-10 13:27 GMT+01:00 Peter Uhnák <[hidden email]>:
On Tue, Mar 10, 2015 at 1:23 PM, Joachim Tuchel <[hidden email]> wrote:
#doWithIndex: ?
 
doWithIndex: elementAndIndexBlock
"Use the new version with consistent naming"
^ self withIndexDo: elementAndIndexBlock

On Tue, Mar 10, 2015 at 8:36 PM, Thierry Goubier <[hidden email]> wrote:
And one has to guess that elementAndIndexBlock means

[:each :i | ... ] ? (and of course not [:i :each | ... ] )

The Principle Of Least Surprise would imply that the variables appear in the same order that they appear in the method name. Of course that doesn't mean you don't get surprised sometimes.  Are there any that don't follow that rule?  We should consider fixing them.  It would be interesting if something like autocompletion could supply the template for the block.
 

I'm always looking for senders with that type of code ;) arguments to blocks are usually not documented.



I do too.  
cheers -ben
Reply | Threaded
Open this post in threaded view
|

Re: when iterating over a collection how to determine the current objects index

jtuchel
In reply to this post by Sanjay Minni
Sounds like a bad naming to me. I don't see why withIndexDo is any better. It even contradicts the order of the arguments. And it is Proprietary to Pharo.  

Joachim


Am 10.03.2015 um 13:39 schrieb Sanjay Minni <[hidden email]>:

Yes usually I have to open the code and see, use intuition or write a sample code to find out
incidentally here I also have to figure out which is the current and which is the deprecated version ... well thats it

sanjay 


---
Sanjay Minni
+91-9900-902902

On Tue, Mar 10, 2015 at 5:57 PM, Thierry Goubier [via Smalltalk] <[hidden email]> wrote:
And one has to guess that elementAndIndexBlock means

[:each :i | ... ] ? (and of course not [:i :each | ... ] )

I'm allways looking for senders with that type of code ;) arguments to blocks are usually not documented.

Thierry

2015-03-10 13:27 GMT+01:00 Peter Uhnák <[hidden email]>:
On Tue, Mar 10, 2015 at 1:23 PM, Joachim Tuchel <[hidden email]> wrote:
#doWithIndex: ?
 
doWithIndex: elementAndIndexBlock
"Use the new version with consistent naming"
^ self withIndexDo: elementAndIndexBlock




If you reply to this email, your message will be added to the discussion below:
http://forum.world.st/when-iterating-over-a-collection-how-to-determine-the-current-objects-index-tp4810920p4810931.html
To unsubscribe from when iterating over a collection how to determine the current objects index, click here.
NAML

---
Regards, Sanjay


View this message in context: Re: when iterating over a collection how to determine the current objects index
Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
Reply | Threaded
Open this post in threaded view
|

Re: when iterating over a collection how to determine the current objects index

Sanjay Minni
In reply to this post by Ben Coman

Is there a way to query an object to its index value otherwise in withIndexDo: I have to explicitly store the index values if I need it later in some cases

regards
Sanjay
 
cheers,
Sanjay
Reply | Threaded
Open this post in threaded view
|

Re: when iterating over a collection how to determine the current objects index

Marcus Denker-4
In reply to this post by jtuchel

On 10 Mar 2015, at 14:25, Joachim Tuchel <[hidden email]> wrote:

Sounds like a bad naming to me. I don't see why withIndexDo is any better. It even contradicts the order of the arguments. And it is Proprietary to Pharo.  


No, if you look a the timeStamp it was done in 1997 by Dan. 

Joachim


Am 10.03.2015 um 13:39 schrieb Sanjay Minni <[hidden email]>:

Yes usually I have to open the code and see, use intuition or write a sample code to find out
incidentally here I also have to figure out which is the current and which is the deprecated version ... well thats it

sanjay 


---
Sanjay Minni
+91-9900-902902

On Tue, Mar 10, 2015 at 5:57 PM, Thierry Goubier [via Smalltalk] <<a href="x-msg://6/user/SendEmail.jtp?type=node&amp;node=4810945&amp;i=0" target="_top" rel="nofollow" link="external" class="">[hidden email]> wrote:
And one has to guess that elementAndIndexBlock means

[:each :i | ... ] ? (and of course not [:i :each | ... ] )

I'm allways looking for senders with that type of code ;) arguments to blocks are usually not documented.

Thierry

2015-03-10 13:27 GMT+01:00 Peter Uhnák <[hidden email]>:
On Tue, Mar 10, 2015 at 1:23 PM, Joachim Tuchel <[hidden email]> wrote:
#doWithIndex: ?
 
doWithIndex: elementAndIndexBlock
"Use the new version with consistent naming"
^ self withIndexDo: elementAndIndexBlock




If you reply to this email, your message will be added to the discussion below:
http://forum.world.st/when-iterating-over-a-collection-how-to-determine-the-current-objects-index-tp4810920p4810931.html
To unsubscribe from when iterating over a collection how to determine the current objects index, click here.
NAML

---
Regards, Sanjay


View this message in context: Re: when iterating over a collection how to determine the current objects index
Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|

Re: when iterating over a collection how to determine the current objects index

Thierry Goubier
In reply to this post by Ben Coman


2015-03-10 14:13 GMT+01:00 Ben Coman <[hidden email]>:





2015-03-10 13:27 GMT+01:00 Peter Uhnák <[hidden email]>:
On Tue, Mar 10, 2015 at 1:23 PM, Joachim Tuchel <[hidden email]> wrote:
#doWithIndex: ?
 
doWithIndex: elementAndIndexBlock
"Use the new version with consistent naming"
^ self withIndexDo: elementAndIndexBlock

On Tue, Mar 10, 2015 at 8:36 PM, Thierry Goubier <[hidden email]> wrote:
And one has to guess that elementAndIndexBlock means

[:each :i | ... ] ? (and of course not [:i :each | ... ] )

The Principle Of Least Surprise would imply that the variables appear in the same order that they appear in the method name. Of course that doesn't mean you don't get surprised sometimes.  Are there any that don't follow that rule?  We should consider fixing them.

In Pharo 4. GLMTableColumn>>#modifiedBlock: anObject.

I tried to follow the trail and it's a three arguments block, according to GLMTreeMorphNodeModel>>#rowMorphForColumn: , since we see:

aGlamourColumn modifiedBlock cull: contentMorph text cull: self item cull: self containerTree glamourPresentation

Following senders, I have

GLMTablePresentation>>column: aBlockOrString evaluated: aBlock modified: aModifiedBlock,

(still no documentation)

and:

GTObjectVariablesBrowser>>variablesIn: composite

where I find:

column: 'Value' 
evaluated: [:assoc | GTObjectPrinter new asTruncatedTextFrom: assoc value ]
modified: [:newValue :assoc :presentation | 
self updateInstanceVariable: assoc key from: presentation entity basedOn: newValue.
presentation update ];

Now, I know :)

A good experience: use a finder or spotter, search for senders of cull:cull:cull:. I choose the first one I found for the example above (I have a memory of some which were annoying, in Morphic or Spec, but couldn't remember which ones).

  It would be interesting if something like autocompletion could supply the template for the block.

Ouch. It would not be easy, but possible. A pragma ? ;)

Thierry
Reply | Threaded
Open this post in threaded view
|

Re: when iterating over a collection how to determine the current objects index

jtuchel
In reply to this post by Marcus Denker-4
Marcus
 
So sorry for this false accusation. It is proprietary in Squeak.
And even if it was Dan's idea to rename it, I'd like to learn more about the reasoning.
My understanding would be:
 
#do: iterates over a collection
#doWithIndex: iterates over a collection and also keeps track of the current index.
 
Sounds very consistent to me. the most important thing I want the machine to do is iterate over the collection, and the index thing is just a variant thereof.
 
#do: iterates over a collection
#withIndexDo: keeps track of the index while iterating over a collection
 
Sounds clumsy to me. Introduces incompatibilities for not much value. If people complained about #do: as not intentioon revealing and opted to rename it to something like #withEachDo: , then I could understand it. The variant could be #withEachDoIndexed:
 
But not that one ;-)
 
... but this is completely off-topic of course...
 
Joachim
 
Marcus Denker <[hidden email]> hat am 10. März 2015 um 14:32 geschrieben:


On 10 Mar 2015, at 14:25, Joachim Tuchel < [hidden email]> wrote:

Sounds like a bad naming to me. I don't see why withIndexDo is any better. It even contradicts the order of the arguments. And it is Proprietary to Pharo.  
 
 
No, if you look a the timeStamp it was done in 1997 by Dan. 

Joachim


Am 10.03.2015 um 13:39 schrieb Sanjay Minni < [hidden email]>:

Yes usually I have to open the code and see, use intuition or write a sample code to find out
incidentally here I also have to figure out which is the current and which is the deprecated version ... well thats it
 
sanjay 
 

---
Sanjay Minni
+91-9900-902902

On Tue, Mar 10, 2015 at 5:57 PM, Thierry Goubier [via Smalltalk] <[hidden email]> wrote:
And one has to guess that elementAndIndexBlock means
 
[:each :i | ... ] ? (and of course not [:i :each | ... ] )
 
I'm allways looking for senders with that type of code ;) arguments to blocks are usually not documented.
 
Thierry

2015-03-10 13:27 GMT+01:00 Peter Uhnák <[hidden email]>:
On Tue, Mar 10, 2015 at 1:23 PM, Joachim Tuchel <[hidden email]> wrote:
#doWithIndex: ?
 
doWithIndex: elementAndIndexBlock
"Use the new version with consistent naming"
^ self withIndexDo: elementAndIndexBlock



If you reply to this email, your message will be added to the discussion below:
http://forum.world.st/when-iterating-over-a-collection-how-to-determine-the-current-objects-index-tp4810920p4810931.html
To unsubscribe from when iterating over a collection how to determine the current objects index, click here.
NAML
---
Regards, Sanjay


View this message in context: Re: when iterating over a collection how to determine the current objects index
Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.

 
Reply | Threaded
Open this post in threaded view
|

Re: when iterating over a collection how to determine the current objects index

Sanjay Minni

sorry folks ...

so back to terra firma ...

1. Is there any way to query an object to finds its index value in the original collection ...
say when all the objects from a collection are displayed and user clicks on one item - I want to locate the object back in the collection. Knowing the index for me would be most efficient.

2. ... also Is there any way to know if I am on the last element ...

otherwise I will have to explicitly store the index values ...

regards
Sanjay


jtuchel wrote
Marcus
 
So sorry for this false accusation. It is proprietary in Squeak.
And even if it was Dan's idea to rename it, I'd like to learn more about the
reasoning.
My understanding would be:
 
[...]
cheers,
Sanjay
Reply | Threaded
Open this post in threaded view
|

Re: when iterating over a collection how to determine the current objects index

Thierry Goubier
Hi Sanjay,

2015-03-10 14:47 GMT+01:00 Sanjay Minni <[hidden email]>:

sorry folks ...

so back to terra firma ...

1. Is there any way to query an object to finds its index value in the
original collection ...
say when all the objects from a collection are displayed and user clicks on
one item - I want to locate the object back in the collection. Knowing the
index for me would be most efficient.

aCollection indexOf: anElement

You will often find this code in list-related gui objects.
 

2. ... also Is there any way to know if I am on the last element ...

aCollection last == anElement
 

otherwise I will have to explicitly store the index values ...

If you do it more than once per iteration of the collection, it is probably faster to store the indexes. But I wouldn't do it for anything GUI related; indexOf: is efficient enough.

Regards,

Thierry 

Reply | Threaded
Open this post in threaded view
|

Re: when iterating over a collection how to determine the current objects index

jtuchel
In reply to this post by Sanjay Minni
(aCollection indexOf: anObject) = aCollection size



> Am 10.03.2015 um 14:47 schrieb Sanjay Minni <[hidden email]>:
>
>
> sorry folks ...
>
> so back to terra firma ...
>
> 1. Is there any way to query an object to finds its index value in the
> original collection ...
> say when all the objects from a collection are displayed and user clicks on
> one item - I want to locate the object back in the collection. Knowing the
> index for me would be most efficient.
>
> 2. ... also Is there any way to know if I am on the last element ...
>
> otherwise I will have to explicitly store the index values ...
>
> regards
> Sanjay
>
>
>
> jtuchel wrote
>> Marcus
>>
>> So sorry for this false accusation. It is proprietary in Squeak.
>> And even if it was Dan's idea to rename it, I'd like to learn more about
>> the
>> reasoning.
>> My understanding would be:
>>
>> [...]
>
>
>
>
>
> -----
> ---
> Regards, Sanjay
> --
> View this message in context: http://forum.world.st/when-iterating-over-a-collection-how-to-determine-the-current-objects-index-tp4810920p4810984.html
> Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
>
>

Reply | Threaded
Open this post in threaded view
|

Re: when iterating over a collection how to determine the current objects index

Peter Uhnak
In reply to this post by Sanjay Minni
1. Is there any way to query an object to finds its index value in the
original collection ...
say when all the objects from a collection are displayed and user clicks on
one item - I want to locate the object back in the collection. Knowing the
index for me would be most efficient.
============================== 
indexOf: anElement
"Answer the index of the first occurrence of anElement within the  
receiver. If the receiver does not contain anElement, answer 0."

^ self indexOf: anElement ifAbsent: 0
============================== 

don't be afraid to look inside Pharo and on Collection protocols.
 
2. ... also Is there any way to know if I am on the last element ...
I'm not sure what you are trying to achieve, but you can just compare against the last element

(myCollection last = myObject) ifTrue: [ do something ]
Reply | Threaded
Open this post in threaded view
|

Re: when iterating over a collection how to determine the current objects index

Peter Uhnak
In reply to this post by Ben Coman
The Principle Of Least Surprise would imply that the variables appear in the same order that they appear in the method name. Of course that doesn't mean you don't get surprised sometimes.  Are there any that don't follow that rule?  We should consider fixing them.  It would be interesting if something like autocompletion could supply the template for the block.

It may also depend on which part you apply the principle of least surprise... I personally would expect it to be in the ":key :value" order regardless of the name of the method...  most do methods end with 'Do:' so I wouldn't take that as factor in this.

Peter
Reply | Threaded
Open this post in threaded view
|

Re: when iterating over a collection how to determine the current objects index

NorbertHartl
In reply to this post by jtuchel
Always try to do it as you would say it

anObject = aCollection last

Norbert

> Am 10.03.2015 um 15:05 schrieb Joachim Tuchel <[hidden email]>:
>
> (aCollection indexOf: anObject) = aCollection size
>
>
>
>> Am 10.03.2015 um 14:47 schrieb Sanjay Minni <[hidden email]>:
>>
>>
>> sorry folks ...
>>
>> so back to terra firma ...
>>
>> 1. Is there any way to query an object to finds its index value in the
>> original collection ...
>> say when all the objects from a collection are displayed and user clicks on
>> one item - I want to locate the object back in the collection. Knowing the
>> index for me would be most efficient.
>>
>> 2. ... also Is there any way to know if I am on the last element ...
>>
>> otherwise I will have to explicitly store the index values ...
>>
>> regards
>> Sanjay
>>
>>
>>
>> jtuchel wrote
>>> Marcus
>>>
>>> So sorry for this false accusation. It is proprietary in Squeak.
>>> And even if it was Dan's idea to rename it, I'd like to learn more about
>>> the
>>> reasoning.
>>> My understanding would be:
>>>
>>> [...]
>>
>>
>>
>>
>>
>> -----
>> ---
>> Regards, Sanjay
>> --
>> View this message in context: http://forum.world.st/when-iterating-over-a-collection-how-to-determine-the-current-objects-index-tp4810920p4810984.html
>> Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
>>
>>
>


Reply | Threaded
Open this post in threaded view
|

Re: when iterating over a collection how to determine the current objects index

stepharo
In reply to this post by jtuchel


Le 10/3/15 14:40, Joachim Tuchel a écrit :
Marcus
 
So sorry for this false accusation. It is proprietary in Squeak.
And even if it was Dan's idea to rename it, I'd like to learn more about the reasoning.
My understanding would be:
 
#do: iterates over a collection
#doWithIndex: iterates over a collection and also keeps track of the current index.
 
Sounds very consistent to me. the most important thing I want the machine to do is iterate over the collection, and the index thing is just a variant thereof.
 
#do: iterates over a collection
#withIndexDo: keeps track of the index while iterating over a collection
 
Sounds clumsy to me. Introduces incompatibilities for not much value. If people complained about #do: as not intentioon revealing and opted to rename it to something like #withEachDo: , then I c
+1

ould understand it. The variant could be #withEachDoIndexed:
 
But not that one ;-)
 
... but this is completely off-topic of course...
 
Joachim
 
Marcus Denker [hidden email] hat am 10. März 2015 um 14:32 geschrieben:


On 10 Mar 2015, at 14:25, Joachim Tuchel < [hidden email]> wrote:

Sounds like a bad naming to me. I don't see why withIndexDo is any better. It even contradicts the order of the arguments. And it is Proprietary to Pharo.  
 
 
No, if you look a the timeStamp it was done in 1997 by Dan. 

Joachim


Am 10.03.2015 um 13:39 schrieb Sanjay Minni < [hidden email]>:

Yes usually I have to open the code and see, use intuition or write a sample code to find out
incidentally here I also have to figure out which is the current and which is the deprecated version ... well thats it
 
sanjay 
 

---
Sanjay Minni
+91-9900-902902

On Tue, Mar 10, 2015 at 5:57 PM, Thierry Goubier [via Smalltalk] <[hidden email]> wrote:
And one has to guess that elementAndIndexBlock means
 
[:each :i | ... ] ? (and of course not [:i :each | ... ] )
 
I'm allways looking for senders with that type of code ;) arguments to blocks are usually not documented.
 
Thierry

2015-03-10 13:27 GMT+01:00 Peter Uhnák <[hidden email]>:
On Tue, Mar 10, 2015 at 1:23 PM, Joachim Tuchel <[hidden email]> wrote:
#doWithIndex: ?
 
doWithIndex: elementAndIndexBlock
"Use the new version with consistent naming"
^ self withIndexDo: elementAndIndexBlock



If you reply to this email, your message will be added to the discussion below:
http://forum.world.st/when-iterating-over-a-collection-how-to-determine-the-current-objects-index-tp4810920p4810931.html
To unsubscribe from when iterating over a collection how to determine the current objects index, click here.
NAML
---
Regards, Sanjay


View this message in context: Re: when iterating over a collection how to determine the current objects index
Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.

 

Reply | Threaded
Open this post in threaded view
|

Re: when iterating over a collection how to determine the current objects index

Sean P. DeNigris
Administrator
In reply to this post by NorbertHartl
NorbertHartl wrote
anObject = aCollection last
Of course the above and #indexOf: will only work if there are no duplicates in the collection and #indexOf: will require a lot of extra iterations. If you need the indices associated to the objects, maybe you should wrap them e.g. "indexedObjects := objects collectWithIndex: [ :e :i | IndexedObject object: e index: i ]". Then you can cleanly access the indices whenever you want.
Cheers,
Sean
12