an idea regarding map

R

Ryan Paul

just some syntactic nitpicking:

If I want to perform a map operation using a function...

def somefunc(x)
# ... perform some operation on x ...
# ... and return a value ...
end

I have to do this:

l = [1,2,3].map { |x| somefunc x }

Why shouldnt I be able to do this:

l = [1,2,3].map somefunc

--SegPhault
 
K

Kent Sibilev

Consider this:

$ irb
irb(main):001:0> def doubler(x); x * 2 end
=> nil
irb(main):002:0> (1..3).map &method:)doubler)
=> [2, 4, 6]
irb(main):003:0>

Cheers,
Kent.
 
G

Gavin Sinclair

SegPhault said:
just some syntactic nitpicking:

If I want to perform a map operation using a function...

def somefunc(x)
# ... perform some operation on x ...
# ... and return a value ...
end

I have to do this:

l = [1,2,3].map { |x| somefunc x }

Why shouldn't I be able to do this:

l = [1,2,3].map somefunc

It's just not how Ruby works, for a number of reasons. If 'somefunc' were
truly a function, then it would seem strange not to be able to do that.
But 'somefunc' is a method, and as such it is bound up in the state of
some object.

Ruby will not sacrifice the purity of its OO concept for a bit of extra
convenience. (It already has convenience in spades.)

The 'extensions' project on RubyForge has a couple of shortcuts aimed at
map, but not in the (function-oriented) way you want.

require 'extensions'

a = [1, -2, 3]
a.mapf :abs # short for a.map { |n| n.abs }
a.map &:abs # ditto

The second example is a bit ugly and opaque, but it can be applied to any
method, whereas the first is obviously limited to map/collect.

An alternative for your code is

a.map method:)somefunc)

Ultimately, you have to look at your suggested code

a.map somefunc

and realise that 'somefunc' must be either a local variable or a method
invocation. Treating it as a method object (like method:)somefunc)) would
mean that all method invocations would require parentheses, which would be
unpopular.

Cheers,
Gavin
 
D

David A. Black

Hi --

just some syntactic nitpicking:

If I want to perform a map operation using a function...

def somefunc(x)
# ... perform some operation on x ...
# ... and return a value ...
end

I have to do this:

l = [1,2,3].map { |x| somefunc x }

Why shouldnt I be able to do this:

l = [1,2,3].map somefunc

Assuming you mean:

[1,2,3].map:)somefunc) # it would have to be a symbol or string

it's been proposed as an RCR and rejected
(http://www.rcrchive.net/rgarchive/rejected.html#rcr50)


David
 
R

Ryan Paul

I appreciate the good responses I get here. Another syntactic question:

why do we not have an 'in' operator like python does?
 
H

Hal Fulton

Ryan said:
I appreciate the good responses I get here. Another syntactic question:

why do we not have an 'in' operator like python does?

David Black is going to think I paid you to ask that. :)

I created RCR #241 for this, as I've been wanting it for over three
years. See http://rcrchive.net

I believe it could be added to the language easily and would represent
a minor improvement (which those who don't like need not use).

Interestingly, there seems to be a wider spread of opinion on this
RCR than any other I have noticed. Some love it, some hate it, some
are neutral.


Cheers,
Hal
 
R

Ryan Paul

David Black is going to think I paid you to ask that. :)

I created RCR #241 for this, as I've been wanting it for over three
years. See http://rcrchive.net

I believe it could be added to the language easily and would represent
a minor improvement (which those who don't like need not use).

Interestingly, there seems to be a wider spread of opinion on this
RCR than any other I have noticed. Some love it, some hate it, some
are neutral.


Cheers,
Hal

I gave it my vote. It's clear and its intuitive, whats not to love? I'm
surprised that it hasnt been implemented yet!
 
M

Mike Stok

Ryan Paul said:
I appreciate the good responses I get here. Another syntactic question:

why do we not have an 'in' operator like python does?

Would include? do what you want? e.g.

if legal_values.include? input_value then
...
end

Mike
 

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

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top