clojure

Mutable fields in Clojure deftype?

I'm trying out Clojure 1.2, specifically mutable fields which are supported in deftype according to the clojure.org documentation. But I can't get the set to work. What is the syntax for updating a field? Or isn't mutability implemented yet? (definterface IPoint (getX []) (setX [v])) (deftype Point [x] IPoint (getX [this] x) ...

Immutable queue in Clojure

What is the best way to obtain a simple, efficient immutable queue data type in Clojure? It only needs two operations, enqueue and dequeue with the usual semantics. I considered lists and vectors of course, but I understand that they have comparatively poor performance (i.e. O(n) or worse) for modifications at the end and beginning re...

Managing updates to nested immutable data structures in functional languages

I've noticed while on my quest to lean functional programming that there are cases when parameter lists start to become excessive when using nested immutable data structures. This is because when making an update to an object state, you need to update all the parent nodes in the data structure as well. Note that here I take "update" to m...

Sending a POSIX signal from the JVM

How do I send a POSIX signal from within the JVM? (in Java or Clojure) I never thought this would be an issue until I tried googling it — there is lots of information about handling signals, but nothing about sending them. Short of using the JNI or calling the shell to execute "kill", is there any other way to send a signal to a PID? ...

Avoiding duplicate library .jars when exporting a single .jar in Eclipse

I'm using the Eclipse "Export... Runnable jar file" feature to package up my Clojure+Java application for deployment. This works great, magically including various resources and Clojure source files etc. The one issue I have is that various libraries I have get included multiple times from the "lib" directory dependant projects, e.g. I...

Clojure: reduce vs. apply

I understand the conceptual difference between reduce and apply: (reduce + (list 1 2 3 4 5)) ; translates to: (+ (+ (+ (+ 1 2) 3) 4) 5) (apply + (list 1 2 3 4 5)) ; translates to: (+ 1 2 3 4 5) However, which one is more idiomatic clojure? Does it make much difference one way or the other? From my (limited) performance testing, it ...

Is there a way to get a collection of clojure special forms programatically?

Does something similar to this exist?: (deftest fantasy (is (= ["let" "def" "ns" "etc."] clojure.core/special-chars))) ...

What's the most idiomatic way to pass vectors in for var-args in clojure?

Suppose that I have a vector of key-value pairs that I want to put into a map. (def v [k1 v1 k2 v2]) I do this sort of thing: (apply assoc (cons my-map v)) And in fact, I've found myself doing this pattern, (apply some-function (cons some-value some-seq)) several times in the past couple days. Is this idiomatic, or is there a ...

Compojure + clojure.contrib.sql: SELECT query is being cached. Why?

I'm writing a Compojure TODO app and with MySQL as the primary data store. I'm using clojure.contrib.sql to interface with MySQL as follows: (def db {:classname "com.mysql.jdbc.Driver" :subprotocol "mysql" :subname "//localhost:3306/todo" :user "<user>" :password ""}) The queries I'm using seem to w...

Make macros and functions integrate more seamlessly

OK, I understand pretty well how to use both function and macros. What I'm curious about is why the compiler can't be a bit more clever when integrating the two, e.g. consider the Clojure code: (defmacro wonky-add [a b] `(+ ~a (* 2 ~b))) (defn wonky-increment [a] (apply wonky-add a 1)) => Error: can't take value of a macro Yes, I kn...

Accept headers of the clojure.xml/parse call

When calling the function clojure.xml/parse with an URI Clojure performs a HTTP GET request to fetch the data. However the HTTP request contains the following accept headers: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2 Shouldn't this be application/xml? ...

Make Clojure recognize and isolate the line in a file

Hi! I am trying to get Clojure to read a file, put the first line in a variable, and the rest in another variable. I cannot seem to find out how to do this and would love if anyone could give me a heads up, ...

Java command lastModified() not working in Clojure

I am trying to get the last modified time from a file in Clojure, by executing a Java command. By using java.io.File.lastModified I am supposed to be able to get the UNIX-time, this does not work by execution of the script or in the REPL. My code is: (java.io.File.lastModified "/home/lol/lolness.txt") and my error is: java....

What is the good starting point to developing RESTful web service in Clojure?

I am looking into something lightweight, that, at a minimum should support the following features: Support for easy definition of actions through metadata Wrapper that extracts parameters from request into clojure map, or as function parameters Support for multiple forms of authentication (basic, form, cookie) basic authorization based...

Clojure Metaprogramming Question (for a beginner!)

All, I'm starting to take a look at the Clojure language, and had a couple questions about something I'm trying to do. The broad objective is to alias the sequence function every? to all?. I'm sure there's a function or macro that does alias-ing (or something along those lines) but I wanted to see if it was possible with some of the basi...

How do I use clojure.set/difference? Why won't it work on a PersistentSet?

The following code: (require '[clojure.set]) (println (clojure.set/difference '("a" "b" "c" "d") '("c" "d" "e" "f"))) gives me the following error: java.lang.ClassCastException: clojure.lang.PersistentList (repl-1:47) I don't understand what I'm doing wrong. Shouldn't this print out ("a" "b")? ...

A way to strip returned values from java.io.File.listFiles in Clojure

Hi I call a java function in Clojure to get a list of files. (require '[clojure.java.io :as io]) (str (.listFiles (io/file "/home/loluser/loldir"))) And I get a whole bunch of strings like these #<File /home/loluser/loldir/lolfile1> etc. How do I get rid of the brackets and put them in some form of an array so another function can...

Why are there so many map construction functions in clojure?

Novice question, but I don't really understand why there are so many operations for constructing maps in clojure. You have conj, assoc and merge, but they seem to more or less do the same thing? (assoc {:a 1 :b 2} {:c 3}) (conj {:a 1 :b 2} :c 3) (merge {:a 1 :b 2} {:c 3}) Whats really the difference and why are all these methods re...

Why is there no peek! function for clojure transient vectors?

Clojure has transient analogs for some of its persistent data structures, vectors, maps and sets. For vectors, there are pop! and conj! functions, analogous to pop and conj for persistent vectors, but no peek!. Is there a technical reason that makes an efficient implementation of peek! impossible? Or is it just not necessary in most...

How to create default value for function argument in Clojure.

...