clojure

Can not get clojure-contrib to load - FileNotFoundException

I have tried putting clojure-contrib.jar various places on my filesystem, I have tried manually specifying the classpath when launching the REPL, I have tried extracting the jar and putting the clj files on the classpath, nothing seems to work. I continue getting something like this: java.io.FileNotFoundException: Could not locate cloju...

Building IP and Port from Byte Buffer

I have a byte buffer 6 bytes long first four contains ip address last 2 contains port, in big endian notation. to get the ip i am using, (apply str (interleave (map int (take 4 peer)) (repeat "."))) Is casting bytes to int safe to get the ip address? and also in java i use, int port = 0; port |= peerList[i+4] & 0xFF; po...

What is the idiomatic way to capture prints to *out* from a Clojure function?

For example, the prxml function prints XML to *out*. I would like to instead capture this output as a String. Here is the typical usage from a REPL: user> (prxml [:p "Test"]) <p>Test</p>nil I'd instead like to do: (def xml (capture-out (prxml [:p "Test"]))) I made up the capture-out function, but I suspect something like it exists,...

What to learn after PHP? Scala or Clojure?

I have a heavy web dev background with PHP. My reasons for learning a functional programming languages are: to improve my programming skills. It was heavily suggested that learning a FPL helps. this has high priority because I want to be better and better. learn a general purpose programming language to solve tasks like scripting (OS s...

Clojure: How to replace an element in a nested list ?

I have this deeply nested list (list of lists) and I want to replace a single arbitrary element in the list. How can I do this ? (The built-in replace might replace many occurrences while I need to replace only one element.) ...

Any example in which Clojure really shines against Java which is not concurrency/immutability-feature related?

I can perfectly see why Clojure is really good for concurrent programming. I can see the advantages of FP also in this regard. But clearly, not every line of code that we write is part of a thread or needs concurrent access. For those parts of the code (the more simple and sequential piece of code) what is it that Java really missed tha...

Replace strings using regular expressions and backreferences in Clojure

I'm trying to convert from HTML to Latex, and want to change this: <a href="www.foo.com/bar">baz</a> into: baz\footnote{www.foo.com/bar} I'd like to generate a Clojure function to take a chunk of text, and replace as many matches as exist in a given paragraph. I've tried (.replaceAll "<a href=\"foo.com\">baz</a>" "<a.*h...

How might you implement design-by-contract in Clojure specifically or functional languages in general?

I'd prefer examples to be in a Lisp variant (bonus points for Clojure or Scheme) since that's what I'm most familiar with, but any feedback regarding DBC in functional lanugages would of course be valuable to the greater community. Here's an obvious way: (defn foo [action options] (when-not (#{"go-forward" "go-backward" "turn-right...

How can you "parameterize" Clojure Contrib's test-is?

Both Junit and TestNG provide mechanisms for iterating over a collection of input parameters and running your tests against them. In Junit this is supported via the Parameterized annotation, while TestNG uses @DataProvider. How can you write data-driven tests using the test-is library? I tried using for list comprehension to iterate ove...

Clojure data structure traversal/searching.

I'd like to be able to do something like this: (search data list? (fn [x] (and (list? x) (= 4 (first x)))) (fn [x] (and (set? x) (contains x 3)))) And have it recursively search a nested data structure data: first for the shallowest lists (might be in a set of sets, for example). then within those lists for the shallowest li...

setLookAndFeel and NullPointerException

Hi! Has anyone ever tried to change swing's look and feel? This code, taken from an example, simply yields a null pointer exception, and I wonder what might be wrong: (javax.swing.UIManager/setLookAndFeel (javax.swing.UIManager/getSystemLookAndFeelClassName)) Thanks! ...

How to translate this piece of imperative code into Clojure code

Usually, we have situation like this in C++ int a=0; if(some_condition_satisfied(g)) { a = eval(g); // never returns 0 } if(a!=0) { do_something(); } How can I do the above in Clojure without using refs, because I cannot assign after initialization? Thanks. ...

tutorial for installing VimClojure

Is there any tutorial/screencast available online for free to figure out how to use/install vimClojure. ...

Printing and reading lists from a file in Clojure

Hi, I have a Clojure data structure of the form: {:foo '("bar" "blat")} and have tried writing them to a file using the various pr/prn/print. However, each time the structure is written as {:foo ("bar" "blat")} then when I try to read in it using load-file, I get an error such as: java.lang.ClassCastException: java.lan...

How to write this piece of code in Clojure

If I have something like this: int foo() { if(somecondition) { // some code if(cond2) return -1; // some code again } if(cond3){ // something again } return bar(); } How do I express it in Clojure? it should not be written as if_elseif because both the somecondition and cond3 may be tr...

Clojure: How to to recur upon exception?

I am trying to execute a func several times before giving up upon exceptions. But it is not valid in Clojure to recur from catch block. How can this be achieved ? (loop [tries 10] (try (might-throw-exception) (catch Exception e (when (pos? tries) (recur (dec tries)))))) java.lang.UnsupportedOperationException: Cannot r...

Does Functional programming allow better runtime compiler optimizations?

NOTE: Already made this a Wiki. I don't care what this question is tagged as, as long as there is a good discussion. I've heard that since in pure functional programs, there are no side effects and values dont mutate, it makes it easier for the compiler to make more runtime optimizations. To what extent is this true? If this is true, m...

How do you evaluate a string as a clojure expression?

How would I get something similar to the following?: (evaluate-text "(+ 1 2)") ; resolves to 3 ...

Building the directory tree

I am trying to build a directory tree such as how xml trees are represented, in the form of a vector, i can traverse the file system fine using the following snippet but i can't put my head around how to build a tree structure out of this? (defn trav [dir] (if (.isDirectory dir) (do (println (.getName dir)) (doseq [fi...

clojure friendly java library for traversing NAT

I need to be able to establish a connection between two boxes that are both behind NAT? I have a central system that both sides can find each other through. I'm working in Clojure. ...