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
676
votes
6 answers

Scala vs. Groovy vs. Clojure

Can someone please explain the major differences between Scala, Groovy and Clojure. I know each of these compiles to run on the JVM but I'd like a simple comparison between them.
James Fassett
  • 40,306
  • 11
  • 38
  • 43
298
votes
1 answer

How can I make nrepl-ritz-jack-in work remotely over TRAMP / Emacs

What I want: I have a clojure program on a remote site, let's call it mccarthy. What I want to do is connect to a nrepl-ritz from my laptop, preferably using nrepl-ritz-jack-in. The jack in works fine for a local program, but doesn't seem to…
MattoxBeckman
  • 3,682
  • 2
  • 20
  • 16
235
votes
14 answers

Debugging in Clojure?

What are best ways to Debug Clojure code, while using the repl?
Arun R
  • 8,372
  • 6
  • 37
  • 46
219
votes
16 answers

How do you make a web application in Clojure?

I suppose this is a strange question to the huge majority of programmers that work daily with Java. I don't. I know Java-the-language, because I worked on Java projects, but not Java-the-world. I never made a web app from scratch in Java. If I have…
Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
205
votes
13 answers

Is there a software-engineering methodology for functional programming?

Software Engineering as it is taught today is entirely focused on object-oriented programming and the 'natural' object-oriented view of the world. There is a detailed methodology that describes how to transform a domain model into a class model with…
Thorsten
  • 3,451
  • 3
  • 20
  • 25
194
votes
19 answers

Test whether a list contains a specific value in Clojure

What is the best way to test whether a list contains a given value in Clojure? In particular, the behaviour of contains? is currently confusing me: (contains? '(100 101 102) 101) => false I could obviously write a simple function to traverse the…
mikera
  • 105,238
  • 25
  • 256
  • 415
191
votes
8 answers

How to reload a clojure file in REPL

What is the preferred way of reloading functions defined in a Clojure file without having to restart the REPL. Right now, in order to use the updated file I have to: edit src/foo/bar.clj close the REPL open the REPL (load-file…
pkaleta
  • 2,129
  • 2
  • 13
  • 9
176
votes
9 answers

Calling clojure from java

Most of the top google hits for "calling clojure from java" are outdated and recommend using clojure.lang.RT to compile the source code. Could you help with a clear explanation of how to call Clojure from Java assuming you have already built a jar…
Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284
172
votes
6 answers

In Clojure 1.3, How to read and write a file

I'd like to know the "recommended" way of reading and writing a file in clojure 1.3 . How to read the whole file How to read a file line by line How to write a new file How to add a line to an existing file
jolly-san
  • 2,047
  • 2
  • 14
  • 9
166
votes
5 answers

In Clojure, when should I use a vector over a list, and the other way around?

I read that Vectors are not seqs, but Lists are. I'm not sure what the rationale is for using one over the other. It seems that vectors are used the most, but is there a reason for that?
Rayne
  • 31,473
  • 17
  • 86
  • 101
163
votes
4 answers

difference between use and require

Can anyone explain the difference between use and require, both when used directly and as :use and :require in the ns macro?
Jegschemesch
  • 11,414
  • 4
  • 32
  • 37
155
votes
17 answers

What is the best way to do GUIs in Clojure?

What is the best way to do GUIs in Clojure? Is there an example of some functional Swing or SWT wrapper? Or some integration with JavaFX declarative GUI description which could be easily wrapped to s-expressions using some macrology? Any tutorials?
Marko
  • 30,263
  • 18
  • 74
  • 108
151
votes
12 answers

Mapping a function on the values of a map in Clojure

I want to transform one map of values to another map with the same keys but with a function applied to the values. I would think there was a function for doing this in the clojure api, but I have been unable to find it. Here's an example…
Thomas
  • 2,769
  • 5
  • 21
  • 21
150
votes
2 answers

Simple explanation of clojure protocols

I'm trying to understand clojure protocols and what problem they are supposed to solve. Does anyone have a clear explanation of the whats and whys of clojure protocols?
yazz.com
  • 57,320
  • 66
  • 234
  • 385
150
votes
4 answers

Please explain some of Paul Graham's points on Lisp

I need some help understanding some of the points from Paul Graham’s What Made Lisp Different. A new concept of variables. In Lisp, all variables are effectively pointers. Values are what have types, not variables, and assigning or binding…
unj2
  • 52,135
  • 87
  • 247
  • 375
1
2 3
99 100