Ruby idiom (0...(expr)).map

I’ve found myself writing (0…(expr)).map (as an alternative to
a = []; expr.times {|i| a << …}) often enough that I was wondering
whether to define it as a method of fixnum. Fixnum#map doesn’t seem
quite right, though - any name suggestions?

martin

I vaguely remember this idiom. Can you expand on its use? I think it
may become outdated with 1.8 features.

Gavin

···

On Wednesday, January 22, 2003, 2:36:50 AM, Martin wrote:

I’ve found myself writing (0…(expr)).map (as an alternative to
a = ; expr.times {|i| a << …}) often enough that I was wondering
whether to define it as a method of fixnum. Fixnum#map doesn’t seem
quite right, though - any name suggestions?

In general, making an array out of an integer sequence (in its most
basic form, (0…10).to_a # => [0,1,2,3,4,5,6,7,8,9].)

More usefully, Range#map is a shortcut for Range.to_a.map, but since I
most commonly want to start from 0, I thought a further shortcut for
(0…n).map would make my code prettier.

Frinstance, here’s a snippet from some code I’m currently writing to
format text into columns:

npages.times {|i| ranges.push((pagebreak * i)…(pagebreaki+psize-1))}
ranges.each {|r|
page << r.map {|line|
(0…ncols).map {|col| text[line + col
psize]}.join(col_sep)
}
}

where I want an array each element of which is a function of the current
index. Here it’s not so bad, since I’ve already had occasion to define
ncols, but if it were an inline expression, the code would start looking
really messy.

martin

···

Gavin Sinclair gsinclair@soyabean.com.au wrote:

On Wednesday, January 22, 2003, 2:36:50 AM, Martin wrote:

I’ve found myself writing (0…(expr)).map (as an alternative to
a = ; expr.times {|i| a << …}) often enough that I was wondering
whether to define it as a method of fixnum. Fixnum#map doesn’t seem
quite right, though - any name suggestions?

I vaguely remember this idiom. Can you expand on its use? I think it
may become outdated with 1.8 features.

o.k. i smell yet another ‘inject’ post. are you aware of any docs on the 1.8
release or are you just a whole lot more ambitious than i and checking out the
sources?

-a

···

On Wed, 22 Jan 2003, Gavin Sinclair wrote:

I’ve found myself writing (0…(expr)).map (as an alternative to
a = ; expr.times {|i| a << …}) often enough that I was wondering
whether to define it as a method of fixnum. Fixnum#map doesn’t seem
quite right, though - any name suggestions?

I vaguely remember this idiom. Can you expand on its use? I think it
may become outdated with 1.8 features.

====================================

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

Well, you always have “occasion” to define the “expr” in (0…expr).
If it’s inline and messy, you create an expression variable.

I can’t see that particular idiom disappearing in a hurry. Have you
tried the shortcut yourself, with pleasant results?

class Fixnum
def map(&block)
(0…self).map &block
end
end

Gavin

···

On Wednesday, January 22, 2003, 4:17:08 AM, Martin wrote:

More usefully, Range#map is a shortcut for Range.to_a.map, but since I
most commonly want to start from 0, I thought a further shortcut for
(0…n).map would make my code prettier.

Frinstance, here’s a snippet from some code I’m currently writing to
format text into columns:

npages.times {|i| ranges.push((pagebreak * i)…(pagebreaki+psize-1))}
ranges.each {|r|
page << r.map {|line|
(0…ncols).map {|col| text[line + col
psize]}.join(col_sep)
}
}

where I want an array each element of which is a function of the current
index. Here it’s not so bad, since I’ve already had occasion to define
ncols, but if it were an inline expression, the code would start looking
really messy.

Hello ahoward,

Tuesday, January 21, 2003, 8:57:22 PM, you wrote:

I’ve found myself writing (0…(expr)).map (as an alternative to
a = ; expr.times {|i| a << …}) often enough that I was wondering
whether to define it as a method of fixnum. Fixnum#map doesn’t seem
quite right, though - any name suggestions?

may be

Array.new(expr) {|i| i}

···


Best regards,
Bulat mailto:bulatz@integ.ru

Well, you always have “occasion” to define the “expr” in (0…expr).
If it’s inline and messy, you create an expression variable.

Yeah, but that has an unnecessary feel to it.

I can’t see that particular idiom disappearing in a hurry. Have you
tried the shortcut yourself, with pleasant results?

class Fixnum
def map(&block)
(0…self).map &block
end
end

Nope - since the main purpose was to make my code read better, I decided
to adopt matz’s philosohpy and wait until I’ve found the perfect name
for it. Fixnum#map doesn’t sound right, IMO.

martin

···

Gavin Sinclair gsinclair@soyabean.com.au wrote:

i didn’t actually write that post, but i’ve been thinking of simply having
new/initialize take a block… something like this :

class Array
def self.init n = 0, o = nil, &b
if b
a = new n
n.times {|i| a[i] = o ? b.call(o) : b.call(i)}
a
else
self.new n, o
end
end
end

if $0 == FILE
ary = [0,1,2]

p Array.init 1, 42 # >> [42]
p Array.init (3, ) {|a| a.clone} # >> [, , ]
p Array.init (3, ary) {|a| a.pop} # >> [2, 1, 0]
p Array.init (4) {|i| i} # >> [0, 1, 2, 3]
p Array.init (2) {[rand]} # >> [[0.3466412831], [0.7881289076]]
end

if a default object is given, the block is called with that object, otherwise
the block is called with the index being initialized

.... someone will fill this in ;-) ...

-a

···

On Fri, 24 Jan 2003, Bulat Ziganshin wrote:

Hello ahoward,

Tuesday, January 21, 2003, 8:57:22 PM, you wrote:

I’ve found myself writing (0…(expr)).map (as an alternative to
a = ; expr.times {|i| a << …}) often enough that I was wondering
whether to define it as a method of fixnum. Fixnum#map doesn’t seem
quite right, though - any name suggestions?

may be

Array.new(expr) {|i| i}

====================================

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================