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

How do Clojure futures and promises differ?

Both futures and promises block until they have calculated their values, so what is the difference between them?
yazz.com
  • 57,320
  • 66
  • 234
  • 385
91
votes
6 answers

When I divide numbers in clojure I get a fraction , how do I get the decimal?

When I do (/ 411 125) , I don't get it in terms of decimal. How do I do that?
unj2
  • 52,135
  • 87
  • 247
  • 375
90
votes
3 answers

What is the :: used for in clojure?

I understand keywords in Clojure being :keyword. But what is the :: used for? Why does it look like it has a binding? user=> :foo :foo user=> ::foo :user/foo
carinmeier
  • 1,351
  • 1
  • 8
  • 9
90
votes
19 answers

Is functional programming relevant to web development?

I've been seeing so much recently about functional programming and Clojure looks particularly interesting. While I 'understand' the basic description of what it is, I can't figure out how I would use it on a day to day basis as a web developer, if I…
Hates_
  • 66,613
  • 6
  • 32
  • 37
90
votes
13 answers

Interpreting a benchmark in C, Clojure, Python, Ruby, Scala and others

Disclaimer I know that artificial benchmarks are evil. They can show results only for very specific narrow situation. I don't assume that one language is better than the other because of the some stupid bench. However I wonder why results is so…
defhlt
  • 1,775
  • 2
  • 17
  • 24
89
votes
5 answers

How to make a Clojure function take a variable number of parameters?

I'm learning Clojure and I'm trying to define a function that take a variable number of parameters (a variadic function) and sum them up (yep, just like the + procedure). However, I don´t know how to implement such function Everything I can do…
rodrigoalvesvieira
  • 7,895
  • 15
  • 63
  • 84
88
votes
2 answers

Why does Clojure have 5 ways to define a class instead of just one?

Clojure has gen-class, reify, proxy and also deftype and defrecord to define new class-like datatypes. For a language that values syntactic simplicity and abhors unnecessary complexity, it seems like an aberration. Could someone explain why it is…
Salil
  • 9,534
  • 9
  • 42
  • 56
88
votes
6 answers

Generating permutations lazily

I'm looking for an algorithm to generate permutations of a set in such a way that I could make a lazy list of them in Clojure. i.e. I'd like to iterate over a list of permutations where each permutation is not calculated until I request it, and all…
Brian Carper
  • 71,150
  • 28
  • 166
  • 168
88
votes
5 answers

A gentle tutorial to Emacs/Swank/Paredit for Clojure

I am moving to Emacs to work on Clojure/Lisp. What is all the information I need to setup on Emacs to be able to do the following? automatic matching/generation of corresponding closing brackets autoindent Lisp/Clojure style, not C++/Java…
user855
  • 19,048
  • 38
  • 98
  • 162
86
votes
4 answers

Clojure - named arguments

Does Clojure have named arguments? If so, can you please provide a small example of it?
one-zero-zero-one
  • 1,570
  • 1
  • 11
  • 15
85
votes
4 answers

Anonymous function shorthand

There's something I don't understand about anonymous functions using the short notation #(..) The following works: REPL> ((fn [s] s) "Eh") "Eh" But this doesn't: REPL> (#(%) "Eh") This works: REPL> (#(str %) "Eh") "Eh" What I don't understand…
Cedric Martin
  • 5,945
  • 4
  • 34
  • 66
84
votes
6 answers

How many primitives does it take to build a LISP machine? Ten, seven or five?

On this site they say there are 10 LISP primitives. The primitives are: atom, quote, eq, car, cdr, cons, cond, lambda, label, apply. http://hyperpolyglot.wikidot.com/lisp#ten-primitives Stevey reckons there are seven (or five): Its part of the…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
83
votes
10 answers

How can I run a .clj Clojure file I created?

I wrote a small "hello world" program on Linux to get started with the language. Now I'd like to run it and see what it outputs. How can I run this code? What command should I run?
Only Bolivian Here
  • 35,719
  • 63
  • 161
  • 257
83
votes
8 answers

On Performance and Java Interoperability: Clojure vs. Scala

I have already read various accounts of Clojure vs. Scala and while I realize that both have their place. There are a few considerations that I haven't acquired a complete explanation on when it comes to comparing both Clojure with Scala: 1.) Which…
Ryan Delucchi
  • 7,718
  • 13
  • 48
  • 60
81
votes
4 answers

How to list the functions of a namespace?

I would like to know how to list all functions of a Clojure namespace. I've done some research but I'm not there yet. I already found out how to list the methods of a Java class using the show method: (show java.awt.Graphics) To list the functions…
StackedCrooked
  • 34,653
  • 44
  • 154
  • 278