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 |
#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. > > |
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 |
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]>:
|
In reply to this post by Peter Uhnak
Thanks Joachim and Peter ... Its worked regards Sanjay
cheers,
Sanjay |
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 On Tue, Mar 10, 2015 at 5:57 PM, Thierry Goubier [via Smalltalk] <[hidden email]> wrote:
cheers,
Sanjay |
In reply to this post by Thierry Goubier
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 do too. cheers -ben |
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
|
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 |
In reply to this post by jtuchel
No, if you look a the timeStamp it was done in 1997 by Dan.
|
In reply to this post by Ben Coman
2015-03-10 14:13 GMT+01:00 Ben Coman <[hidden email]>:
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).
Ouch. It would not be easy, but possible. A pragma ? ;) Thierry |
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: |
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
cheers,
Sanjay |
Hi Sanjay,
2015-03-10 14:47 GMT+01:00 Sanjay Minni <[hidden email]>:
aCollection indexOf: anElement You will often find this code in list-related gui objects.
aCollection last == anElement
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 |
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. > > |
In reply to this post by Sanjay Minni
1. Is there any way to query an object to finds its index value in the ============================== 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 ] |
In reply to this post by Ben Coman
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 |
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. >> >> > |
In reply to this post by jtuchel
Le 10/3/15 14:40, Joachim Tuchel a
écrit :
+1
|
Administrator
|
In reply to this post by NorbertHartl
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 |
Free forum by Nabble | Edit this page |