clojure

Getting the name of a Clojure struct type?

When defining a struct type and instance, I can print the value and get the "struct" implementation type: (defstruct person :name :age) (def p (struct person "peter" 30)) user=> p {:name "peter", :age 30} user=> (type p) clojure.lang.PersistentStructMap But is it possible to tell whether p is an instance of the struct type "person"? ...

Overview of features in Clojure coming from other languages than CL

I am searching for an overview of the features of Clojure that are coming from other languages than Common Lisp. For example: STM: language X, Y and Z Your input is welcome! ...

Delayed evaluation in Clojure

I'm having some trouble understanding how the delay macro works in Clojure. It doesn't seem to do what expect it to do (that is: delaying evaluation). As you can see in this code sample: ; returns the current time (defn get-timestamp [] (.getTime (java.util.Date.))) ; var should contain the current timestamp after calling "force" (def ...

Good workflow with emacs+swank+slime+clojure?

I just wanted opinion on good workflow using the emacs environment with clojure+swank+slime. I often find myself doing very repetitive keycommands and wonder if there is an obvious better way. I include swank with lein and start my project using lein swank from shell. Then I connect with emacs and do the correct use commands so that I ...

Performance Problem with Clojure Array

This piece of code is very slow. Execution from the slime-repl on my netbook takes a couple minutes. (def test-array (make-array Integer/TYPE 400 400 3)) (doseq [x (range 400), y (range 400), z (range 3)] (aset test-array x y z 0)) Conversely, this code runs really fast: (def max-one (apply max (map (fn [w] (apply max (map #(fir...

Clojure lots of threads

I just got done watching Rick Hickey's "Clojure Concurrency" talk, and I have a few questions about threads. Let's say I have a situation with lots of Agents, let's say 10,000 of them running one machine. I'd rather not have 10,000 CPU threads running at once, but I don't want threads to be blocked by the actions of other threads. In...

How to benchmark functions in Clojure?

I know I can get the time take to evaluate a function can be printed out on the screen/stdout using the time function/macro. The time macro returns the value of the evaluated function, which makes it great to use it inline. However I want to automatically measure the runtime under specific circumstances. Is there a function which retur...

Advantages of Clojure

Can somebody point out the advantages of Clojure and what type of applications is it suited for ? I don't intend to compare it to any languages as such. As a language in itself what is it suitable for ? My intention is to know the right tools for the right job, and where does clojure fit-in in that kind of scenario. ...

Coverting a vector of maps to map of maps in clojure

Hi, I've a vector of maps like this: [ {:categoryid 1, :categoryname "foo" } {:categoryid 2, :categoryname "bar" } {:categoryid 3, :categoryname "baz" } ] and would like to generate a map of maps like this for searching by categoryname { "foo" {:categoryid 1, :categoryname "foo" }, "bar" {:categoryid 2, :categoryname "bar" ...

Multimethods performance

What's the performance hit of using multi methods? If I have 2 functions with the same name, and the same number of arguments that differ only by the type (list vs. int), is my performance going to suffer much? In other words, it it better to name my vector adding function: "add-vector" or leave it as "add" or possibly "+"? (For the sa...

Passing Args to Clojure from Java

I would like to embed Clojure code into Java. This site was helpful in the basics of setting this up, but the only arg it ever passes is of type String. I have tried using ints as well, and those also work. My question is whether there is some formatted way to pass in structured data to Clojure. In particular, I have a list of points I ...

Ref to map vs. map to refs vs. multiple refs

I'm working on a GUI application in Swing+Clojure that requires various mutable pieces of data (e.g. scroll position, user data, filename, selected tool options etc.). I can see at least three different ways of handling this set of data: Create a ref to a map of all the data: (def data (ref { :filename "filename.xml" :scroll ...

Adding fields to a proxied class in Clojure

I'm using "proxy" to extend various Swing classes in a Clojure GUI application, generally with code that looks something like: (def ^JPanel mypanel (proxy [JPanel] [] (paintComponent [#^Graphics g] (.drawImage g background-image 0 0 nil)))) This works well but I can't figure out how to add additional fields to the newly e...

swap! alter and alike

Hello, I am having a problem understanding how these functions update the underlying ref, atom etc. The docs say: (apply f current-value-of-identity args) (def one (atom 0)) (swap! one inc) ;; => 1 So I am wondering how it got "expanded" to the apply form. It's not mentioned what exactly 'args' in the apply form is. Is it a sequence...

Idiomatic approach for structuring Clojure source code

I'm interested in how people structure their Clojure source code. Being used to Java, I'm pretty familiar with the paradigm of one class per source code file, bundling all the data and method definitions with appropriate comments and annotations etc. However Clojure offers a lot more flexibility, and I'm not sure how I should structur...

Clojure Multimethods

In one namespace say "shapes" I have the following, (derive ::rect ::shape) (derive ::square ::rect) now in shapes ns when I do, (isa? ::square ::shape) returns true,but in the namespace where I actually implement multimethods for drawing when I do, (isa? ::square ::shape) it returns false so even though I have correct multi met...

Updating an atom with a single value

I have a number of atoms in my code where a common requirement is to update them to a new value, regardless of the current value. I therefore find myself writing something like this: (swap! atom-name (fn [_] (identity new-value))) This works but seems pretty ugly and presumably incurs a performance penalty for constructing the anonym...

how does one _model_ data from relational databases in clojure ?

I have asked this question on twitter as well the #clojure IRC channel, yet got no responses. There have been several articles about Clojure-for-Ruby-programmers, Clojure-for-lisp-programmers.. but what is the missing part is Clojure for ActiveRecord programmers . There have been articles about interacting with MongoDB, Redis, etc. - ...

Why isn't this running in constant space (and how do I make it so it does)?

I'm doing Project Euler to learn Clojure. The purpose of this function is to calculate the lcm of the set of integers from 1 to m. (lcm 10) returns 2520 This is a rather brute-force way of doing this. In theory, we go through each number from m to infinity and return the first number for which all values 1 through m divide that number...

Clojure Lazy Sequences that are Vectors

I have noticed that lazy sequences in Clojure seem to be represented internally as linked lists (Or at least they are being treated as a sequence with only sequential access to elements). Even after being cached into memory, access time over the lazy-seq with nth is O(n), not constant time as with vectors. ;; ...created my-lazy-seq here...