[RCR] Numeric#of

A

Ara.T.Howard

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

Matz has said that the ! signifies danger in general.


It's starting to become unreadable IMO.


Hal

i must say that idea of a collecting block syntax is a nice one...

this conversation has basically went the same way my own thought process went
before submitting the RCR. 'of' might not be the _greatest_ name, but then
again neither is 'map' (unless you have programmed perl). my point is this:
'of' is nice and short and mnemonic _enough_, at least as mnemonic as
Array#map isn't it?

after you'd seen

a, b, c = 3.of { MyClass.new }

or even

first, second, third, fourth = 4.of {|which| MyClass.new which }

once, maybe thrice, in some sources - wouldn't it be clear enough?

-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 :: http://www.ngdc.noaa.gov/stp/
| "640K ought to be enough for anybody." - Bill Gates, 1981
===============================================================================
 
J

Jason Creighton

I'd rather keep Integer#times as it is, and use the
a, b, c = (1..3).map{[]}
idiom; I don't think the use case deserves polluting the language (in the
case of 'of', by being too generic) or a potentially big performance hit,
for a minimal gain.

I certainly agree that that idiom is clear enough and we don't need to
address the "problem" at hand. And your notes about performance are
well made.

I guess something like this would qualify for 'extensions' if there
was enough interest and a really good name. Perhaps n.map { ... } as
a shortcut for (0...n).map { ... } is suitable, but I've realised by
now that my intuition is not often widely shared :)

If you were going to do that....
class Integer
alias each times
include Enumerable
end => Integer
5.map { [] } => [[], [], [], [], []]
5.select { |n| n % 2 == 0 } => [0, 2, 4]

Jason Creighton
 
S

Simon Strandgaard

Ara.T.Howardwrote:
[snip]
after you'd seen

a, b, c = 3.of { MyClass.new }

or even

first, second, third, fourth = 4.of {|which| MyClass.new which }

once, maybe thrice, in some sources - wouldn't it be clear enough?


Your 'of' is a mix between times and map. Wouldn't it be better to
name it 'times_map' ?

a, b, c = 3.times_map {|n| -n}

# a=0, b=-1, c=-2
 
S

Simon Strandgaard

Simon Strandgaard said:
Your 'of' is a mix between times and map. Wouldn't it be better to
name it 'times_map' ?

Even simpler.. it would make sense to me if it were just named 'maps'

a, b, c = 3.maps {|n| -n}
# a=0, b=-1, c=-2
 
H

Harry Ohlsen

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.
 
G

gabriele renzi

i must say that idea of a collecting block syntax is a nice one...

please, no more syntax :)
I believe that in the case a collecting/non collecting dychotomy
should exist it should be something at the convention level, like !
for dangerous methods
this conversation has basically went the same way my own thought process went
before submitting the RCR. 'of' might not be the _greatest_ name, but then
again neither is 'map' (unless you have programmed perl).

I believe map() was really common long time before Larry wrote his
first language, and it is very common in many languages.
not that this makes it obvious to anyone, anyway.

my point is this:
'of' is nice and short and mnemonic _enough_, at least as mnemonic as
Array#map isn't it?

after you'd seen

a, b, c = 3.of { MyClass.new }

or even

first, second, third, fourth = 4.of {|which| MyClass.new which }

once, maybe thrice, in some sources - wouldn't it be clear enough?

agreed
 
G

gabriele renzi

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.

not if you don't want instances, say:

them 3.instances_of {'foo'}
 
M

Martin DeMello

Simon Strandgaard said:
Even simpler.. it would make sense to me if it were just named 'maps'

a, b, c = 3.maps {|n| -n}
# a=0, b=-1, c=-2

Or even just map (there's no Numeric#map) - I don't like that either
[ruby-talk:62177] but I can't think of anything I like better.

martin
 
S

Simon Strandgaard

Martin DeMello said:
Simon Strandgaard said:
Even simpler.. it would make sense to me if it were just named 'maps'

a, b, c = 3.maps {|n| -n}
# a=0, b=-1, c=-2

Or even just map (there's no Numeric#map) - I don't like that either
[ruby-talk:62177] but I can't think of anything I like better.

Agree, Numeric.map would be perfect.

irb(main):001:0> (2..4).map{|i| -i}
=> [-2, -3, -4]
irb(main):002:0> 5.map{|i| -i}
NoMethodError: undefined method `map' for 5:Fixnum
from (irb):2
irb(main):003:0>
 
K

Kent Dahl

gabriele said:
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.
 
G

gabriele renzi

il Wed, 26 May 2004 06:29:51 GMT, Martin DeMello
Even simpler.. it would make sense to me if it were just named 'maps'

a, b, c = 3.maps {|n| -n}
# a=0, b=-1, c=-2

Or even just map (there's no Numeric#map) - I don't like that either
[ruby-talk:62177] but I can't think of anything I like better.

I believe it is Integer#map more than Numeric. What whould you expect
from (3/Complex(1,i)).map {|x| -x} ? :)
 
S

Simon Strandgaard

gabriele renzi said:
il Wed, 26 May 2004 06:29:51 GMT, Martin DeMello
Even simpler.. it would make sense to me if it were just named 'maps'

a, b, c = 3.maps {|n| -n}
# a=0, b=-1, c=-2

Or even just map (there's no Numeric#map) - I don't like that either
[ruby-talk:62177] but I can't think of anything I like better.

I believe it is Integer#map more than Numeric. What whould you expect
from (3/Complex(1,i)).map {|x| -x} ? :)

It depends on 2 things:

A) what the default element are

B) what the Range default_element..specified_element yields


Maybe it would be a good idea to extend all Numeric classes
with a null_element. ala traits

p 9.null_element # -> 0
p Complex(123, 456).null_element # -> 42

when one does "x.map{|y| y.z}" then map makes a range between
the null_element and x.
 
G

gabriele renzi

il Wed, 26 May 2004 18:55:38 +0900, Simon Strandgaard
Maybe it would be a good idea to extend all Numeric classes
with a null_element. ala traits

p 9.null_element # -> 0
p Complex(123, 456).null_element # -> 42

when one does "x.map{|y| y.z}" then map makes a range between
the null_element and x.

well, I'd just prefer to see it working like times(().
I mean, walk in a range 1..n, just for integers. But it's my opinion.
 
S

Simon Strandgaard

gabriele renzi said:
well, I'd just prefer to see it working like times(().
I mean, walk in a range 1..n, just for integers. But it's my opinion.

Times walks the range 0..(n-1) :)

It was just a silly idea.. I have plenty of weird ideas today (its my birthday).

Just ask if you want more silly ideas (only today ;-)
 
G

gabriele renzi

il Wed, 26 May 2004 19:28:31 +0900, Simon Strandgaard
Times walks the range 0..(n-1) :)
oh sure, I meant 0...n (I hate this :)

It was just a silly idea.. I have plenty of weird ideas today (its my birthday).

tanti auguri! (happy birthday!)
 
K

Kristof Bastiaensen

On Wed, 26 May 2004 17:07:11 +0900, Simon Strandgaard wrote:

Hi,
Martin DeMello said:
Simon Strandgaard said:
[snip]
Your 'of' is a mix between times and map. Wouldn't it be better to
name it 'times_map' ?


Even simpler.. it would make sense to me if it were just named 'maps'

a, b, c = 3.maps {|n| -n}
# a=0, b=-1, c=-2

Or even just map (there's no Numeric#map) - I don't like that either
[ruby-talk:62177] but I can't think of anything I like better.

Agree, Numeric.map would be perfect.

irb(main):001:0> (2..4).map{|i| -i}
=> [-2, -3, -4]
irb(main):002:0> 5.map{|i| -i}
NoMethodError: undefined method `map' for 5:Fixnum
from (irb):2
irb(main):003:0>

I feel that an integer represents only that integer,
not a range, I mean, that's where ranges are for, isn't it?
More logical would be 4.map{ |i| -i } to return [-4].
Times_map I find more accurate, but is it necessary?
What's wrong with Array.new(3){ |i| -i } or
(0..2).map{ |i| -i } ?
For small values it is even faster to type
a, b, c = 0, -1, -2
and more clear IMO than
a, b, c = 3.map { |i| -i }

Cheers,
Kristof

PS. I wonder if my messages make it to the newsgroup,
I can see only half of the postings people send...
 
A

Alan Chen

Ara.T.Howard said:
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)
 
M

Martin DeMello

gabriele renzi said:
please, no more syntax :)

What do you have against syntax? :)
I believe that in the case a collecting/non collecting dychotomy
should exist it should be something at the convention level, like !
for dangerous methods

Add % to the list of acceptable method name suffixes? (I'd say we've
pretty thoroughly colonised the keyboard by now - good free symbols are
hard to find.)

martin
 
G

gabriele renzi

il Thu, 27 May 2004 05:23:12 GMT, Martin DeMello

Well, I'd love ruby to stay simple. I have this ugly feeling of ruby
becoming perlish in the bad way, i.e. adding new syntax, new
mistetyous operators and so on. Just think of how many new things
we'll see in Ruby2.. Key:val literals, val:default arguments,
@_variables..

And, in the end, everybody is going to hate syntax (sooner or later),
that's why people gets happy with LISP (sooner or later ;)
What do you have against syntax? :)


Add % to the list of acceptable method name suffixes? (I'd say we've
pretty thoroughly colonised the keyboard by now - good free symbols are
hard to find.)

not necessarily a new symbol (I'm against stuff that is not obvious).
But something like: collecting_times/times colllecting_each/each and
so on, would be nice.
Surely not 'collecting' but something on the line. Now I just need to
find a good name, I'm sure I can get it in the next few decades..
 
K

Kristof Bastiaensen

On Thu, 27 May 2004 05:23:12 +0000, Martin DeMello wrote:

Hi,
What do you have against syntax? :)

I think it will not work with this syntax.
What means the following:
mymethod [ a ]
call mymethod, and collect a for each iteration, or call mymethod
with an array?

to_enum.to_a works with the current syntax:
(2..10).to_enum:)step, 3).to_a
=> [2, 5, 8]
Add % to the list of acceptable method name suffixes? (I'd say we've
pretty thoroughly colonised the keyboard by now - good free symbols are
hard to find.)

That's true. I think every common symbol is used.
Fortunately they are arranged so as not to make ugly combinations.
Imagine having to type $_->$*@ or something.
Adding new syntax in an elegant way is not so easy...

Cheers,
Kristof
 

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

Similar Threads

Numeric#of 2
stable snapshot 4
Object#copy [rcr?] 4
[RCR] Kernel#hostname 0
parent of TrueClass, FalseClass 9
RMagick jp2 problem 8
RCR - 'struct flock*' wrapper for rb_io_fcntl 7
narray on windows? 1

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top