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

16902 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
  • 36,630
  • 11
  • 33
  • 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,654
  • 2
  • 18
  • 16
232
votes
14 answers

Debugging in Clojure?

What are best ways to Debug Clojure code, while using the repl?
Arun R
  • 8,262
  • 5
  • 35
  • 45
216
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…
pupeno
  • 256,034
  • 114
  • 324
  • 541
203
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,411
  • 3
  • 18
  • 23
178
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
  • 1,999
  • 2
  • 12
  • 9
175
votes
18 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
  • 101,777
  • 23
  • 241
  • 402
169
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
  • 87,736
  • 24
  • 197
  • 278
165
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
  • 1,977
  • 2
  • 13
  • 9
159
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
  • 10,804
  • 4
  • 28
  • 35
153
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
  • 28,305
  • 16
  • 83
  • 100
152
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
  • 28,383
  • 17
  • 70
  • 107
148
votes
38 answers

Lisp in the real world

I have experimented with Lisp (actually Scheme) and found it to be a very beautiful language that I am interested in learning more about. However, it appears that Lisp is never used in serious projects, and I haven't seen it listed as a desired…
titaniumdecoy
  • 17,909
  • 17
  • 93
  • 130
147
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
  • 47,759
  • 80
  • 235
  • 362
142
votes
12 answers

Why exactly is eval evil?

I know that Lisp and Scheme programmers usually say that eval should be avoided unless strictly necessary. I’ve seen the same recommendation for several programming languages, but I’ve not yet seen a list of clear arguments against the use of eval.…
Jay
  • 9,293
  • 6
  • 44
  • 67
1
2 3
99 100