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
80
votes
5 answers

Building a Clojure app with a command-line interface?

I just started w/ Clojure (coming from Ruby) and I would like to build an small app with a command-line interface. How do I handle input/output to a CL? I noticed that there is a clojure.contrib.command-line, but documentation is…
kush
  • 16,408
  • 17
  • 48
  • 65
79
votes
3 answers

How to read mentally Lisp/Clojure code

Thanks a lot for all the beautiful answers! Cannot mark just one as correct Note: Already a wiki I am new to functional programming and while I can read simple functions in Functional programming, for e.g. computing the factorial of a number, I am…
user855
  • 19,048
  • 38
  • 98
  • 162
78
votes
6 answers

Clojure on Android

Is it possible to program Android apps in Clojure? Can anybody suggest a good tutorial or book, if it is possible?
JaneNY
  • 781
  • 1
  • 5
  • 3
77
votes
5 answers

Private def in clojure/clojurescript

In Clojure and clojurescript you can have a private version of defn called defn-, but how do you do the same for def, as def- doesn't seem to be included?
yazz.com
  • 57,320
  • 66
  • 234
  • 385
76
votes
4 answers

How to Iterate over Map Keys and Values in Clojure?

I have the following map which I want to iterate: (def db {:classname "com.mysql.jdbc.Driver" :subprotocol "mysql" :subname "//100.100.100.100:3306/clo" :username "usr" :password "pwd"}) I've tried the following, but…
sjac
  • 2,811
  • 5
  • 23
  • 20
76
votes
7 answers

What is the difference between ; and ;; in Clojure code comments?

What is the difference between ; and ;; when starting a comment in Clojure? I see that my text editor colours them differently, so I'm assuming there is notionally some difference. I also see that Marginalia treats them differently: ; Stripped…
pauldoo
  • 18,087
  • 20
  • 94
  • 116
76
votes
4 answers

When should I use Datomic?

I'm intrigued in the database service Datomic, but I'm not sure if it fits the needs of the projects I work on. When is Datomic a good choice, and when should it be avoided?
Mantas Vidutis
  • 16,376
  • 20
  • 76
  • 92
76
votes
6 answers

How to compare two functions for extensional equivalence, as in (λx.2*x) == (λx.x+x)?

Is there a way to compare two functions for equality? For example, (λx.2*x) == (λx.x+x) should return true, because those are obviously equivalent.
MaiaVictor
  • 51,090
  • 44
  • 144
  • 286
75
votes
5 answers

Where should I use defrecord in clojure?

I use many maps and structs in my clojure programs. What are the benefits (apart from performance) of converting these to defrecords?
yazz.com
  • 57,320
  • 66
  • 234
  • 385
74
votes
17 answers

Which Lisp should I learn?

Which Lisp (dialect) should I learn, and why? The fragmentation between CL and Scheme slows uptake (at least for me!). So, give me the "true answer", please! I have tried to read feature comparisons, and they seem to get bogged down in esoterica…
Gregg Lind
  • 20,690
  • 15
  • 67
  • 81
74
votes
5 answers

Clojure: rest vs. next

I'm having a hard time understanding the difference between rest and next in Clojure. The official site's page on laziness indicates that the preference should probably be to use rest, but it doesn't really explain clearly the difference between the…
Daniel Yankowsky
  • 6,956
  • 1
  • 35
  • 39
73
votes
3 answers

In Clojure 1.4 what is the use of refer within require?

What advantage does using :refer in :require have over using :only in :use? Are the following synonymous? (ns so.example (:use [my.lib :only [function]])) and (ns so.example (:require [my.lib :refer [function]]))
Andrew
  • 7,286
  • 3
  • 28
  • 38
72
votes
3 answers

Let vs. Binding in Clojure

I understand that they're different since one works for setting *compile-path* and one doesn't. However, I need help with why they're different. let creates a new scope with the given bindings, but binding...?
Carl
  • 887
  • 1
  • 7
  • 6
70
votes
6 answers

How can I get the methods of a Java class from Clojure?

How can I get the methods of a Java class from Clojure?
yazz.com
  • 57,320
  • 66
  • 234
  • 385
69
votes
13 answers

Editing programs "while they are running"? Why?

I've been getting more into Lisp and Lispy languages lately, and I'm finding them quite powerful. One thing I've been reading all over the net is that a benefit of writing in Lisp, Clojure, etc, is that you can edit your program "while it's…
MikeC8
  • 3,783
  • 4
  • 27
  • 33