clojure

Clojure program reading its own MANIFEST.MF

How can a Clojure program find its own MANIFEST.MF (assuming it is packaged in a JAR file). I am trying to do this from my "-main" function, but I can't find a class to use in the following code: (.getValue (.. (java.util.jar.Manifest. (.openStream (java.net.URL. (str "jar:" ...

How can I do batch image processing with ImageJ in Java or clojure?

I want to use ImageJ to do some processing of several thousand images. Is there a way to take any general imageJ plugin and apply it to hundreds of images automatically? For example, say I want to take my thousand images and apply a polar transformation to each--- A polar transformation plugin for ImageJ can be found here: http://rsb...

How to reconnect to slime/swank-clojure session?

It seems that whenever I disconnect from clojure slime session, I cannot reconnect again. I am using leiningen to start the swank session (with lein-swank plugin). So, every time I quit emacs (I know I shouldn't) or reboot/logout I have to restart both slime and swank. Is there a way to re-connect to a slime/clojure-swank session? ...

Iterator blocks in Clojure?

I am using clojure.contrib.sql to fetch some records from an SQLite database. (defn read-all-foo [] (with-connection *db* (with-query-results res ["select * from foo"] (into [] res)))) Now, I don't really want to realize the whole sequence before returning from the function (i.e. I want to keep it lazy), but if I return ...

How to filter a persistent map in Clojure?

I have a persistent map which I want to filter. Something like this: (filter #(-> % val (= 1)) {:a 1 :b 1 :c 2}) The above comes out as ([:a 1] [:b 1]) (a lazy sequence of map entries). However I want to be get {:a 1 :b 1}. How can I filter a map so it remains a map without having to rebuild it from a sequence of map entries? ...

How can I mix optional keyword arguments with the & rest stuff?

I have a macro that takes a body: (defmacro blah [& body] (dostuffwithbody)) But I'd like to add an optional keyword argument to it as well, so when called it could look like either of these: (blah :specialthingy 0 body morebody lotsofbody) (blah body morebody lotsofboy) How can I do that? Note that I'm using Clojure 1.2, so I'm al...

Podcasts for Clojurians?

I regularly listen to the podcasts DotNetRocks and Software Engineering Radio. Lately I have become more interested in Clojure. Are there any audio podcasts that target more toward the Clojure (more general FP/LISP/dynamic languages) audience? I know of some single podcast episodes that all give the introductory overview of Clojure (VM...

Producer consumer with qualifications

I am new to clojure and am trying to understand how to properly use its concurrency features, so any critique/suggestions is appreciated. So I am trying to write a small test program in clojure that works as follows: there 5 producers and 2 consumers a producer waits for a random time and then pushes a number onto a shared queue. a ...

isReachable in Java doesn't appear to be working quite the way it's supposed to

I'm using Clojure, but I can read Java, so this isn't a Clojure specific question. This doesn't even seem to be working from Java. I'm trying to implement a bit of a 'ping' function using isReachable. The code I'm using is this: (.isReachable (java.net.InetAddress/getByName "www.microsoft.com") 5000) Translated to Java by a good frie...

How to dispatch a multimethod on the type of an array

I'm working on a multimethod that needs to update a hash for a bunch of different things in a sequence. Looked fairly straitforward until I tried to enter the 'type of an array of X'. (defmulti update-hash #(class %2)) (type (byte 1)) => java.lang.Byte (defmethod update-hash java.lang.Byte [md byte] (. md update byte)) (type (into-a...

Does ClojureBox support auto-align out of the box?

I am switching to Clojurebox to edit my clojure files. However I can't get the automatic alignment to work. I want the cursor to move two spaces after the line that I am editing when I hit return. ...

How to return the output of a recursive function in Clojure

Hi everyone! I'm new to functional languages and clojure, so please bear with me... I'm trying to construct a list of functions, with either random parameters or constants. The function that constructs the list of functions is already working, though it doesn't return the function itself. I verified this using println. (edit: Okay, it ...

Idiomatic way to build a custom structure from XML zipper in Clojure

Say, I'm parsing an RSS feed and want to extract a subset of information from it. (def feed (-> "http://..." clojure.zip/xml-zip clojure.xml/parse)) I can get links and titles separately: (xml-> feed :channel :item :link text) (xml-> feed :channel :item :title text) However I can't figure out the way to extract them at the same tim...

Improving my first Clojure program

After a few weekends exploring Clojure I came up with this program. It allows you to move a little rectangle in a window. Here's the code: (import java.awt.Color) (import java.awt.Dimension) (import java.awt.event.KeyListener) (import javax.swing.JFrame) (import javax.swing.JPanel) (def x (ref 0)) (def y (ref 0)) (def panel (proxy [...

Using slime's C-x C-e (Eval the form under the point) with swank-clojure in emacs

Hi, I'm using swank-clojure in emacs on OSX. I'm able to run a slime session. When I use C-x C-e on a simple form in a .clj file like (+ 7 7) I get an sldb buffer with Unable to resolve symbol: + in this context [Thrown class java.lang.Exception] I'm able to evaluate that form in the slime session directly. I was hoping the fo...

Lazy sequence or recur for mathematical power function?

As an exercise I implemented the mathematical power function. Once using recur: (defn power [a n] (let [multiply (fn [x factor i] (if (zero? i) x (recur (* x factor) factor (dec i))))] (multiply a a (dec n)))) user=> (time (dotimes [_ 10000] (power 2 512))) "Elapsed time:...

Using a javax.servlet.Filter with Compojure

I'm trying to build a simple web site using Clojure / Compojure and want to feed apply a servlet filter to the request / response (i.e. a standard javax.servlet.Filter instance). e.g. if the current source code is: (defroutes my-app (GET "/*" (html [:h1 "Hello Foo!!"])) ) I would like to add a filter like this: (defroutes my-a...

Unexpected result from reduce function

I would like to get the smallest element from a vector. For this I use combine the reduce and min functions. However, when providing my own implementation of min I get unexpected results: user=> (reduce (fn [x y] (< x y) x y) [1 2 3 2 1 0 1 2]) 2 user=> (reduce min [1 2 3 2 1 0 1 2 3]) 0 The reduce with standard min returns 0 as expec...

How would you write this Clojure snippet in Ruby and/or Haskell?

I was working on a Rails template and was trying to write a bit of code that allows me to populate a table or multiple columns of ul tags "top-to-bottom" and "left-to-right" across however many columns I specify. I'm just getting the hang of Ruby so I couldn't figure this out. I'm also curious about an idiomatic Haskell version for this ...

Yet another Haskell vs. Scala question

I've been using Haskell for several months, and I love it—it's gradually become my tool of choice for everything from one-off file renaming scripts to larger XML processing programs. I'm definitely still a beginner, but I'm starting to feel comfortable with the language and the basics of the theory behind it. I'm a lowly graduate studen...