Arc Forumnew | comments | leaders | submitlogin
Suggestion for [... _ ...]
5 points by projectileboy 6170 days ago | 7 comments
I really like the syntax for anonymous single-arg functions. But it'd be nice if I could use it within def. When looking through the source, my eye looks for the "def"s, so it feels odd to me that I can write this:

(= add-1 [+ 1 _])

...but not this:

(def add-1 [+ 1 _])

If you allowed this, and generalized, you could tighten up certain function defs that didn't require formal parameter lists. Something like:

(def add (x y) (+ x y))

becomes:

(def add [+ $0 $1])

(OK, dumb example, but you get the idea.) The only thing I don't like about this is that it starts to feel like we're walking down the Perl path: "Hey, we provide a dozen ways to do the same thing, and so you can't possibly hope to read anyone else's source!" Additionally, I very much like the "fill-in-the-blank underscore".



8 points by pg 6170 days ago | link

I think the right answer is to use = in this case. Def is an abbreviation for =. If you can just say =, you don't need def.

-----

2 points by partdavid 6169 days ago | link

The second def to me implies that add-1 returns a function, not a value.

Since arc supports subscripting data structures using a list (I'm sorry if that's a terrible way to say it--I'm not a lisper), the last expression can be non-uglified:

   (def add2 [+ (_ 0) (_ 1)])
So [ ] is an anonymous single-argument function, and when more than one argument is passed to the function, a list is passed.

How would that grab you?

-----

1 point by projectileboy 6169 days ago | link

Actually, forget everything I said - I agree with your first sentence. I had originally thought there might be an elegant way to ditch formal parameter lists when there aren't optional or rest parameters. But the cost seems too high for the value added.

-----

2 points by mcoles 6169 days ago | link

It's totally possible that I'm missing the point, but how is _ 0 and _ 1 and better than x and y? Sure this gives you an implicit parameter list, but when you go beyond short functions like add2, wouldn't it be nice to have names for variables? My understanding of [ _ ] was for all those quick, inline anonymous functions, not for function definition of non-trivial functions.

-----

2 points by sjs 6170 days ago | link

This is already being discussed in a few threads.

http://arclanguage.org/item?id=1227

http://arclanguage.org/item?id=1329

-----

1 point by mdemare 6169 days ago | link

What about [... * ...] where all 0 .. N arguments are collected in a list called *?

-----

1 point by bogomipz 6169 days ago | link

Except you're shadowing the multiply operator, so I think you need to pick a different name.

The idea is nice though. You should even be able to mix it with the other implicit arguments;

  [prn "the first two args were " $1 " and " $2 ", and the rest were " $*]

-----