[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

neat idea from arc

Martin DeMello

1/29/2008 10:08:00 PM

Paul Graham just released Arc (http://paulgraham.com...), 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

7 Answers

Robert Klemme

1/29/2008 10:27:00 PM

0

On 29.01.2008 23:07, Martin DeMello wrote:
> Paul Graham just released Arc (http://paulgraham.com...), 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

Marcel Molina Jr.

1/29/2008 10:40:00 PM

0



On Jan 29, 2008, at 4:07 PM, "Martin DeMello"
<martindemello@gmail.com> wrote:

> Paul Graham just released Arc (http://paulgraham.com...), 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]

MenTaLguY

1/29/2008 10:47:00 PM

0

On Wed, 30 Jan 2008 07:40:19 +0900, "Marcel Molina Jr." <marcel@vernix.org> wrote:
> You can do that with grep.
>
> [1, :foo].grep Fixnum
> # => [1]

I suppose the reverse of this is a use case for having a Case::Not[] in my case gem.

-mental


Daniel DeLorme

1/30/2008 1:06:00 AM

0

Marcel Molina Jr. wrote:
> On Jan 29, 2008, at 4:07 PM, "Martin DeMello" <martindemello@gmail.com>
>> 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. Edward (Ed) Borasky

1/30/2008 3:52:00 AM

0

Martin DeMello wrote:
> Paul Graham just released Arc (http://paulgraham.com...), 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. :)

Martin DeMello

1/30/2008 6:10:00 AM

0

On Jan 30, 2008 3:52 AM, M. Edward (Ed) Borasky <znmeb@cesmail.net> wrote:
>
> 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

ThoML

1/30/2008 8:03:00 AM

0

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