[RCR] Numeric#of

this got a generally favourable reception so:

RCR - Numeric#of - an accumlative version of Numeric#times

~ > cat of.rb
class Numeric
def of
ret =
times{|i| ret << yield(i)}
ret
end
end

them = 3.of{ Array.new }
p them # => [,,]

-a

···

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
URL :: Solar-Terrestrial Physics Data | NCEI
“640K ought to be enough for anybody.” - Bill Gates, 1981
===============================================================================

Hi,

···

In message “[RCR] Numeric#of” on 04/05/25, “Ara.T.Howard” ahoward@noaa.gov writes:

this got a generally favourable reception so:

RCR - Numeric#of - an accumlative version of Numeric#times

What if n is very large number?

Besides that Numeric#of is not very intuitive for me (non native
English speaker). It seems too vague for me. Is the name “of” really
OK for you guys?

						matz.

Ara.T.Howard wrote:

RCR - Numeric#of - an accumlative version of Numeric#times
[…]
them = 3.of{ Array.new }

It also looks awfully terse. How about:

them = 3.times_collect{ Array.new }

I think it reads better. “Three times, collect [result of] Array.new”.

It is three characters longer than the (seemingly) equivalent:

Array.new(3){Array.new}

but far more readable, and could assume another top-level container.

Just my 0.02 NOK.

···


([ Kent Dahl ]/)_ ~ [ Kent Dahl - Kent Dahl ]/~
))_student_/(( _d L b_/ Master of Science in Technology )
( __õ|õ// ) ) Industrial economics and technology management (
_
/ö____/ (_engineering.discipline=Computer::Technology)

“Ara.T.Howard” ahoward@noaa.gov wrote in message news:Pine.LNX.4.44.0405241938530.20070-100000@fattire.ngdc.noaa.gov

this got a generally favourable reception so:

RCR - Numeric#of - an accumlative version of Numeric#times

~ > cat of.rb
class Numeric
def of
ret =
times{|i| ret << yield(i)}
ret
end
end

them = 3.of{ Array.new }
p them # => [,,]

To me, creating multiple instances of initialized objects feels like
it belongs more with the new call, rather than Integer. Perhaps we
could add a “multiple instance” new? Here is one implementation.

def Object.newx( nx, *args)
instances =
if( block_given? )
nx.times { instances.push yield(self.new(*args)) }
else
nx.times { instances.push self.new(*args) }
end
instances
end

a,b,c = Array.newx(3)

Hi,

this got a generally favourable reception so:

RCR - Numeric#of - an accumlative version of Numeric#times

What if n is very large number?

same problem with using #map vs. #map! isn’t it?

Besides that Numeric#of is not very intuitive for me (non native English
speaker). It seems too vague for me. Is the name “of” really OK for you
guys?

  					matz.

i’m not attached to ‘of’ - all i was suggesting was a collecting
Numeric#times. i actually considered this first:

~ > cat a.rb
class Numeric
def inject accum =
times{|i| accum = yield(accum, i)}
accum
end
end

stacks = 3.inject{ }
p stacks # [,,]

hash = 16.inject({}){|h,i| h[i] = i; h}
p hash[4] # => 4

but this is even less intuitive, whereas the ‘of’ why kindof reads like ‘give
me n of these’. sorry i can’t think of a better non-english-centric name :wink:

-a

···

On Tue, 25 May 2004, Yukihiro Matsumoto wrote:

In message “[RCR] Numeric#of” > on 04/05/25, “Ara.T.Howard” ahoward@noaa.gov writes:

===============================================================================
EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
URL :: Solar-Terrestrial Physics Data | NCEI
“640K ought to be enough for anybody.” - Bill Gates, 1981
===============================================================================

I don’t like it in the case where the block actually uses the index -
i.e. 3.of { Array.new } seems fine, but 3.of {|i| i+1} doesn’t. I can’t
think of a better name either, though.

(And it seems worth reiterating my idea of a collecting block syntax -
3.times [|i| i+1] would be nicely orthogonal to the ‘times’ iterator)

martin

···

Yukihiro Matsumoto matz@ruby-lang.org wrote:

Besides that Numeric#of is not very intuitive for me (non native
English speaker). It seems too vague for me. Is the name “of” really
OK for you guys?

Hi –

matz@ruby-lang.org (Yukihiro Matsumoto) writes:

Hi,

this got a generally favourable reception so:

RCR - Numeric#of - an accumlative version of Numeric#times

What if n is very large number?

Besides that Numeric#of is not very intuitive for me (non native
English speaker). It seems too vague for me. Is the name “of” really
OK for you guys?

My problem with it (the word, rather than the idea) is that it doesn’t
make sense to me as a message being sent to an integer. It reads more
like a keyword construct.

A couple of ideas:

  1. Integer#collect # maybe too vague
  2. Integer#times! # ! as a warning that results are accumulating

David

···

In message “[RCR] Numeric#of” > on 04/05/25, “Ara.T.Howard” ahoward@noaa.gov writes:


David A. Black
dblack@wobblini.net

Hi,

Besides that Numeric#of is not very intuitive for me (non native
English speaker). It seems too vague for me. Is the name “of” really
OK for you guys?
matz.

Isn’t “3.of { Array.new }” the same as
“Array.new(3) { Array.new }”?
IMHO the last one is more clear because it describes
better what you are doing
(creating an Array with three arrays as elements)

Regards,
Kristof

···

On Tue, 25 May 2004 12:57:59 +0900, Yukihiro Matsumoto wrote:

Kent Dahl wrote:

It also looks awfully terse. How about:

them = 3.times_collect{ Array.new }

I think it reads better. “Three times, collect [result of] Array.new”.

What about

them = 3.instances_of { Array.new }

It’s certainly more verbose than “of”, but I think it reads well and the
user would understand precisely what to expect as the result.

Hi,

What if n is very large number?

same problem with using #map vs. #map! isn’t it?

Yes, but they are for Enumerables, collecting items for number of
times can be abused more easily. Just my concern.

i’m not attached to ‘of’ - all i was suggesting was a collecting
Numeric#times. i actually considered this first:

but this is even less intuitive, whereas the ‘of’ why kindof reads like ‘give
me n of these’. sorry i can’t think of a better non-english-centric name :wink:

You don’t have to care about being “non-english-centric”, but I just
worry that the word “of” can mean virtually anything.

						matz.
···

In message “Re: [RCR] Numeric#of” on 04/05/25, “Ara.T.Howard” ahoward@noaa.gov writes:

Don’t method names ending in ! imply that the receiver is being
modified.
(Do any methods exist in the core/standard which are so named, but
which do not modify the receiver?)

It would be foolish to think that
3.times! { … }
could modify the integer 3, but that’s what the second suggestion says
to me (despite being a nice attempt at an end-run around the
performance problem of accumulating while only wanting to iterate).

···

On May 25, 2004, at 5:08 AM, David Alan Black wrote:

A couple of ideas:

  1. Integer#collect # maybe too vague
  2. Integer#times! # ! as a warning that results are accumulating


(-, /\ / / //

not if you don’t want instances, say:

them 3.instances_of {‘foo’}

···

il Wed, 26 May 2004 06:28:32 +0900, Harry Ohlsen harryo@zip.com.au ha scritto::

Kent Dahl wrote:

What about

them = 3.instances_of { Array.new }

It’s certainly more verbose than “of”, but I think it reads well and the
user would understand precisely what to expect as the result.

should’nt this be Kernel::collect?
Anyway, I’d prefer to see Object#* :slight_smile:
them = Array.new * 3 # actually dup/clone

···

il Tue, 25 May 2004 15:37:04 -0600, “Ara.T.Howard” ahoward@noaa.gov ha scritto::

how about this?

Object#collect

them = collect(3){ Array.new }

Yukihiro Matsumoto wrote:

Hi,

What if n is very large number?

same problem with using #map vs. #map! isn’t it?

Yes, but they are for Enumerables, collecting items for number of
times can be abused more easily. Just my concern.

i’m not attached to ‘of’ - all i was suggesting was a collecting
Numeric#times. i actually considered this first:

but this is even less intuitive, whereas the ‘of’ why kindof reads like ‘give
me n of these’. sorry i can’t think of a better non-english-centric name :wink:

You don’t have to care about being “non-english-centric”, but I just
worry that the word “of” can mean virtually anything.

I understand the idiom, but I agree that it is a little vague.

What if we allow Integer#* to take a block?

a,b,c,d,e = 5 * { Array.new }

Hal

···

In message “Re: [RCR] Numeric#of” > on 04/05/25, “Ara.T.Howard” ahoward@noaa.gov writes:

(Do any methods exist in the core/standard which are so named, but
which do not modify the receiver?)

svg% ri Kernel#exit!
----------------------------------------------------------- Kernel#exit!
     Process.exit!(fixnum=-1)

···

------------------------------------------------------------------------
     Exits the process immediately. No exit handlers are run. _fixnum_
     is returned to the underlying system as the exit status.

        Process.exit!(0)

svg%

Guy Decoux

Gavin Kistner wrote:

A couple of ideas:

  1. Integer#collect # maybe too vague
  2. Integer#times! # ! as a warning that results are accumulating

Don’t method names ending in ! imply that the receiver is being modified.
(Do any methods exist in the core/standard which are so named, but which
do not modify the receiver?)

There is at least exit! and I think one or two others.

Matz has said that the ! signifies danger in general.

It would be foolish to think that
3.times! { … }
could modify the integer 3, but that’s what the second suggestion says
to me (despite being a nice attempt at an end-run around the performance
problem of accumulating while only wanting to iterate).

It’s starting to become unreadable IMO.

Hal

···

On May 25, 2004, at 5:08 AM, David Alan Black wrote:

Hi –

Gavin Kistner gavin@refinery.com writes:

A couple of ideas:

  1. Integer#collect # maybe too vague
  2. Integer#times! # ! as a warning that results are accumulating

Don’t method names ending in ! imply that the receiver is being
modified.

I put the little note over on the right in the hope of pre-answering
this :slight_smile:

! in a method names alerts you to danger (which often, but not always,
involves a modification to the receiver). I thought maybe the
accumulation of results would be alert-worthy, since you’d want to
avoid it, for performance reasons, unless you were sure that’s what
you wanted.

David

···

On May 25, 2004, at 5:08 AM, David Alan Black wrote:


David A. Black
dblack@wobblini.net

gabriele renzi wrote:

not if you don’t want instances, say:

them 3.instances_of {‘foo’}

Good point!

I was thinking that the point of the method was to get unique objects, but of course it could be used in this way.

gabriele renzi wrote:

···

il Wed, 26 May 2004 06:28:32 +0900, Harry Ohlsen harryo@zip.com.au

them = 3.instances_of { Array.new }

not if you don’t want instances, say:

them 3.instances_of {‘foo’}

Couldn’t agree more. I also think it is visually too similar to
instance_of?, making it harder to have a quick look at code.


([ Kent Dahl ]/)_ ~ [ Kent Dahl - Kent Dahl ]/~
))_student_/(( _d L b_/ Master of Science in Technology )
( __õ|õ// ) ) Industrial economics and technology management (
_
/ö____/ (_engineering.discipline=Computer::Technology)

gabriele renzi surrender_it@remove.yahoo.it wrote in message news:1aulb0pj5pk1b5hup1otnt70plqn4dnifh@4ax.com

how about this?

Object#collect

them = collect(3){ Array.new }

should’nt this be Kernel::collect?
Anyway, I’d prefer to see Object#* :slight_smile:
them = Array.new * 3 # actually dup/clone

I had one suggestion which I think was lost in the recent gateway
debug process… I’ll repeat it again below - hopefully it wasn’t a
simply bad idea :slight_smile:

To me, creating multiple instances of initialized objects feels like
it belongs more with the new call, rather than Integer. Perhaps we
could add a “multiple instance” new? Here is one implementation.

def Object.newx( nx, *args)
instances =
if( block_given? )
nx.times { instances.push yield(self.new(*args)) }
else
nx.times { instances.push self.new(*args) }
end
instances
end

a,b,c = Array.newx(3)

I’m not sure what should be done when the new also expects a block.

···

il Tue, 25 May 2004 15:37:04 -0600, “Ara.T.Howard” ahoward@noaa.gov > ha scritto::