[RCR] Numeric#of

A

ara.t.howard

i threw this out before. thought i'd try again:

harp:~ > irb
irb(main):001:0> class Numeric; def of(&b) Array::new(self).map(&b) end; end
=> nil

irb(main):002:0> 42.of{ String::new }
=> ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]


regards.

-a
 
Y

Yukihiro Matsumoto

Hi,

i threw this out before. thought i'd try again:

harp:~ > irb
irb(main):001:0> class Numeric; def of(&b) Array::new(self).map(&b) en= d; end
=3D> nil

irb(main):002:0> 42.of{ String::new }
=3D> ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", =
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",=
"", "", "", "", "", "", ""]

Using my not-yet-committed 1.9, you can type

42.times.collect{String::new}

to get the same result.

matz.
 
J

Jacob Fugal

harp:~ > irb
irb(main):001:0> class Numeric; def of(&b) Array::new(self).map(&b) = end; end
=3D> nil

irb(main):002:0> 42.of{ String::new }
=3D> ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""=
, "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "=
", "", "", "", "", "", "", ""]
Using my not-yet-committed 1.9, you can type

42.times.collect{String::new}

to get the same result.

Cool!

So does times still work with a block? I'm assuming that it switches
behavior, being a generator itself if a block is present, and creating
a generator object (which includes Enumerable) when no block is
provided.

Eg. (simplified):

class Numeric
def times( &blk )
generator =3D # some code to create the generator
# that runs from 0 to self-1
generator.each &blk if block_given?
generator
end
end

If that's the case, this is awesome!

Jacob Fugal
 
M

Matthew Moss

All I did was

class Integer
def times
if block_given?
(0...self).each { |i| yield i }
else
(0...self)
end
end
end

3.times.collect { String.new }
=3D> ["", "", ""]
 
A

ara.t.howard

Hi,

i threw this out before. thought i'd try again:

harp:~ > irb
irb(main):001:0> class Numeric; def of(&b) Array::new(self).map(&b) end; end
=> nil

irb(main):002:0> 42.of{ String::new }
=> ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]

Using my not-yet-committed 1.9, you can type

42.times.collect{String::new}

to get the same result.

great! i'm still partial to the shorter version since

42.times.collect{String::new}

isn't that much shorter/prettier than my current idiom of

Array::new(42).map{String::new}

but it's the capability that's needed so this is good news.

kind regards.

-a
 
J

James Edward Gray II

Array::new(42).map{String::new}

That can be shortened to:
=> ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", ""]

James Edward Gray II
 
A

ara.t.howard

Array::new(42).map{String::new}

That can be shortened to:
=> ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", ""]

indeed. my bad.

none still as pretty as '42.of' though! ;-)

-a
 
P

Phil Duby

James Edward Gray II said:
Array::new(42).map{String::new}

That can be shortened to:
=> ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", ""]

James Edward Gray II
Thank for these!!

I was looking for a clean (Ruby) way of creating an array of ascending
integer values, with the final value zero. IE [ 1, 2, 3, 4, 0 ]
i = 0; a = Array.new( 4 ) { i += 1 }.push 0
does what I want.
 
P

Phil Duby

James Edward Gray II said:
Array::new(42).map{String::new}

That can be shortened to:
=> ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", ""]

James Edward Gray II
Thank for these!!

I was looking for a clean (Ruby) way of creating an array of ascending
integer values, with the final value zero. IE [ 1, 2, 3, 4, 0 ]
i = 0; a = Array.new( 4 ) { i += 1 }.push 0
does what I want.
 
W

William James

i threw this out before. thought i'd try again:

harp:~ > irb
irb(main):001:0> class Numeric; def of(&b) Array::new(self).map(&b) end; end
=> nil

irb(main):002:0> 42.of{ String::new }
=> ["", "", "", "", "", "", "", "", "", "", . . .

class Fixnum
def of
(0...self).map{ yield }
end
end

p 3.of { "" }

===> ["", "", ""]
 
Y

Yukihiro Matsumoto

Hi,

So does times still work with a block?
Yes.

I'm assuming that it switches
behavior, being a generator itself if a block is present, and creating
a generator object (which includes Enumerable) when no block is
provided.

If returns a enumerator object just like other enumerable methods do
if no block is given to them.

matz.
 
T

Trans

The coming dawn of Role-Oriented Progamming is upon us (such is the way
of the Functor).
 
L

Logan Capaldo

--Apple-Mail-4--593028061
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
format=flowed


If returns a enumerator object just like other enumerable methods do
if no block is given to them.

matz.

How deep does this rabbit hole go?

Does map return an enumerable without a block? And if so, can we type
obj.map.map.map... til the end of time?


--Apple-Mail-4--593028061--
 
M

Martin DeMello

Phil Duby said:
I was looking for a clean (Ruby) way of creating an array of ascending
integer values, with the final value zero. IE [ 1, 2, 3, 4, 0 ]
i = 0; a = Array.new( 4 ) { i += 1 }.push 0
does what I want.

(1..4).to_a.push(0)

also Array.new passes in the index to the block, so you can do

Array.new(4) {|i| i+1}

martin
 
R

Robert Klemme

Martin said:
Phil Duby said:
I was looking for a clean (Ruby) way of creating an array of
ascending integer values, with the final value zero. IE [ 1, 2, 3,
4, 0 ]
i = 0; a = Array.new( 4 ) { i += 1 }.push 0
does what I want.

(1..4).to_a.push(0)

also Array.new passes in the index to the block, so you can do

Array.new(4) {|i| i+1}

Here are some more alternatives including a solution with the famous
#inject. :)
a=Array.new(5) {|i| (i+1)%5} => [1, 2, 3, 4, 0]
a=(1..4).to_a << 0 => [1, 2, 3, 4, 0]
a=(1..5).inject([]) {|ar,i| ar << (i%5)} => [1, 2, 3, 4, 0]
a=(1..5).inject([]) {|ar,i| ar << i} << 0
=> [1, 2, 3, 4, 5, 0]

Have fun!

Kind regards

robert
 
R

Robert Klemme

Robert said:
Martin said:
Phil Duby said:
I was looking for a clean (Ruby) way of creating an array of
ascending integer values, with the final value zero. IE [ 1, 2, 3,
4, 0 ]
i = 0; a = Array.new( 4 ) { i += 1 }.push 0
does what I want.

(1..4).to_a.push(0)

also Array.new passes in the index to the block, so you can do

Array.new(4) {|i| i+1}

Here are some more alternatives including a solution with the famous
#inject. :)
a=Array.new(5) {|i| (i+1)%5} => [1, 2, 3, 4, 0]
a=(1..4).to_a << 0 => [1, 2, 3, 4, 0]
a=(1..5).inject([]) {|ar,i| ar << (i%5)} => [1, 2, 3, 4, 0]
a=(1..5).inject([]) {|ar,i| ar << i} << 0
=> [1, 2, 3, 4, 5, 0]

Pardon me, the last one should've read:
a=(1..4).inject([]) {|ar,i| ar << i} << 0
=> [1, 2, 3, 4, 0]

robert
 
J

Jacob Fugal

If returns a enumerator object just like other enumerable methods do
if no block is given to them.

Sweet! I've wanted this sort of enumerator generation for a while. Is
this new across the board in 1.9, or have I just been stupid in
assuming it didn't work when it has this whole time?

Jacob Fugal
 
A

Andrew Johnson

Bah, too long!
[""] * 42
Sorry, just had to jump in :)
.adam sanderson

Except that:

a = [""] * 5 # => ["", "", "", "", ""]
a[0] << "foo" # => "foo"
a # => ["foo", "foo", "foo", "foo", "foo"]

Whereas:

c = Array.new(5){""} # => ["", "", "", "", ""]
c[0] << "bar" # => "bar"
c # => ["bar", "", "", "", ""]

andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top