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
10
votes
1 answer

In Clojure why use :only []

The source for lazy-xml has the following: (:use [clojure.xml :as xml :only []] [clojure.contrib.seq :only [fill-queue]]) What is the purpose of using clojure.xml but listing nothing for the :only arguments?
Andrew
  • 7,286
  • 3
  • 28
  • 38
10
votes
1 answer

How to Mock a Java Object in Clojure

I'm exploring clojure.contrib.mock. I think I learned how to mock Clojure functions, but I don't see anything about mocking Java objects. Is there a Clojure library to help me create Java mock objects or will I have to bring in libraries like…
Cristian
  • 42,563
  • 25
  • 88
  • 99
10
votes
2 answers

Dynamically load dependencies in Clojure REPL

Is it possible to download and install previously unspecified Maven dependencies in a running Clojure REPL? I'm thinking of the fairly common case where you want to quickly pull in a dependency temporarily for some testing or visualisation tools,…
mikera
  • 105,238
  • 25
  • 256
  • 415
10
votes
2 answers

How to build robust data apis in clojure?

I find that my clojure apps get structurally coupled very rapidly due to the lack of a data API. I have maps with keys that have names which, if mistyped, because exceptions to be thrown or errors. I also notice that it's easy to make mistakes when…
jayunit100
  • 17,388
  • 22
  • 92
  • 167
10
votes
2 answers

how to parse binary files in Clojure

What is the cleanest way to parse binary data in clojure? I need to be able to read/write equally cleanly to a file or a socket. something like: (read-data source-of-data) => { :index 42 , :block-size 4 , data-size: 31415, :data (1 2 3 4…
Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284
10
votes
2 answers

Inconsistency with Clojure's sequences?

Clojure: 1:13 user=> (first (conj '(1 2 3) 4)) 4 1:14 user=> (first (conj [1 2 3] 4)) 1 ; . . . 1:17 user=> (first (conj (seq [1 2 3]) 4)) 4 I understand what is going on, but should this work differently?
compman
  • 2,174
  • 2
  • 18
  • 26
10
votes
3 answers

clojure filter map by keys

I'm following this example: http://groups.google.com/group/clojure/browse_thread/thread/99b3d792b1d34b56 (see the last reply) And this is the cryptic error that I get: Clojure 1.2.1 user=> (def m {:a "x" :b "y" :c "z" :d "w"}) #'user/m user=>…
Kevin
  • 24,871
  • 19
  • 102
  • 158
10
votes
4 answers

GROUP BY and Aggregation on Vector of maps - Clojure

I have data that looks like this (def a [{:firmAccount "MSFT" :Val 10 :PE 3 } {:firmAccount "MSFT" :Val 15 :PE 4} {:firmAccount "GOG" :Val 15 :PE 3} {:firmAccount "YAH" :Val 8 :PE 1}]) I want to group by on…
Ash
  • 2,531
  • 2
  • 29
  • 38
10
votes
4 answers

Applying multiple filters to a collection in a thrush in Clojure

The following code (let [coll [1 2 3 4 5] filters [#(> % 1) #(< % 5)]] (->> coll (filter (first filters)) (filter (second filters)))) Gives me (2 3 4) Which is great, but how do I apply all the filters in coll without having…
George
  • 3,433
  • 4
  • 27
  • 25
10
votes
5 answers

Javascript to clojure

I am aware of ClojureScript - possibility to compile clojure code to javascript, but is it possible to do the reverse, take some subset of javascript code and translate it back to clojure?
Jiri
  • 16,425
  • 6
  • 52
  • 68
10
votes
2 answers

How to use ClojureScript practically?

I am not getting the idea of ClojureScript. For example, I am writing a web application, and I need to write some javascript. Should I use ClojureScript which will generate the javascript for me? Looking for some guidance. thanks
ruskin
  • 469
  • 9
  • 17
10
votes
1 answer

Inner-join in clojure

Lets say I have (def test-left [{:name "Sean" :age 27} {:name "Ross" :age 27} {:name "Brian" :age 22}]) and (def test-right …
Ash
  • 2,531
  • 2
  • 29
  • 38
10
votes
4 answers

How to call Clojure Macros from Java?

Is there anyway to call Clojure macros from Java? Here is what I am trying to do: RT.var("clojure.core", "require").invoke(Symbol.create("clojure.contrib.prxml")); Var prxml = RT.var("clojure.contrib.prxml", "prxml"); Var withOutStr =…
mudgen
  • 7,213
  • 11
  • 46
  • 46
10
votes
2 answers

Type hinting return value with ^ or :tag meta?

The two seem to be doing the same thing in Clojure. Which syntax is canonical? (defn a ^int [] 4) (defn b ^{:tag int} [] 4) I hope it's a since it's shorter.
Paul Lam
  • 1,729
  • 1
  • 15
  • 25
10
votes
5 answers

Keeping State in a Purely Functional Language

I am trying to figure out how to do the following, assume that your are working on a controller for a DC motor you want to keep it spinning at a certain speed set by the user, (def set-point (ref {:sp 90})) (while true (let [curr (read-speed)] …
Hamza Yerlikaya
  • 49,047
  • 44
  • 147
  • 241