Symbol#to_proc is just so beautiful


#1

When is this ever getting into Ruby Core?

class Symbol
def to_proc
proc{|obj| obj.send(self) }
end
end

Consider this:

class Numeric
def positive?
self > 0
end
end

[1, 2, 3].all? &:positive? => true
[-1, 2, 3].all? &:positive? => false

It’s just so damn beautiful!

Daniel


#2

Daniel S. schrieb:

class Numeric
def positive?
self > 0
end
end

[1, 2, 3].all? &:positive? => true
[-1, 2, 3].all? &:positive? => false

It’s just so damn beautiful!

If you use this definition instead:

class Symbol
def to_proc()
lambda { |obj, *args| obj.send(self, *args) }
end
end

You can also do this:

(1 … 5).inject(&:+) # => 15
(1 … 5).inject(&:*) # => 120


#3

Florian Groß wrote:

If you use this definition instead:

class Symbol
def to_proc()
lambda { |obj, *args| obj.send(self, *args) }
end
end

You can also do this:

(1 … 5).inject(&:+) # => 15
(1 … 5).inject(&:*) # => 120

Sweeeeet! Oh man, I love this language!


#4

On 4/19/06, Daniel S. removed_email_address@domain.invalid wrote:

[2, 3].inject(&:*) => 6

Is it just me, or does that look like an ASCII singing Elvis? Maybe
that’s how he died, injecting to many [2, 3] shots…

Jacob F.


#5

Florian Groß wrote:

(1 … 5).inject(&:+) # => 15
(1 … 5).inject(&:*) # => 120

This has got to be the most obfuscated way to write 2 * 3:

[2, 3].inject(&:*) => 6

Daniel


#6

John J. wrote:

It’s a lot more legible than getting into all the Snoopy talk (@#$%).

This is way OT, but I can’t actually remember Snoopy ever swearing. Yet
it gets mentioned frequently (even in the pick-axe). Has the ruby
community collectively hallucinated it? Or did ruby programmers grow up
in a parallel universe with R rated daily comics?


#7

On 19-Apr-2006, at 15:03, Florian Groß wrote:

end
end

You can also do this:

(1 … 5).inject(&:+) # => 15
(1 … 5).inject(&:*) # => 120

IIIEEEeee, it’s Perl, it’s Perl! :slight_smile:

I don’t see anything wrong with:

[1, 2, 3].all? { |n| n > 0 }

It’s a lot more legible than getting into all the Snoopy talk (@#$%).

Regards,
JJ

Help everyone. If you can’t do that, then at least be nice.


#8

Wilson B. wrote:

Ah… the parallel universe of television :wink:

Thanks for clearing that up for me.


#9

John J. wrote:

IIIEEEeee, it’s Perl, it’s Perl! :slight_smile:

I don’t see anything wrong with:

[1, 2, 3].all? { |n| n > 0 }

It’s a lot more legible than getting into all the Snoopy talk (@#$%).

I agree that it’s fine, but:

a) It can be expressed with a shorter code snippet (less line noise),
therefore making it easy to do method-chaining.

b) I implicitly understand that it is an array of numbers, so the idea
of “a number” repeats itself three times (you had to write ‘n’ twice).

a better example might be:
all_people.select { |person| person.retired? }
The subject of this operation is expressed twice more than it ought to
be.

Therefore,

[1,2,3].all?(:>, 0)
and
all_people.select(:retired?)

is also, IMHO, a good choice.


#10

Perhaps instead of

all_people.select(&:retired?)

select could return a functor --in this case an enumerator:

all_people.select.retired?

And in fact I believe this functionality is in 1.9 already. It works
with all enumeratable methods and methods of classes that serve the
same purpose. I think it could be further generalized though for any
method accepting a block. For:

def foo(&blk)

then

foo.bar

translates into

foo { |x| x.bar }

(It may require the ability to sepcify manditory blocks though, like I
brought up in another recent thread)

T.


#11

removed_email_address@domain.invalid wrote:

def foo(&blk)

then

foo.bar

translates into

foo { |x| x.bar }

No one though this was interesing?

T.


#12

Hi,

In message “Re: Symbol#to_proc is just so beautiful”
on Sat, 1 Jul 2006 16:56:15 +0900, removed_email_address@domain.invalid writes:

|No one though this was interesing?

Interesting but too radical. I’m afraid that it’s too hard to imagine

|> foo { |x| x.bar }

when I see

|> foo.bar

						matz.

#13

Hi,

I don’t want to rain on anyone’s parade, but this technique is fairly
inefficient compared to using normal blocks.

Consider this:

class Symbol
def to_proc
proc{|obj| obj.send(self) }
end
end

class Numeric
def positive?
self > 0
end
end

def symbol_to_proc
(1…1000).all? &:positive?
end
def use_block
(1…1000).all? {|x| x.positive?}
end

require ‘benchmark’
include Benchmark

n = 1000
bm(20) {|x|
x.report(“symbol_to_proc”) { n.times { symbol_to_proc }}
x.report(“use_block”) { n.times { use_block }}
}
END
user system total real
symbol_to_proc 2.031000 0.000000 2.031000 ( 2.063000)
use_block 1.219000 0.000000 1.219000 ( 1.266000)

Of course, that may change if Symbol#to_proc makes it into core.

Regards,
Sean


#14

On 4/19/06, Joel VanderWerf removed_email_address@domain.invalid wrote:

John J. wrote:

It’s a lot more legible than getting into all the Snoopy talk (@#$%).

This is way OT, but I can’t actually remember Snoopy ever swearing. Yet
it gets mentioned frequently (even in the pick-axe). Has the ruby
community collectively hallucinated it? Or did ruby programmers grow up
in a parallel universe with R rated daily comics?

That’s how Snoopy’s speech was depicted in the cartoons (not the comic
strips).


#15

Hi –

On Fri, 30 Jun 2006, Omer Raviv wrote:

John J. wrote:

IIIEEEeee, it’s Perl, it’s Perl! :slight_smile:

I don’t see anything wrong with:

[1, 2, 3].all? { |n| n > 0 }

It’s a lot more legible than getting into all the Snoopy talk (@#$%).

I agree. I don’t see the problem with it.

I agree that it’s fine, but:

a) It can be expressed with a shorter code snippet (less line noise),
therefore making it easy to do method-chaining.

“&:>” and such, I would argue, are shorter but have more line noise
than the equivalent explicit blocks.

[1,2,3].all?(:>, 0)
and
all_people.select(:retired?)

I always think of these things partly in terms of explanation. In
order to explain all_people.select(:retired?), one would either have
to present :retired? as a “magic” way of filtering for retiredness, or
explain that it’s actually {|p| p.retired? } in disguise. Somehow it
doesn’t work as, so to speak, a primary, transparent technique.

“Why is there a colon?” Because it’s a symbol. “So a symbol invokes
the method of that name?” No.

And so on…

David


David A. Black (removed_email_address@domain.invalid)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

See what the readers are saying about “Ruby for Rails”!
http://www.rubypowerandlight.com/quotes


#16

Yukihiro M. wrote:

when I see

|> foo.bar

I can see that on first econounter the idea, certainly. And I think the
same is true when enumerable method returns an enumerator. It’s
something that requires getting usedto. I’m more accustom to it myself
becuase I have used “Functor pattern” enough. If I may suggest, at
least make a note to reconsider it in the future. I think as the
enumerator functionality becomes more common, this will not seem so
radical.

And actually if you look at it less abstract terms it doesn’t seem
quite as radical either:

[1,2,3].collect.to_s

[“A3”,“B2”,“C1”].sort_by[1,1]

‘pretty little ponies’.gsub(/[^aeiou]/).upcase

T.


#17

Hi,

In message “Re: Symbol#to_proc is just so beautiful”
on Sun, 2 Jul 2006 00:34:22 +0900, removed_email_address@domain.invalid writes:

|And actually if you look at it less abstract terms it doesn’t seem
|quite as radical either:
|
| [1,2,3].collect.to_s

This means ‘print([1,2,3].collect)’ would not work, since it calls
to_s for string conversion, but to_s gives it an array of strings.
In any case, I feel like it requires special notation (for partial
evaluation), even if we really need it.

						matz.

#18

Yukihiro M. wrote:

This means ‘print([1,2,3].collect)’ would not work, since it calls
to_s for string conversion, but to_s gives it an array of strings.

Why should it? If the block is manditory then it won’t work anyway.
This is what I was saying about this notation needing a way to specify
manditory blocks in order to work. It cannot apply to optional blocks.
(Which reminds me I got bit by optional blocks yesterday when I forgt
to put .each :/)

In any case, I feel like it requires special notation (for partial
evaluation), even if we really need it.

Well, “need” is pretty relative. It’s more a question of want I think.
How much do we want a consice notation like this? After all a special
notation might ruin it’s readiabilty, which is it’s main advantage.

Anyhow, it’s certainly not pressing. But I have a hunch the idiom will
gain more acceptance over time.

Thanks,
T.


#19

Hi –

On Sun, 2 Jul 2006, removed_email_address@domain.invalid wrote:

|> foo { |x| x.bar }

when I see

|> foo.bar

I can see that on first econounter the idea, certainly. And I think the
same is true when enumerable method returns an enumerator.

Definitely. I find this:

array.map.upcase

(if I’m getting the magic enumerator thing right) pretty obscure. I
mean, the words on the screen more or less describe what’s happening,
but the dot syntax feels to me like it’s being pushed beyond the
limits of usefulness.

David


David A. Black (removed_email_address@domain.invalid)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

See what the readers are saying about “Ruby for Rails”!
http://www.rubypowerandlight.com/quotes


#20

Yukihiro M. wrote:

“need” is pretty relative indeed. And I’m a big fan of concise
notation in general. But still I don’t think this is a good idea
(without special notation). Iterating method calls (like to_s in
above example), should be distinguishable from usual method calls,
because they are different, they act different, their implementations
are different.

Besides that you need to have knowledge of whether #collect requires
mandatory block or not to tell how obj.collect.to_s works. Note that
#collect may or may not have mandatory block depends on its receiver.

Okay. Though we should consider that it can already be done, and
similar code is already being done in limited cases. Just as an example
here’s how collect could be defined.

alias :old_collect, :collect
def collect( &yld )
if yld
old_collect( &yld )
else
Functor.new { |op, x| x.send(op) }
end
end

But even so, say a special notaion were the best option. What kind of
notations do we have left? Everything starts to seem so perlish. I
don’t know. Maybe ‘->’ could play a role here?

T.