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
2 answers

How do Lisp (Clojure) and Tcl compare in terms of abstraction and metaprogramming abilities?

Both seem to be good for building extensible API's and code generation. What are the main differences between them? What do you see as their strengths, weaknesses, ...
StackedCrooked
  • 34,653
  • 44
  • 154
  • 278
10
votes
4 answers

Can clojure be made fully dynamic?

In clojure 1.1, all calls were dynamic, meaning that you could redefine a function at the REPL and it would be included in the running program automatically. This was also nice for things like dotrace. In clojure 1.2, many calls seem to be…
John Lawrence Aspden
  • 17,124
  • 11
  • 67
  • 110
10
votes
1 answer

weirdness in clojure map function

the first strange thing about map in clojure is in following snippet: (apply map list '((1 a) (2 b) (3 c))) The result is surprising for me: ((1 2 3) (a b c)) Anyone could explain how it works?
Dfr
  • 4,075
  • 10
  • 40
  • 63
10
votes
2 answers

Is defn thread-safe?

Can I redefine function in real-time without side effects? Is defn thread-safe?
Kirill Trofimov
  • 1,218
  • 1
  • 14
  • 26
10
votes
6 answers

Is it a bad idea to put functions in Clojure maps, like in JavaScript?

I'm new to Clojure and finding my sea legs. I was wondering whether it is considered good or bad practice, from a functional programming standpoint, to put functions in Clojure maps and then pass those maps around like quasi-objects, as is often…
dan
  • 43,914
  • 47
  • 153
  • 254
10
votes
1 answer

Scientific dataset manipulation in Clojure -- reading ByteBuffers into matrices

I'm looking to use Clojure and Incanter for processing of a large scientific dataset; specifically, the 0.5 degree version of this dataset (only available in binary format). My question is, what recommendations do you have for elegant ways to deal…
Sam Ritchie
  • 10,988
  • 4
  • 42
  • 53
10
votes
3 answers

Lambda expressions of .NET in Java

I recently moved from C# to Java [again]. But I badly miss lambda expressions and things like IEnumerable.Foreach of C#. So I am looking for a lambda expression library in Java. are there better libraries than LambdaJ? Also is clojure directly…
Fakrudeen
  • 5,778
  • 7
  • 44
  • 70
10
votes
3 answers

How do I find implemented protocols in Clojure object?

Is there a documented way to find which protocols are implemented by a Clojure object? The other way around (show for which classes a given protocol is extended) is easy: (extenders protocol).
Maurits Rijk
  • 9,789
  • 2
  • 36
  • 53
10
votes
2 answers

How to replace the last element in a vector in Clojure

As a newbie to Clojure I often have difficulties to express the simplest things. For example, for replacing the last element in a vector, which would be v[-1]=new_value in python, I end up with the following variants in Clojure: (assoc v (dec…
ead
  • 32,758
  • 6
  • 90
  • 153
10
votes
3 answers

Is there some lispy language that seamlessly integrates with Python?

Is there a language based on S-expressions with powerful macros that allows as seamless integration with Python as Clojure with JVM? I want to try using such syntax and features while having access to all usual python libraries (including PyQt).
Vi.
  • 37,014
  • 18
  • 93
  • 148
10
votes
1 answer

What are the leiningen default repositories?

Leiningen (https://github.com/technomancy/leiningen) looks into some default repositories to satisfy the dependencies specified in your project.clj. I want to browse these repositories to see what's available out-of-the-box in leiningen. What are…
Bernhard Kausler
  • 5,119
  • 3
  • 32
  • 36
10
votes
1 answer

How can I define something belonging to another namespace in clojure?

I have a clj file using a certain namespace and I wish to define something belonging to another namespace, so I do : (def other.namespace/name-of-something "value") : but when I do this I get the result : java.lang.Exception: Can't refer to…
yazz.com
  • 57,320
  • 66
  • 234
  • 385
10
votes
3 answers

How can I test to see if a function has side effects in Clojure?

Is there a function or a macro where I can do the following: (has-side-effects? my-function my-function-args) : or something like it which can return true or false, or {:side-effects true/false, :result return_value) I just need an easy way to…
yazz.com
  • 57,320
  • 66
  • 234
  • 385
10
votes
1 answer

What are the tasks of the "reader" during Lisp interpretation?

I'm wondering about the purpose, or perhaps more correctly, the tasks of the "reader" during interpretation/compilation of Lisp programs. From the pre-question-research I've just done, it seems to me that a reader (particular to Clojure in this…
Andrew
  • 103
  • 4
10
votes
3 answers

Clojure: Assigning defrecord fields from Map

Following up on How to make a record from a sequence of values, how can you write a defrecord constructor call and assign the fields from a Map, leaving un-named fields nil? (defrecord MyRecord [f1 f2 f3]) (assign-from-map MyRecord {:f1 "Huey" :f2…
Ralph
  • 31,584
  • 38
  • 145
  • 282