clojure

When to use let vs. if-let in Clojure

When does using if-let rather than let make code look better and does it have any performance impact? ...

Abstract functions or function interfaces in Clojure?

In a number of occasions I have a collection of functions that I'd like to implement in different ways. The most obvious example of this would be to abstract from specific databases. In an object-oriented language you would use an interface for this: interface DB { ResultSet query(String query); void persist(Object o); ... } In ...

Check the class of something in clojure?

I'm learning clojure and have a very basic question: given that clojure has type infernece, how can you tell what class was inferred? For instance, these would each result in difference data types: (2) (/ 2 3) (/ 2.0 3) Is there some kind of class function that will return the data type? Also, is there a normal way of casting some...

Clojure namespace: method to see defined objects?

Is there some way to see what has already been defined in a clojure session (equivalent to calling ls())? Let's say that I create a few objects: (def x 1) (def y 2.2) (def plus-one (fn [x] (+ x 1))) Is there a command that can be run to show me that these now exist in the user namespace? ...

Common programming mistakes for Clojure developers to avoid

Following a masterful trend: Common programming mistakes for Scala developers to avoid? Common programming mistakes for Java developers to avoid? Common programming mistakes for JavaScript developers to avoid? Common programming mistakes for .NET developers to avoid? Common programming mistakes for Haskell developers to avoid? Common p...

Idiomatic clojure for progress reporting?

How should I monitor the progress of a mapped function in clojure? When processing records in an imperative language I often print a message every so often to indicate how far things have gone, e.g. reporting every 1000 records. Essentially this is counting loop repetitions. I was wondering what approaches I could take to this in cloj...

How to read lines from stdin (*in*) in clojure

I am writing my first clojure program, and want to read lines from stdin. When I try this: (doall (map #(println %) (line-seq *in*))) I get this exception: Exception in thread "main" java.lang.ClassCastException: clojure.lang.LineNumberingPushbackReader cannot be cast to java.io.BufferedReader (test.clj:0) I get the same results i...

returning multiple values using clojure xml zipper

Lets suppose we have some XML like so: <a> <b> <c>text</c> <d> <e>text</e> <f> ... lots of cruft here .. </f> </d> </b> <b> ... </b> <!-- more b sub-trees --> </a> Now, looking through the samples in zip_filter/xml.clj, I've figured out how to get to single values that I'm intereste...

How to load program resources in Clojure

How do you load program resources such as icons, strings, graphical elements, scripts, and so on in a Clojure program? I am using a project layout similar to that in many Java projects where there is a "resources" directory hanging off of a "source" directory. A jar file is created from the source and includes the resources, but I can't ...

When should I use deftype in Clojure?

Yesterday, Rich pulled the 'new' branch of Clojure into master. We are now embracing the beauty that is deftype and defprotocol. Of course, I, coming from Haskell, am very tempted to define types like I would in Haskell, which would be for virtually everything short of a throwaway tuple, but I don't think it works like that in Clojure wo...

How do I combine results from zip-filter queries on an xml tree in Clojure?

I want to combine the results of three zip-filter queries on an xml tree. The XML I am parsing looks like this: <someroot> <publication> <contributors> <person_name> <surname>Surname A</surname> </person_name> <person_name> <given_name>Given B</given_name> <surname>Surname B</surname> ...

Clojure weirdness with Var.intern and RT.var

So I'm trying to explore Clojure's internals and I've come across something I'm not quite sure I understand: From the REPL, I can access RT.var("clojure.core","require") just fine (this is supposed to return the var associated with the "require" symbol in the "clojure.core" namespace): user=> (clojure.lang.RT/var "clojure.core" "requir...

let inside cond

Hello. I'm working with clojure and while I've dabbled with lisps before, I'm having trouble finding a clean way to nest let statements in cond statements. For example, consider the following function: (defn operate-on-list [xs] (let [[unpack vector] (first xs)] (cond [(empty? xs) 'empty unpack vector :else (op...

Clojure rename loaded libraries

Is there any way to rename clojure libraries when you load them with require or use? For example, is there any way to do something like (require 'some.include.path.some-library :as something-else) and then reference elements of some-library through something-else/element-name? Thanks. ...

Enclojure NBM file turns out to be a folder

I'm trying to install Enclojure. I've downloaded the latest zip file from GitHub (http://github.com/EricThorsen/enclojure/downloads), but when I unzip it, I get a folder ending in .nbm, not a file. The install directions say to point NetBeans to a .nbm file, not a folder, and NetBeans won't let me select the folder, either. The folder co...

A clojure friendly library for playing sounds

I'm looking for an easy to program library for infrequently playing sounds (notifications and the like) from a clojure function. edit: like this (use 'my.sound.lib') (play-file "filename") (beep-loudly) (bark-like-a-dog) ... ...

Better alternative to pmap in Clojure for parellizing moderately inexpensive functions over big data?

Using clojure I have a very large amount of data in a sequence and I want to process it in parallel, with a relatively small number of cores (4 to 8). The easiest thing to do is use pmap instead of map, to map my processing function over the sequence of data. But the coordination overhead results in a net loss in my case. I think the r...

StackOverFlow while counting digits

Hi, I am trying to count the number of digits in a number in Clojure as follows: I get a StackOverflowError even for 2 digit numbers (defn num-digits [n] (if (= 0 n) 0 (inc (num-digits (/ n 10))))) (println (num-digits 93)) But if I replace / with unchecked-divide then it works for at least 93. But neither of the techniques ...

Learning a Lisp variant? Suggestions?

I ultimately want to learn Clojure, but I've found learning resources for Clojure to be scarce for people of little experience... I'm wondering if it would be beneficial to start with Scheme (read The Little Schemer and SICP) or some other Lisp variant. My only other programming experience is with Java and Python (which is pretty minim...

safely parsing maps in clojure

I'm looking for an easy and safe way to parse a map, and only a map, from a string supplied by an untrusted source. The map contains keywords and numbers. What are the security concerns of using read to do this? ...