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
===============================================================================
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)
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.
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
-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)
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:
Integer#collect # maybe too vague
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:
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:
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:
Integer#collect # maybe too vague
Integer#times! # ! as a warning that results are accumulating
(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.
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:
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
! 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:
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)
should’nt this be Kernel::collect?
Anyway, I’d prefer to see Object#*
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
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.