clojure

How can I remove an item from a sequence in Clojure?

First, I assume each structure-specific sequences would have different ways to remove an item: Vectors could be by index, List could be remove first or last, Set should be passing of the actual item to remove, etc. Second, I assume there are some methods for removal that are structure agnostic; they work on seq interface. Since sequenc...

Redefining a let'd variable in Clojure loop

OK. I've been tinkering with Clojure and I continually run into the same problem. Let's take this little fragment of code: (let [x 128] (while (> x 1) (do (println x) (def x (/ x 2))))) Now I expect this to print out a sequence starting with 128 as so: 128 64 32 16 8 4 2 Instead, it's an infinite loop, printing 12...

Spring-managed Clojure beans

Hi. I'm just wondering if it's possible to create Spring managed beans backed by Clojure code (Clojure namespace I guess, would implement regular Java interface)? If so, the example would be appreciated. Thanks, Dmitriy. ...

Fast Prime Number Generation in Clojure

I've been working on solving Project Euler problems in Clojure to get better, and I've already run into prime number generation a couple of times. My problem is that it is just taking way too long. I was hoping someone could help me find an efficient way to do this in a Clojure-y way. When I fist did this, I brute-forced it. That was ea...

Emacs Clojure mode tab-indentation huge in some cases

I'm using Emacs' Clojure mode with SLIME and swank-clojure. I have an issue with the indentation. Most of the time the indentation does what I want: it indents with 2 spaces when I press TAB. But , for example, in the case of a proxy, the indentation I get with TAB is huge: 10 spaces. Example: (defn- create-frame [] (let [frame (JFram...

What is the clojure equivalent of the Python idiom "if __name__ == '__main__'"?

I'm dabbling in clojure and am having a little trouble trying to determine the clojure (and / or Lisp) equivalent of this common python idiom. The idiom is that at the bottom of a python module there is often a bit of test code, and then a statement which runs the code, for example: # mymodule.py class MyClass(object): """Main logi...

Help me write a Clojure macro which automatically adds metadata to a function definition

Hi. I realize that the first rule of Macro Club is Don't Use Macros, so the following question is intended more as an exercise in learning Clojure than anything else (I realize this isn't necessarily the best use of macros). I want to write a simple macro which acts as a wrapper around a regular (defn) macro and winds up adding some me...

What is the easiest way to persist maps/structs in Clojure?

The obvious way is to load up JDBC support from Clojure Contrib and write some function to translate a map/struct to a table. One drawback of this is that it isn't very flexible; changes to your structure will require DDL changes. This implies either writing DDL generation (tough) or hand-coding migrations (boring). What alternatives ex...

Processing pairs of values from two sequences in Clojure

I'm trying to get into the Clojure community. I've been working a lot with Python, and one of the features I make extensive use of is the zip() method, for iterating over pairs of values. Is there a (clever and short) way of achieving the same in Clojure? ...

Apache Integration with Lisp like web language

I've been playing a little bit with Arc/Anarki and Clojure lately. But what I really miss is something like mod_arc or mod_clojure for Apache. What I really miss is good Apache integration for a Lispy web language. Both Arc and Clojure use their own built in webserver that you launch within your code. I want all the functionality, re...

Binding multiple related variables in Clojure without nested let

I want to use the value of a variable to compute the value of another variable in the same let statement. Is there a way to do this in Clojure without using nested lets? Nested let solution: (let [x 3] (let [y (+ 1 x)] y)) = 4 Desired solution: (let [x 3 y (+ 1 x)] y) = 4 ...

Clojure mutable storage types

I'm attempting to learn Clojure from the API and documentation available on the site. I'm a bit unclear about mutable storage in Clojure and I want to make sure my understanding is correct. Please let me know if there are any ideas that I've gotten wrong. Edit: I'm updating this as I receive comments on its correctness. Disclaimer: A...

Please recommend a good Slime tutorial or screencast.

My adventures in Java have lead me to look into Clojure, which then lead me to (re)discover Emacs and that lead me to SLIME. I have a fairly decent handle on Emacs itself, and I have the emacs-starter-kit as well as clojure-mode/slime/swank as well as a few other unrelated modes and tweaks setup and running. But setting up a program an...

padding binary-block lazy sequences

I have Clojure function that takes a sequence of numbers chops it into the appropriate number of bits and returns a lazy sequence of the chunks (lowest order bits first). It pads the high order bits of the last block to fill out the block size and I need advice on the "best way(tm)" to record the amount of padding while keeping it lazy a...

Clojure Vector of Refs

What's the simplest way to create a vector of distinct refs? Using (repeat 5 (ref nil)) will return a list, but they will all reference the same ref: user=> (repeat 5 (ref nil)) (#<Ref@16ef71: nil> #<Ref@16ef71: nil> #<Ref@16ef71: nil> #<Ref@16ef71: nil> #<R ef@16ef71: nil>) Same result with (replicate 5 (ref nil)): user=> (replicat...

Clojure Parameters with Optional Flags

What's the best way to implement keywords as optional flags to a function? I want to make function calls such as: (myfunction 5) (myfunction 6 :do-this) (myfunction 3 :go-here) (myfunction 2 :do-this :do-that) Using defn, I can define a function such as: (defn myfunction [value & flags] ... ) But the flags becomes a list. I can wri...

Clojure representing byte array

In java i would read the whole file in to byte array and do some processing with it. Now i want to implement the same algorithm in clojure. What i am wondering is can i use a list instead of an array? I know i can create array from clojure but what is the lisp way of handling this? ...

Clojure While Loop

I trying clojure i am trying to figure out how to implement the following algorithm, I am reading from an input stream i want to continue reading until it is not a delimiter character. i can do this in java with a while loop but i can't seem to figure out how to do it in clojure? while read readChar != delimiter do some pr...

Clojure Variables and Looping

From googling around, I found that using while loops or using variables is discouraged. Now I implemented a very simple algorithm that will read characters from an inputstream and parse accordingly: if input is 10:abcdefghej it will parse out 10 then read next 10 bytes after the colon. The thing I am kinda lost with is how I can refact...

Clojure loop reads one extra.

When length is 4 following loop executes 5 times. Reading 5 characters from the stream. (loop [i (.read stream) result "" counter length] (let [c (char i)] (println "=>" c) (if (zero? counter) result (recur (.read stream) (str result c) (dec counter))))) ...