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
116
votes
12 answers

Can someone explain Clojure Transducers to me in Simple terms?

I have tried reading up on this but I still don't understand the value of them or what they replace. And do they make my code shorter, more understandable or what? Update Alot of people posted answers, but it would be nice to see examples of with…
yazz.com
  • 57,320
  • 66
  • 234
  • 385
114
votes
2 answers

What is the difference between Lisp-1 and Lisp-2?

I have tried to understand the difference between Lisp-1 and Lisp-2 and how this relates to Clojure but I still do not understand properly. Can anyone enlighten me?
yazz.com
  • 57,320
  • 66
  • 234
  • 385
114
votes
5 answers

How to convert a clojure keyword into a string?

In my application I need to convert clojure keyword eg. :var_name into a string "var_name". Any ideas how that could be done?
Santosh
  • 1,928
  • 2
  • 15
  • 13
113
votes
5 answers

What is Clojure useful for?

What real world applications are people using Clojure for? I use Groovy for glue stuff, Java for big systems, Python/Perl scripts for parsing and glue. I could see myself calling into Clojure from Java but I can't see where I would use it in its…
Fortyrunner
  • 12,702
  • 4
  • 31
  • 54
113
votes
5 answers

What's the "big idea" behind compojure routes?

I'm new to Clojure and have been using Compojure to write a basic web application. I'm hitting a wall with Compojure's defroutes syntax, though, and I think I need to understand both the "how" and the "why" behind it all. It seems like a Ring-style…
Sean Woods
  • 2,514
  • 3
  • 18
  • 24
108
votes
5 answers

Clojure: cons (seq) vs. conj (list)

I know that cons returns a seq and conj returns a collection. I also know that conj "adds" the item to the optimal end of the collection, and cons always "adds" the item to the front. This example illustrates both of these points: user=> (conj [1…
dbyrne
  • 59,111
  • 13
  • 86
  • 103
105
votes
5 answers

How to convert lazy sequence to non-lazy in Clojure

I tried the following in Clojure, expecting to have the class of a non-lazy sequence returned: (.getClass (doall (take 3 (repeatedly rand)))) However, this still returns clojure.lang.LazySeq. My guess is that doall does evaluate the entire…
Tim Clemons
  • 6,231
  • 4
  • 25
  • 23
103
votes
1 answer

Clojure 1.2.1/1.3/1.4 'proxy generated in Grails 2.0.0 runtime fails. 1.2.0 is fine

I'm working on extending the Grails Clojure plugin in Grails 2.0.0 (and 2.1.0-SNAPSHOT) and I wanted to update it to Clojure 1.3.0 and add clojure.tools.logging. Clojure throws an exception during compilation of a proxy of a ByteArrayOutputStream…
John Courtland
  • 1,055
  • 1
  • 7
  • 10
103
votes
4 answers

Clojure vs other Lisps

The intent of my question is not to start a flame war, but rather to determine in what circumstances each language is "the best tool for the job." I have read several books on Clojure (Programming Clojure, Practical Clojure, The Joy of Clojure, and…
Ralph
  • 31,584
  • 38
  • 145
  • 282
98
votes
1 answer

Splitting a Clojure namespace over multiple files

Is it possible to split a Clojure namespace over multiple source files when doing ahead-of-time compilation with :gen-class? How do (:main true) and (defn- ...) come into play?
Ralph
  • 31,584
  • 38
  • 145
  • 282
98
votes
14 answers

Medium-size Clojure sample application?

Is there a medium-sized Clojure sample application that could be used as a "best-practices" example, and a good way to see what such an application would look like in terms of code and code organization? A web application would be particularly…
foxdonut
  • 7,779
  • 7
  • 34
  • 33
97
votes
3 answers

Java to Clojure rewrite

I have just been asked by my company to rewrite a largish (50,000 single lines of code) Java application (a web app using JSP and servlets) in Clojure. Has anyone else got tips as to what I should watch out for? Please bear in mind that I know both…
yazz.com
  • 57,320
  • 66
  • 234
  • 385
96
votes
6 answers

In Clojure is there an easy way to convert between list types?

I am often finding myself using a lazy list when I want a vector, and vice versa. Also, sometimes I have a vector of maps, when I really wanted a set of maps. Are there any helper functions to help me to convert between these types?
yazz.com
  • 57,320
  • 66
  • 234
  • 385
94
votes
9 answers

How do I find the index of an item in a vector?

Any ideas what ???? should be? Is there a built in? What would be the best way to accomplish this task? (def v ["one" "two" "three" "two"]) (defn find-thing [ thing vectr ] (????)) (find-thing "two" v) ; ? maybe 1, maybe '(1,3), actually…
John Lawrence Aspden
  • 17,124
  • 11
  • 67
  • 110
92
votes
8 answers

Common programming mistakes for Clojure developers to avoid

What are some common mistakes made by Clojure developers, and how can we avoid them? For example; newcomers to Clojure think that the contains? function works the same as java.util.Collection#contains. However, contains? will only work similarly…
fogus
  • 6,126
  • 5
  • 36
  • 42