clojure

adding metadata to a lazy sequence

When I try to add metadata to an infinite lazy sequence in Clojure, I get a stack overflow, and if I take off the metadata, then it works just fine. Why does adding the with-meta macro break the lazy seq? First create an infinite seq of a very nice number: (defn good [] (lazy-seq (cons 42 (good)))) user> (take 5 (good)) ...

Clojure function reading from stream locks

I have a function that reads one token from an input stream that is called (get-next-indicator stream indicator) and returns it. I am trying to use it to construct a map. But, when I run it, it locks up. If I remove one of the get-next-indicator function, it does work. Does both functions try to read the stream at the same time is this...

How do I setup REPL on vim?

I have installed vimclojure to make it easier to start learning clojure. But, I haven't been able to setup REPL inside vim. This is essentially because I was not able to write a maplocalleader in vim(?) Based on the documentation in vimclojure/doc/clojure.txt I put the following in my .vimrc to start the REPL - :map <LocalLeader>sr *s...

Deploying Compojure/Sinatra Applications.

What is the preferred way of deploying a compojure/sinatra applications? I have multiple sites running on the host so i can't run jetty/mongrel on port 80. How should i handle multiple sites/applications running at the same host? ...

Get list of fields in struct

How would I find what fields a struct has? For example, if i have the following: (defstruct bintree :data :left :right) (def a (struct bintree 0 nil nil)) how would i get a list,set, or vector of (:data :left :right) from a? I've tried (show a) but this gives no methods that look correct. ...

Clojure macro problem

I'm trying to create a function to create a new basewith another struct as a base, and as a start I attempted to make a macro that would create a new struct with the same fields as the old one. The macro I have which i thought should do this is below, but it is giving the following error: java.lang.Exception: Can't use qualified name a...

Clojure/Java Mandelbrot Fractal drawing

I am trying to port this algorithm to clojure. My code is (defn calc-iterations [x y] (let [c (struct complex x y)] (loop [z (struct complex 0 0) iterations 0] (if (and (< 2.0 (abs z)) (> max-iterations iterations)) iterations (recur (add c (multiply z z)) (inc iterations)))))) ...

Trouble understanding :state in Clojure.

I understand what :state /does/. It creates a field, like in Java, in your class. What I don't understand is what is the point of this? It seems like I only see it done with Clojure-generated classes that extend other classes. http://www.fatvat.co.uk/2009/05/clojure-and-robocode.html being one example. I don't know Java, and I'm not very...

When you extend a Java class in Clojure and define a method of the same name as one in that class, what is happening?

I've been reading through Programming Clojure, and I've been having some trouble understanding Stuarts primary Java Interop example. He extends DefaultHandler, and creates a startElement method, and then passes that handler to an XML parser. What I don't understand, is what exactly is happening. Does his implementation of startElement ov...

Java input in Clojure with (read-line) not reading properly in EMACS

Has anyone encountered a problem with emacs on windows and java input ? (read-line) in the REPL does not recognise the delimiter (well thats my guess). Using a vanilla clojure in the box, same issue. Further clarification. Using (read-line) or the scanner class within the REPL in EMACS the reader will not respond to return (As in it...

How to get user input in Clojure ?

Hello everyone I'm currently learning clojure, but I was wondering how to get and store user input in a clojure program. I was looking at the clojure api and I found a function called read-line, however I'm not sure how to use it if it's the right function to use... Anyhow, how do you get user input in clojure ? ...

Questions about Lists and other stuff in Clojure

Hello everyone, I have a few questions concerning Lists, classes and variables in clojure. This may seem quite stupid but how do I access elements in a List ? I'm coding a program that allows you to manipulate a phonebook; You can add an entry, delete one or print the information about one. This leads me to two questions : Is the...

Order of code in Clojure

I have a simple yet frustrating problem in Clojure, I have a function (let's call it read-function) which figures out what the user wants to do from his input then calls another function that does that (let's call it action-function). This action-function calls the read-function when it's done so the user can perform another task. Now ...

Small problem accessing a map inside a list in Clojure

Title says it all, here's the code : (def entry {:name tempName :num tempNum}) (def tempList '(entry)) (println (get (nth tempList 0) (:name))) Exception in thread "main" java.lang.IllegalArgumentException: Wrong number of args passed to keyword: :name In this bit of code, I define a map called entry containing a :name and a :num...

Small question on creating Lists in Clojure

Hi, I am a beginner in Clojure, and I have a simple (stupid) question. I am trying to to read 4 user input , and then storing those inputs into a List this is my code: (def in1 (read-line)) (def in2 (read-line)) (def in3 (read-line)) (def in4 (read-line)) (def mylist '(in1 in2 in3 in4)) However, when i print the list, it gives...

Question about traversing a list

Hi, I am a beginner in Clojure, and I have a simple question Lets say i have a List, composed of Maps. Each Map has a :name and :age My code is: (def Person {:nom rob :age 31 } ) (def Persontwo {:nom sam :age 80 } ) (def Persontthree {:nom jim :age 21 } ) (def mylist (list Person Persontwo Personthree)) Now how do i traverse the lis...

Filter for Lists in Clojure

Hi, I am having a bit of difficulty with Lists in Clojure I have a quick question concerning the filter function Let's say I have a List composed of Maps My code is: (def Person {:name Bob } ) (def Person2 {:name Eric } ) (def Person3 {:name Tim } ) (def mylist (list Person Person2 Person3)) How would i go about filtering my list so t...

return a number with the first X bits set

what is a more efficient way to accomplish this in clojure: (defn ones ([n] (ones n 1 1)) ([n i res] (if (< i n) (recur n (inc i) (bit-set res i)) res))) preferably it should still "do the right thing" when it comes to numerical type. ...

How to end / Force a close to a program (in Clojure)

Hi, I am a pretty decent programmer in Java, however I am new to programming in Clojure. My question is : In java, to force an exit in the program, the code is :System.exit(0) ..........Is there any equivalent to this code is Clojure? -Thank you ...

Organizing Clojure Code

I have a program that draws shapes on an image. I have a separate namespace for each shape, and they are in separate files. com/name/box.clj --> has com.name.box namespace. com/name/triangle.clj --> has com.name.triangle namespace. They all share a common function called generate that draws them on screen, so if I use use, function nam...