An idea regarding map

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

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.

···

On May 23, 2004, at 10:33 PM, Ryan Paul wrote:

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

SegPhault wrote:

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

Hi –

···

On Mon, 24 May 2004, Ryan Paul wrote:

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
(RCRS)

David


David A. Black
dblack@wobblini.net

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

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

Ryan Paul wrote:

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. :slight_smile:

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

Ryan Paul segphault@sbcglobal.net writes:

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

···


mike@stok.co.uk | The “`Stok’ disclaimers” apply.
http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
> Fingerprint 0570 71CD 6790 7C28 3D60
> 75D2 9EC4 C1C0 0599 13DA

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

···

On Tue, 25 May 2004 02:12:15 +0900, Hal Fulton wrote:

Ryan Paul wrote:

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. :slight_smile:

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

“Mike Stok” mike@ratdog.stok.co.uk schrieb im Newsbeitrag
news:m2k6yxgzwt.fsf@ratdog.stok.co.uk

Ryan Paul segphault@sbcglobal.net writes:

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

There’s already an RCR:
http://rcrchive.net/rcr/RCR/RCR241

robert