Accessing index inside map

#> Wouldn't my algorithm slow down? Does not the chain begs for
#a single method
#> -which brings us back to the original question...?
#>

···

daz [mailto:dooby@d10.karoo.co.uk] wrote:

#
#Yes. I'm taking sedatives while it's seriously being considered that
#an Array should become an Enumerator in order to use its
##with_index method
#on an intermediate #map object to produce ... an Array ...
#instead of the
#regular #map method with an optional index.
#
#I would have posted a link to the ruby-core thread, but it was my
#first post to a mailing list and my MAIL settings were different
#from my NEWS settings, so it's messed up (quoted-printable MIME).
#
#Here's a text version (minor EDITs). I'm sure it'll be
#relevant to you.
#
# http://www.d10.karoo.net/ruby/xv/ruby-core-1495.txt
#
#You'll see examples using bang! methods.
#
#(( I'd be fascinated to see map!.with_index or map.with_index!
# produce anything intelligible using require 'enumerator'. ))
#

cool. you're a ruby hacker indeed.

but imho (my opinion only) i do not like the ||.x notation.

how about

# we only need the index
[1,2,3].each() {|i|* printf i,"\n"}

# we need the val and the index
[1,2,3].each() {|x,i|* printf x,i,"\n"}

# in other words, we always get the last param as the index
[[0,1],[1,2],[3,4]].each() {|x,y,i|* printf x,i,"\n"}

ie, we put a star after the param delimiters || to signal an index. It's
like telling the programmer/reader "*hint-hint*"

kind regards -botp

#daz
#
#
#
#

Peña, Botp wrote:

[...] but imho (my opinion only) i do not like the ||.x notation.

OK. You're not the first and you won't be the last :wink:
But more important than how it looks is whether it alters meaning.

how about

# we only need the index
[1,2,3].each() {|i|* printf i,"\n"}

# we need the val and the index
[1,2,3].each() {|x,i|* printf x,i,"\n"}

# in other words, we always get the last param as the index
[[0,1],[1,2],[3,4]].each() {|x,y,i|* printf x,i,"\n"}

ie, we put a star after the param delimiters || to signal an index. It's
like telling the programmer/reader "*hint-hint*"

[1].each { |n, i| x=n; p x } #-> 1
[1].each { |n, i|* x=n; p x } #-> [1]

Great care is needed when adding beyond the |bars| into code territory.
But /no damage/ is caused by thinking aloud :slight_smile:

For #each_with_index, the index is passed as a block parameter.
That method has _two_ block parameters. That's probably why we
expect to see the index there in an alternative implementation
with the same result.

[2].each { |n | p [n ] } #-> [2]
[2].each_with_index { |n, index | p [n, index] } #-> [2, 0]

Let's say that we were offered no choice of name and that an
automatic block index were called $INDX.

[2].meth { |n | p [n ] } #-> [2]
[2].meth { |n | p [n, $INDX] } #-> [2, 0]

#meth passes _one_ argument and, regardless of whether or not
we use the $INDX, it still only passes _one_ argument.
'$INDX' isn't passed at all ... it's just ... /there/.

IMPO, auto-index-naming wouldn't belong between the bars but I
would be interested in hearing contrasting views.

Your suggestion, correctly, highlights the problem that an extra
block parameter, by itself, doesn't indicate to the parser that
it's an index. Your extra '*' marker does that.

Interesting.

daz