neat idea from arc

M

Martin DeMello

Paul Graham just released Arc (http://paulgraham.com/arc0.html), a
lisp dialect with an emphasis on "quick-and-dirty" exploratory
programming, and compactness of code. One of the ideas he threw in
seems like it might fit well into Ruby too - when filtering functions
like keep and rem (select and reject) are given a value rather than a
function, they implicitly construct the equality function and pass
that in instead. The ruby equivalent would be to have select, reject,
partition, any? and all? (and possibly some others I'm overlooking) do
the following:

def select(arg, &blk)
if block_given?
old_select(&blk)
else
old_select {|x| arg === x}
end
end

and likewise for the others

So we can write, for instance

ints = input.select Integer

lcase = words.reject /[A-Z]/

and so on. What say you?

martin
 
R

Robert Klemme

Paul Graham just released Arc (http://paulgraham.com/arc0.html), a
lisp dialect with an emphasis on "quick-and-dirty" exploratory
programming, and compactness of code. One of the ideas he threw in
seems like it might fit well into Ruby too - when filtering functions
like keep and rem (select and reject) are given a value rather than a
function, they implicitly construct the equality function and pass
that in instead. The ruby equivalent would be to have select, reject,
partition, any? and all? (and possibly some others I'm overlooking) do
the following:

def select(arg, &blk)
if block_given?
old_select(&blk)
else
old_select {|x| arg === x}
end
end

You either need "*arg" or "arg = nil" because otherwise the method
cannot be invoked without arg. Here's another way to write it:

module Enumerable
def select2(a = nil, &b)
select(&(b || lambda {|e| a === e}))
end
end

but I guess your variant is more efficient.
and likewise for the others

So we can write, for instance

ints = input.select Integer

lcase = words.reject /[A-Z]/

and so on. What say you?

Sounds good! Btw, there is already #grep which works like the new
#select with argument but I agree, the idea to put this in one method
sounds better.

Kind regards

robert
 
M

Marcel Molina Jr.

Paul Graham just released Arc (http://paulgraham.com/arc0.html), a
lisp dialect with an emphasis on "quick-and-dirty" exploratory
programming, and compactness of code. One of the ideas he threw in
seems like it might fit well into Ruby too - when filtering functions
like keep and rem (select and reject) are given a value rather than a
function, they implicitly construct the equality function and pass
that in instead. The ruby equivalent would be to have select, reject,
partition, any? and all? (and possibly some others I'm overlooking) do
the following:

def select(arg, &blk)
if block_given?
old_select(&blk)
else
old_select {|x| arg === x}
end
end

and likewise for the others

So we can write, for instance

ints = input.select Integer

lcase = words.reject /[A-Z]/

and so on. What say you?

martin

You can do that with grep.

[1, :foo].grep Fixnum
# => [1]
 
D

Daniel DeLorme

Marcel said:
So we can write, for instance

ints = input.select Integer

You can do that with grep.

[1, :foo].grep Fixnum
# => [1]

Oh wow, I never knew you could do that. That means we can also do:
[2,4,6,7].grep(1..5)
=> [2, 4]

But still, I think the point of the OP was that this idiom should also
be available for the other methods (reject, partition, any?, all? ...)

Daniel
 
M

M. Edward (Ed) Borasky

Martin said:
Paul Graham just released Arc (http://paulgraham.com/arc0.html), a
lisp dialect with an emphasis on "quick-and-dirty" exploratory
programming, and compactness of code. One of the ideas he threw in
seems like it might fit well into Ruby too - when filtering functions
like keep and rem (select and reject) are given a value rather than a
function, they implicitly construct the equality function and pass
that in instead. The ruby equivalent would be to have select, reject,
partition, any? and all? (and possibly some others I'm overlooking) do
the following:

[snip]

Well ... not sure about this relative to Ruby, but I must admit I'm a
bit disappointed in Arc itself. Sure, it's a "dialect of Lisp", but does
it differ *enough* from Scheme or Common Lisp in any *practical* way?

I think I'll stick with Ruby. :)
 
M

Martin DeMello

Hi,

In message "Re: neat idea from arc"

|So we can write, for instance
|
|ints = input.select Integer

We can do it by using #grep, e.g.

ints = input.select Integer

I was thinking of this as a replacement for (and generalisation of)
grep, since it would extend to other methods like #reject and
#partition too

martin
 
M

Martin DeMello

Well ... not sure about this relative to Ruby, but I must admit I'm a
bit disappointed in Arc itself. Sure, it's a "dialect of Lisp", but does
it differ *enough* from Scheme or Common Lisp in any *practical* way?

I think I'll stick with Ruby. :)

Same here. I was excited, but it doesn't look all that special. Qi has
a lot more promise that way.

martin
 
T

tho_mica_l

Well ... not sure about this relative to Ruby, but I must admit I'm a
bit disappointed in Arc itself. Sure, it's a "dialect of Lisp", but does
it differ *enough* from Scheme or Common Lisp in any *practical* way?

It seems rather like a Scheme pre-compiler to handle some syntaxtic
sugar (e.g. for function composition etc.) that couldn't be done with
macros alone. Strange ... after all these years.
 
P

Pit Capitain

2008/1/30 said:
I was thinking of this as a replacement for (and generalisation of)
grep, since it would extend to other methods like #reject and
#partition too

Martin, you can define

class Object
def to_proc
lambda {|e| self === e }
end
end

and then

[3,1,4,1,5].all?(&(1..5))) # => true

Regards,
Pit
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top