1 Replies - 584 Views - Last Post: 21 April 2011 - 06:44 PM

#1 lunixer  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 120
  • Joined: 29-January 10

Clojure compiler error

Posted 21 April 2011 - 06:11 PM

Hello.

In this segment of clojure code:
(defn makeStructs ;line 27
 "open fName as a file and turns each line into a struct. Returns a Vector of structs"
 [fName]
   with-open[r (reader (file fName))]
   (let [r 
      res (doall (map makeStruct (line-seq r)))
      ]    
  (. r close)
     res
  ) 
)



I am getting this compiler error:


Exception in thread "main" java.lang.Exception: Can't take value of a macro: #'clojure.core/with-open (clojureHW.clj:27)



Any idea what the problem is? I am pretty certain that I am using with-open correctly.

Is This A Good Question/Topic? 0
  • +

Replies To: Clojure compiler error

#2 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 590
  • View blog
  • Posts: 2,792
  • Joined: 05-January 09

Re: Clojure compiler error

Posted 21 April 2011 - 06:44 PM

I can't even begin to figure out what you're trying to do here. This is probably the most atrocious formatting I have ever seen in my life.

First of all:

with-open[r (reader (file fName))]


...?

You have to wrap these things in parentheses.

I had to reformat it myself so that I could read it.

(defn makeStructs ;line 27
 "open fName as a file and turns each line into a struct. Returns a Vector of structs"
 [fName]
 (with-open [r (reader (file fName))]
   (let [r res (doall (map makeStruct (line-seq r)))]    
     (. r close)
     res)))



First of all, don't use camelCase to name things. Use hyphens. makeStructs becomes make-structs and fName becomes f-name.

Second, do you know what let does? What is that supposed to be doing? [r res (doall (map makeStruct (line-seq r)))] doesn't make sense. A let is used for binding names to values. I don't know what's going on here.

Third, (. r close) is redundant. The whole point of with-open is that it closes the reader at the end. Not only is it redundant, but it's also not what you should be doing. (.close r) is a sugary form that you should prefer over the one you used. Nonetheless, it's entirely unecessary here.

Furthermore, what is this code supposed to do in the first place?

I strongly recommend that you read http://java.ociweb.c...re/article.html, a comprehensive and not-that-out-of-date tutorial on Clojure. It'll help you get the basics and the formatting down. Furthermore, check out my lisp formatting post as well.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1