Neat idea from arc

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

Hi,

In message “Re: neat idea from arc”
on Wed, 30 Jan 2008 07:07:31 +0900, “Martin DeMello”
[email protected] writes:

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

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

ints = input.select Integer

          matz.

On 29.01.2008 23:07, Martin DeMello wrote:

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

On Wed, 30 Jan 2008 07:40:19 +0900, “Marcel Molina Jr.”
[email protected] 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

On Jan 29, 2008, at 4:07 PM, “Martin DeMello”
[email protected] wrote:

def select(arg, &blk)

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]

Marcel Molina Jr. wrote:

On Jan 29, 2008, at 4:07 PM, “Martin DeMello” [email protected]

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

Martin DeMello wrote:

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

On Jan 30, 2008 3:52 AM, M. Edward (Ed) Borasky [email protected]
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. :slight_smile:

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

martin

On Jan 29, 2008 10:24 PM, Yukihiro M. [email protected] wrote:

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

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.

2008/1/30, Martin DeMello [email protected]:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs