doWithIndex: or withIndexDo: ?

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

doWithIndex: or withIndexDo: ?

Oleks
Hello,

I am working on a new collection and this question caught my interest.
Between doWithIndex: or withIndexDo:, which do you find more logical and why?

Oleks
Reply | Threaded
Open this post in threaded view
|

Re: doWithIndex: or withIndexDo: ?

Esteban A. Maringolo
I don't like either of those and use #keysAndValuesDo: as it is polymorphic with other collection classes.

That said, I'd prefer #withIndexDo: [:each :index | ... ] because it preserves the #with:do: structure.

e.g.
#($a $b $c) with: (1 2 3) do: [:char :number | ... ]

Regards,

Esteban A. Maringolo


El mar., 12 mar. 2019 a las 12:27, Oleksandr Zaytsev (<[hidden email]>) escribió:
Hello,

I am working on a new collection and this question caught my interest.
Between doWithIndex: or withIndexDo:, which do you find more logical and why?

Oleks
Reply | Threaded
Open this post in threaded view
|

Re: doWithIndex: or withIndexDo: ?

Henrik Sperre Johansen
In reply to this post by Oleks
#doWithIndex: for me.
 
Both because it's the selector I'm used to from other dialects, and the
parameter order [:element :index | ] makes more sense to me.

Cheers,
Henry



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Reply | Threaded
Open this post in threaded view
|

Re: doWithIndex: or withIndexDo: ?

Tim Mackinnon
Yes - I like the prefix doXXX too, as I often think first of do: for normal iteration, with suffixed selectors for other variations.

> On 12 Mar 2019, at 16:54, Henrik Sperre Johansen <[hidden email]> wrote:
>
> #doWithIndex: for me.
>
> Both because it's the selector I'm used to from other dialects, and the
> parameter order [:element :index | ] makes more sense to me.
>
> Cheers,
> Henry
>
>
>
> --
> Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html
>


Reply | Threaded
Open this post in threaded view
|

Re: doWithIndex: or withIndexDo: ?

Richard O'Keefe
In reply to this post by Oleks
Neither.  No two ways about it.  #keysAndValuesDo:
However,
  seq with: seq keys do: aBlock
and
  seq withIndexDo: aBlock
do the same thing, so #withIndexDo: makes some sense.

This is not my opinion alone.  I'm typing this on a machine
that has Squeak but not Pharo.  In Squeak we find

doWithIndex: elementAndIndexBlock
    "Use the new version with consistent naming"
    ^ self withIndexDo: elementAndIndexBlock

"New" means since 1997.

On Wed, 13 Mar 2019 at 04:27, Oleksandr Zaytsev <[hidden email]> wrote:
Hello,

I am working on a new collection and this question caught my interest.
Between doWithIndex: or withIndexDo:, which do you find more logical and why?

Oleks