Questions tagged [clojure]

Clojure is a modern Lisp dialect for the Java Virtual Machine (with versions for the CLR and JavaScript). More than merely an implementation of Lisp in Java, Clojure provides access to Java's classes and ecosystem.

Clojure is a Lisp dialect for the Java Virtual Machine (with versions for the CLR and JavaScript).

Clojure is a modern Lisp dialect with an emphasis on functional programming (lazy/impure), running on the JVM with transparent access to all Java libraries, an interactive REPL development environment, dynamic runtime polymorphism, Lisp-style macro meta-programming and concurrent programming capabilities supported by software transactional memory.

Key features:

  • Lisp heritage - Clojure is a fully homoiconic language with support for macro-based metaprogramming. The full features of the language are available at compile time, and it is possible to manipulate "code as data". These mechanisms are frequently used to extend the language itself or create new domain-specific languages.
  • Functional programming - Clojure is primarily a functional language. It features immutable data structures and lazy sequences. Like many other Lisps, it is eagerly evaluated (although lazy sequences, macros and closures can be introduced to obtain lazy behavior) and impure.
  • Concurrent programming, supported by software transactional memory, and designed for multi-core environments.
  • Dynamic - Clojure is a dynamic language. However it should be noted that it is still fully compiled, exploits primitive operations on the JVM where needed for performance and can also support (optional) static type hints.
  • Hosted on the JVM, allowing for easy and transparent access to the wide ecosystem of Java libraries.
  • Open source - Clojure is run as a collaborative open source project (hosted on GitHub) and there is a rapidly growing ecosystem of open source libraries for Clojure, in addition to all the open source tools already available for Java
  • Portable - Clojure can run on any platform that supports the JVM, and Clojure versions are also available for the CLR (ClojureCLR) and JavaScript (ClojureScript)
  • Software Transactional Memory (STM) providing Multiversion Concurrency Control (MVCC) - Clojure refs provide thread safety and concurrency benefits without requiring explicit locking by the Clojure programmer

Code Samples:

Hello world is simple:

  (println "Hello World!")

The "infamous" Lisp parentheses are used to apply a function, which is always the first item in the list:

  (+ 1 2 3 4)
  => 10 

Functions can easily be defined and passed to higher order functions like map:

  (defn triple [x]
    (+ x x x))

  (map triple [1 2 3 4 5 10])
  => (3 6 9 12 15 30)

Infinite lazy sequences are fully supported:

  (take 7 (iterate (partial str "a") "b"))
  => ("b" "ab" "aab" "aaab" "aaaab" "aaaaab" "aaaaaab")

You can even do powerful computations on infinite sequences, such as defining the complete Fibonacci series:

  (def fibonaccis
    (lazy-cat [0 1] (map + fibonaccis (rest fibonaccis))))

  (take 10 fibonaccis)
  => (0 1 1 2 3 5 8 13 21 34)

Resources:

Web Sites

Tools

Books

Articles

Videos

Exercises

Trainings

17607 questions
69
votes
9 answers

Clojure not nil check

In Clojure nil? checks for nil. How does one check for not nil? I want to do the Clojure equivalent of the following Java code: if (value1==null && value2!=null) { } Follow-up: I was hoping for a not nil check instead of wrapping it with not. if…
Steve Kuo
  • 61,876
  • 75
  • 195
  • 257
69
votes
7 answers

Return first item in a map/list/sequence that satisfies a predicate

I am looking for a function that returns the first element in a sequence for which an fn evaluates to true. For example: (first-map (fn [x] (= x 1)) '(3 4 1)) The above fake function should return 1 (the last element in the list). Is there…
Matthew
  • 11,203
  • 9
  • 38
  • 44
68
votes
5 answers

How to display Clojure version in REPL?

Such as: (println clojure-version) ?
qrest
  • 711
  • 1
  • 5
  • 3
68
votes
11 answers

Why Clojure over other JVM Lisps: Kawa, Armed Bear or SISC?

The JVM already had three Lisps before Clojure arrived on the scene: Kawa, Armed Bear and SISC. What gap does Clojure fill that was left by those Lisps?
uzo
  • 2,821
  • 2
  • 25
  • 26
67
votes
6 answers

Comparison of Clojure web frameworks

There are a few web frameworks for Clojure Compojure Webjure Conjure Moustache and also some libraries for dealing with certain web development subtasks, such as Enlive for templating Hiccup for templating Ring to handle lower level stuff with…
Adam Schmideg
  • 10,590
  • 10
  • 53
  • 83
67
votes
2 answers

Clojure XML Parsing

I can not find any info on how to parse xml documents and access elements. I have found two ways to parse the xml document (clojure.zip/xml-zip (clojure.xml/parse file)) and (parse-seq file) but i can seem to find any info on how to process the…
Hamza Yerlikaya
  • 49,047
  • 44
  • 147
  • 241
66
votes
7 answers

Listing files in a directory in Clojure

How can I create a list out of all of the files in a specific directory in Clojure? Do I have to resort to calling Java or can Clojure handle this natively?
Mike Crittenden
  • 5,779
  • 6
  • 47
  • 74
65
votes
5 answers

Put an element to the tail of a collection

I find myself doing a lot of: (concat coll [e]) where coll is a collection and e a single element. Is there a function for doing this in Clojure? I know conj does the job best for vectors but I don't know up front which coll will be used. It could…
Michiel Borkent
  • 34,228
  • 15
  • 86
  • 149
65
votes
3 answers

How can I convert a LazySeq of Characters to a String in Clojure?

Let's say I have a LazySeq of java.lang.Character like (\b \ \! \/ \b \ \% \1 \9 \/ \. \i \% \$ \i \space \^@) How can I convert this to a String? I've tried the obvious (String. my-char-seq) but it throws java.lang.IllegalArgumentException: No…
Robert Campbell
  • 6,848
  • 12
  • 63
  • 93
64
votes
6 answers

How do you change the CLASSPATH within Java?

How do you change the CLASSPATH of a Java process from within the Java process? Before you ask me "Why would you want to do that?" I'll explain it shortly. When you have a Clojure REPL running it is common to need more jars in your CLASSPATH to…
Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
64
votes
10 answers

Creating a UUID from a string with no dashes

How would I create a java.util.UUID from a string with no dashes? "5231b533ba17478798a3f2df37de2aD7" => #uuid "5231b533-ba17-4787-98a3-f2df37de2aD7"
yayitswei
  • 4,587
  • 5
  • 27
  • 34
63
votes
2 answers

How to access static inner Java class via Clojure interop?

Basically what I need to do is this FileChannel.MapMode.READ_ONLY I tried doing the obvious (.. FileChannel MapMode READ_ONLY) but that ends up throwing an exception java.lang.NoSuchFieldException: MapMode even the / notation specified as for…
Jakub Arnold
  • 85,596
  • 89
  • 230
  • 327
63
votes
3 answers

How do I stop jetty server in clojure?

I am writing a web application using ring and clojure. I am using the jetty adapter for the development server and emacs/SLIME for IDE. While wrap-reload does help, run-jetty blocks my slime session and I would like to be able to start/stop it at…
Mad Wombat
  • 14,490
  • 14
  • 73
  • 109
63
votes
10 answers

What's the easiest way to parse numbers in clojure?

I've been using java to parse numbers, e.g. (. Integer parseInt numberString) Is there a more clojuriffic way that would handle both integers and floats, and return clojure numbers? I'm not especially worried about performance here, I just want…
Rob Lachlan
  • 14,289
  • 5
  • 49
  • 99
63
votes
6 answers

Any Real-World Experience Using Software Transactional Memory?

It seems that there has been a recent rising interest in STM (software transactional memory) frameworks and language extensions. Clojure in particular has an excellent implementation which uses MVCC (multi-version concurrency control) rather than a…
Daniel Spiewak
  • 54,515
  • 14
  • 108
  • 120