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

What is the difference between macroexpand and macroexpand-1 in Clojure

I couldn't understand the difference between macroexpand and macroexpand-1. Could you provide examples?
Ertuğrul Çetin
  • 5,131
  • 5
  • 37
  • 76
10
votes
1 answer

Common Lisp's symbol-name in Clojure?

Is there anything in Clojure that is equivalent to Common Lisp's symbol-name function?
Paul Reiners
  • 8,576
  • 33
  • 117
  • 202
10
votes
2 answers

Clojure butlast vs drop-last

What is the difference between butlast and drop-last in Clojure ? Is it only the laziness ? Should I should prefer one over the other ?
nha
  • 17,623
  • 13
  • 87
  • 133
10
votes
1 answer

How to read n lines from a file in clojure

I want to read first n lines from a file using clojure. Here is my code: (defn read-nth-line [file] (with-open [rdr (reader file)] (loop [line-number 0] (when (< line-number 20) (nth (line-seq rdr) line-number) …
Xiufen Xu
  • 531
  • 1
  • 3
  • 19
10
votes
2 answers

How do I make a large number of concurrent HTTPS requests robustly in Clojure (/Java)

I have a stream of inputs and I want to make 2 HTTPS network requests for each before passing the result on to another part of the program. The typical throughput is 50 per second. for each input: HTTP request A HTTP request B pass event…
Joe
  • 46,419
  • 33
  • 155
  • 245
10
votes
3 answers

Scala classes in clojure

It's just a stupid question that I had this morning : Can we use Scala classes from clojure ? Because if the answer is yes, I'll definetly learn Clojure ^^
Aymen
  • 558
  • 6
  • 13
10
votes
3 answers

flip order of passed arguments

Is there a idiomatic / built-in way to flip the argument order that is passed to a function in Clojure? As I do here with the definition of a helper function: (defn flip [f & xs] (apply f (reverse xs))) (vector 1 2) ; [1 2] (flip vector 1…
Anton Harald
  • 5,772
  • 4
  • 27
  • 61
10
votes
2 answers

What is the correct term for the following functional programming pattern?

I've heard it referred to as a stream, as an infinite list, and sometimes even as a lazy sequence. What is the correct term for the following pattern? (Clojure code shown) (def first$ first) (defn second$ [str] (cond (empty? str) () true…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
10
votes
2 answers

How can I start a socket REPL in Clojure 1.8 from leiningen or boot?

In the following link http://clojure.org/reference/repl_and_main#_launching_a_socket_server it has detailed info about how to start socket REPL form java, but since I am using lein, so how to start from lein. If start from boot is good to run, I…
Daniel Wu
  • 5,853
  • 12
  • 42
  • 93
10
votes
2 answers

Use for the identity monad in Clojure

I've been reading an excellent introduction to monads for Clojure programmers. The article illustrates that the Identity monad is functionally equivalent to Clojure's let and that the Sequence/List monad is equivalent to for. When the article gets…
G__
  • 7,003
  • 5
  • 36
  • 54
10
votes
2 answers

Is is possible to destructure a clojure vector into last two items and the rest?

I know I can destructure a vector "from the front" like this: (fn [[a b & rest]] (+ a b)) Is there any (short) way to access the last two elements instead? (fn [[rest & a b]] (+ a b)) ;;Not legal My current alternative is to (fn [my-vector]…
RubenLaguna
  • 21,435
  • 13
  • 113
  • 151
10
votes
3 answers

Handling large datasets in Java/Clojure: littleBig data

I've been working on a graphing/data processing application (you can see a screenshot here) using Clojure (though, oftentimes, it feels like I'm using more Java than Clojure), and have started testing my application with bigger datasets. I have no…
Isaac
  • 15,783
  • 9
  • 53
  • 76
10
votes
3 answers

Calling java functions from Clojure

I can use (.toUpperCase "GOOD") in clojure, as "GOOD" is java string, and java string has toUpperCase method. I also can use (java.io.File/separator) from clojure as a way of calling java functions. But, why can't I call (java.lang/Object wait 3)…
prosseek
  • 182,215
  • 215
  • 566
  • 871
10
votes
1 answer

Why does the Clojure core library use concrete derivation?

In the documentation of Clojure's type mechanisms, it is stated that Concrete derivation is bad you cannot derive datatypes from concrete classes, only interfaces However, some of the core Clojure classes use concrete derivation (there…
Sam Estep
  • 12,974
  • 2
  • 37
  • 75
10
votes
2 answers

Why do we need ' in (require '[...]]) with Clojure?

I see that we don't need ' with (ns ...) as ns is a macro. However, why do we need ' in (require '[...])? I thought that Clojure's vector is a fancy way to avoid ', but now I see one here. We use (require 'clojure.string) so require seems to be a…
prosseek
  • 182,215
  • 215
  • 566
  • 871
1 2 3
99
100