clojure

How to read mentally Lisp/Clojure code

Thanks a lot for all the beautiful answers! Cannot mark just one as correct Note: Already a wiki I am new to functional programming and while I can read simple functions in Functional programming, for e.g. computing the factorial of a number, I am finding it hard to read big functions. Part of the reason is I think because of my inab...

Lazy evaluation issue in Clojure

It tried the following in REPL and got no error (because of lazy evaluation): (defn square [n] (* n n)) (if (= 0 0) (println "hello") (map square ["a" "b"])) The following gives error (because it gets evaluated): (defn square [n] (* n n)) (if (= 0 1) (println "hello") (map square ["a" "b"])) My concern is that ...

Is this Clojure Indentend Properly?

http://pastebin.com/d2294a374 I've posted code indented in 2 different way in there. I get confused here because I am not sure what to do when a function has multiple arguments. Should each argument be on a different line or the same? furthermore, when we have something like (def xyz (fn abc [a1 a2] ... does (fn abc ... go in a differen...

Haskell to Clojure

I am going over this haskell lecture on count down game, i don't know any haskell but i am intrested in the problem, i am trying to port his code to clojure. this is the part i got stuck must be something i don't get in haskell, split :: [a] -> [([a],[a])] split [] = [([],[])] split (x:xs) = ([],x:...

More explanation on Lexical Binding in Closures?

There are many SO posts related to this, but I am asking this again with a different purpose I am trying to understand why closures are important and useful. One of things that I've read in other SO posts related to this is that when you pass a variable to closure, the closure starts remembering this value from then onwards. Is this the...

namespace in clojure

I have created sample namespace: [demas @arch.local.net ][~/dev/projects/diary]% cat shell_space.clj (ns shell_space) (defn test_fu [] (println "test-shell")) How can I use the test_fu from my namespace? I have tried: [demas @arch.local.net ][~]% clj Clojure 1.1.0-alpha-SNAPSHOT user=> (require 'shell_space) java.io.FileNotFoun...

Compojure HTML Formatting

I'm relatively new to Clojure and a complete HTML/Compojure virgin. I'm trying to use Compojure to create static pages of HTML using a function similar to this: (defn fake-write-html [dir args] (let [file (str dir *file-separator* *index-file*) my-html (html (doctype :html4) [:html ...

Code completion in CounterClockWise?

Hi, I am a first-time user of CounterClockWise, the Eclipse plugin for Clojure, and it appears that code completion just won't work. I keep wondering whether the problem lies between my keyboard and my chair, but can't find a solution. I just installed CCW on Galileo build 20090920-1017 and MacOSX. Then I try to write some lines of cloj...

How would one create a Clojure Lint?

One example of a common Clojure programming error is expecting a lazy expression to be evaluated for side-effects. On the surface it appears checking for unused lazy expressions would be helpful. What would be the best approach to identifying this and other common mistakes? Should the core compiler check for these situations, or should i...

How can I dynamically look up a static class member in Clojure?

In Clojure I can look up a static member of a Java class (e.g. a field holding a constant) like this: ClassName/CONSTANT_FIELD How can I access the member when I only know it's name at runtime? An example would be looping over a sequence of field names and getting all the field values. I would like to do something like this (this cod...

Easiest way to manage my CLASSPATH?

I'm beginning to play with Clojure a bit and my Java experience is pretty limited. I'm coming from the dynamic world of Ruby and OO, so the functional side of things is very interesting! Anyway, as I discover libraries and various tools for use (and the tutorial files for the Pragmatic Clojure Book), everything typically calls for plac...

which non-sun java(s) can i use to run Clojure?

I'm automating the process of building development-VMs for a project and having a really hard time getting sun-java-6 to install in a non-interactive environment because it really wants to ask about licenses. what are my other options are far as clojure friendly javas go? ...

Clojure emacs slime + swank directory question

I'm using emacs with clojure-swank and slime and trying to set my development environment. And I ran into a problem. When I start a repl I'm stuck in an unknown directory preventing me to load my namespace. Because the clojure repl can't find the right file. Does anyone know how to change the current directory? PS: I've just started us...

WAR created using Clojure deployed on Apache Tomcat does not run Servlet

Hi, I created the sample WAR as given at the Compojure Getting Started Page and deployed it to Apache Tomcat 6.0.2 wepapps folder. The Web.xml I used is as below: <web-app> <servlet> <servlet-name>myservlet</servlet-name> <servlet-class>myapp.MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>myservlet</se...

Nested types in clojure?

In clojure, how do I type type hint a type that I have created? (I want to nest the types.) e.g. I had thought that this would work: (deftype A [#^somePrimitive someField]) (deftype B [#^A Avalue]) This brings up an error message: Unknown location: error: java.lang.ClassNotFoundException: A Note: clojure types are a...

Finding keys closest to a given value for clojure sorted-maps.

For clojure's sorted-map, how do I find the entry having the key closest to a given value? e.g. Suppose I have (def my-map (sorted-map 1 A 2 B 5 C)) I would like a function like (find-closest my-map 4) which would return (5,C), since that's the entry with the cl...

How is the `*var-name*` naming-convention used in clojure?

As a non-lisper coming to clojure how should I best understand the naming convention where vars get a name like *var-name*? This appears to be a lisp convention indicating a global variable. But in clojure such vars appear in namespaces as far as I can tell. I would really appreciate a brief explanation of what I should expect when an...

Clojure: sequence back to vector

How can I cast a sequence back to vector after a sequence producing operation (like sort)? Does using (vec..) on a sequence that was a vector is costly? One (bad?) possibility is creating a new vector out of sequence: (vec (sort [1 2 3 4 5 6])) I am asking because I need random access (nth ..) to huge sorted vectors - which are now ...

Does Clojure have an equivalent of Java's import package.*?

Or do I have to specifically enumerate every class that I import? I'm just learning Clojure now, and it seems useful to be able to do something like this in the REPL: (import '(java.io *)) Not that this is valid syntax, but it would be nice to have something that does the equivalent. It would save some typing, especially when tink...

Modelling / documenting functional programs

I've found UML useful for documenting various aspects of OO systems, particularly class diagrams for overall architecture and sequence diagrams to illustrate particular routines. I'd like to do the same kind of thing for my clojure applications. I'm not currently interested in Model Driven Development, simply on communicating how applica...