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
149
votes
4 answers

What are the differences between Clojure, Scheme/Racket and Common Lisp?

I know they are dialects of the same family of language called lisp, but what exactly are the differences? Could you give an overview, if possible, covering topics such as syntax, characteristics, features and resources.
MaiaVictor
  • 51,090
  • 44
  • 144
  • 286
148
votes
13 answers

In Clojure how can I convert a String to a number?

I have various strings, some like "45", some like "45px". How how I convert both of these to the number 45?
yazz.com
  • 57,320
  • 66
  • 234
  • 385
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
  • 18,900
  • 17
  • 96
  • 133
147
votes
7 answers

Is there an equivalent for the Zip function in Clojure Core or Contrib?

In Clojure, I want to combine two lists to give a list of pairs, > (zip '(1 2 3) '(4 5 6)) ((1 4) (2 5) (3 6)) In Haskell or Ruby the function is called zip. Implementing it is not difficult, but I wanted to make sure I wasn't missing a function…
John Kane
  • 3,601
  • 5
  • 23
  • 18
144
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,585
  • 6
  • 49
  • 72
143
votes
6 answers

How to create default value for function argument in Clojure

I come with this: (defn string->integer [str & [base]] (Integer/parseInt str (if (nil? base) 10 base))) (string->integer "10") (string->integer "FF" 16) But it must be a better way to do this.
jcubic
  • 61,973
  • 54
  • 229
  • 402
141
votes
6 answers

Why does Clojure have "keywords" in addition to "symbols"?

I have a passing knowledge of other Lisps (particularly Scheme) from way back. Recently I've been reading about Clojure. I see that it has both "symbols" and "keywords". Symbols I'm familiar with, but not with keywords. Do other Lisps have keywords?…
Laurence Gonsalves
  • 137,896
  • 35
  • 246
  • 299
140
votes
5 answers

A regex to match a substring that isn't followed by a certain other substring

I need a regex that will match blahfooblah but not blahfoobarblah I want it to match only foo and everything around foo, as long as it isn't followed by bar. I tried using this: foo.*(?
Rayne
  • 31,473
  • 17
  • 86
  • 101
134
votes
10 answers

Clojure: reduce vs. apply

I understand the conceptual difference between reduce and apply: (reduce + (list 1 2 3 4 5)) ; translates to: (+ (+ (+ (+ 1 2) 3) 4) 5) (apply + (list 1 2 3 4 5)) ; translates to: (+ 1 2 3 4 5) However, which one is more idiomatic clojure? Does…
dbyrne
  • 59,111
  • 13
  • 86
  • 103
131
votes
15 answers

How to do exponentiation in clojure?

How can I do exponentiation in clojure? For now I'm only needing integer exponentiation, but the question goes for fractions too.
Peter
  • 47,963
  • 46
  • 132
  • 181
128
votes
10 answers

Block Comments in Clojure

How do I comment multiple lines in Clojure?
unj2
  • 52,135
  • 87
  • 247
  • 375
121
votes
11 answers

leiningen - how to add dependencies for local jars?

I want to use leiningen to build and develop my clojure project. Is there a way to modify project.clj to tell it to pick some jars from local directories? I have some proprietary jars that cannot be uploaded to public repos. Also, can leiningen be…
signalseeker
  • 4,100
  • 7
  • 30
  • 36
120
votes
5 answers

Clojure differences between Ref, Var, Agent, Atom, with examples

I'm very new to Clojure, Can you guys give me explanation with real world scenarios. I mean, where to use Ref, Var, Agent, Atom. I read book, but, still couldn't understand the real world examples.
user472557
120
votes
7 answers

How to bundle a native library and a JNI library inside a JAR?

The library in question is Tokyo Cabinet. I want is to have the native library, JNI library, and all Java API classes in one JAR file to avoid redistribution headaches. There seems to be an attempt at this at GitHub, but It does not include the…
Alex B
  • 82,554
  • 44
  • 203
  • 280
119
votes
2 answers

Difference between doseq and for in Clojure

What's the difference between doseq and for in Clojure? What are some examples of when you would choose to use one over the other?
Jeff the Bear
  • 5,603
  • 3
  • 22
  • 18