Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I tried to evaluate a Clojure expression with nested shorthand functions today, and it wouldn't let me.

The expression was:

(#(+ % (#(+ % (* % %)) %)) 5) ; sorry for the eye bleed

The output was:

IllegalStateException Nested #()s are not allowed  clojure.lang.LispReader$FnReader.invoke (LispReader.java:630)
...and a bunch of other garbage
share|improve this question
2  
i find that not being able to write such code is a good thing for clojure. –  Simon Jan 25 '13 at 11:20
2  
Because it makes your eyes bleed. –  Michael Shaw Jan 25 '13 at 13:16

3 Answers 3

up vote 3 down vote accepted

You would know that % belongs to the inner function. The drawback is that you would lose access to the % in the outer function.

Use the fn [x] syntax instead.

share|improve this answer

You can have nested anonymous functions of the (fn [params] (body)) sort. Only the # syntax doesn't support nesting.

share|improve this answer

It's completely arbitrary; there's a couple lines in the parser that explicitly disable it. If you edit that line out, you can have nested anonymous functions, and they act exactly like you'd expect.

specifically, lines 634-635 in https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/LispReader.java

public static class FnReader extends AFn{
    public Object invoke(Object reader, Object lparen) {
        PushbackReader r = (PushbackReader) reader;
        if(ARG_ENV.deref() != null) // <-- line 634
            throw new IllegalStateException("Nested #()s are not allowed");
        // ...
share|improve this answer
    
Could you identify the line in the parser and demonstrate that the code rewritten to not have an anon nested function and the code with the line removed from the parser and an anon nested function works the same? –  MichaelT Jan 24 '13 at 22:09
2  
@MichaelT: there you go . and you can just test it; it's easy to do since you can switch them out at runtime . the clojure parser is really quite easily hackable –  sparkleshy Jan 25 '13 at 1:17
3  
Well, not completely arbitrary; unless Rick Hickey was just having an arbitrary day, there must have been a reason he put it in there, and you don't seem to know what that reason is. Duh. –  Robert Harvey Jan 25 '13 at 19:40
    
Wow, what a find! Nice -- +1. –  Matt Fenwick Jan 25 '13 at 20:05
    
Would this change cause any ambiguous parsings of nested methods? I am curious of a fn [x] rewrite of the OP's code would have identical functionality to a modified clojure version. Furthermore, would there be any issue with portability of the clojure code? –  MichaelT Jan 25 '13 at 20:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.