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

Clojure Protocols vs Scala Structural Types

After watching the interview with Rich Hickey on Protocols in Clojure 1.2, and knowing very little about Clojure, I have some questions on Clojure Protocols: Are they intended to do the same thing as Structural Types in Scala? What benefits do…
Vasil Remeniuk
  • 20,519
  • 6
  • 71
  • 81
10
votes
4 answers

How to reload a multimethod in Clojure REPL

I am writing a multimethod in the REPL, functions can be redefined just fine, but if I redefine the dispatch function of a multi method it seems not to use the newly refined function: ;; simple fn to resolve defmethod to call, hardcoded to…
Kris
  • 19,188
  • 9
  • 91
  • 111
10
votes
1 answer

How to wait for a key press in Clojure

I'd like to break out of a loop when the user presses a key. In C I'd use kbhit(). Is there a Clojure (or Java) equivalent?
justinhj
  • 11,147
  • 11
  • 58
  • 104
10
votes
1 answer

Why recur from catch section in Clojure is illegal

There already at least one answered question regarding recur on exception. My question is why this recur is not accepted by Clojure compiler (loop [] (try (catch Exception _ex (recur)))) with error "Can only recur from tail…
Petr Gladkikh
  • 1,906
  • 2
  • 18
  • 31
10
votes
3 answers

emacs setup for both clojure and common lisp with slime-fancy (slime-autodoc)

I set up emacs for both clojure and common lisp, but I want also (slime-setup '(slime-fancy)) for common lisp. If I add that line to init.el, clojure won't work: it gives me repl, but it hangs after I run any code. My configuration For clojure: I…
koddo
  • 3,328
  • 2
  • 26
  • 25
10
votes
1 answer

Clojure, can macros do something that couldn't be done with a function

I'm learning Clojure macros, and wonder why we can't use just functions for metaprogramming. As far as I know the difference between macro and function is that arguments of macro are not evaluated but passed as data structures and symbols as they…
Tuomas Toivonen
  • 21,690
  • 47
  • 129
  • 225
10
votes
2 answers

Lispy way to read user input from the keyboard in Clojure?

I am writing a function for my Clojure program that reads user input from the keyboard. If the user enters invalid input, the user is warned and then prompted again. When using a procedural style in a language like Python, I would do something…
davidscolgan
  • 7,508
  • 9
  • 59
  • 78
10
votes
1 answer

How do I get emacs cider (clojure mode) to use my test environment variables when running tests?

I've set up separate database-url for development and test environments, and this works nicely when running my webapp in the REPL and from the lein test on the command line. Here's my profiles.clj: {:profiles/dev {:env {:database-url "wiki"}} …
Eric Clack
  • 1,886
  • 1
  • 15
  • 28
10
votes
1 answer

Clojure Spec Dependent fields

I have a record and for this spec and I want to generate values but ensure that the current amount does not exceed the max amount. A simplified spec would be something like: (s/def ::max-amt (s/and number? #(<= 0 % 1e30))) (s/def ::cur-amt (s/and…
Phil Cooper
  • 5,747
  • 1
  • 25
  • 41
10
votes
5 answers

Will reading the Little Lisper help me in learning clojure?

I plan to pick up the clojure language. I have at my disposal an old book: The Little Lisper I know there are more recent versions of it (The Little Schemer) but I'm wondering if it will help ease me into picking up Clojure. Or should I find other…
Frankie Ribery
  • 11,933
  • 14
  • 50
  • 64
10
votes
4 answers

What is the Clojure equivalent of __FILE__ (found in Ruby & Perl)

In ruby I frequently use File.expand_path(File.dirname(__FILE__)) for loading config files or files with test data. Right now I'm trying to load some html files for a test in my clojure app and I can't figure out how to do it without hard coding the…
jshen
  • 11,507
  • 7
  • 37
  • 59
10
votes
3 answers

Compojure binds HTTP request params from URL, but not from a POST form

Compojure does not bind the fields in a POST form. This is my route def: (defroutes main-routes (POST "/query" {params :params} (debug (str "|" params "|")) "OK...") ) When I post a form with fields in it, I get |{}|, i.e. there are no…
George
  • 3,433
  • 4
  • 27
  • 25
10
votes
2 answers

Convert BufferedInputStream to String in Clojure

mock.request is returning the response :body as a BufferedInputStream. I need to print and compare this as a string. How do I convert it? When I try to pass response as a message to my assertion, I see a raw output, e.g. (is (= 200 (:status…
Petrus Theron
  • 27,855
  • 36
  • 153
  • 287
10
votes
2 answers

What is the specific difference between Clojure 1.8's socket repl and nREPL?

I've been reading this, but there seems to be no tutorial/doc about the difference between these two or whatnot. Is socket repl going to replace nrepl?
Shiva Wu
  • 1,074
  • 1
  • 8
  • 20
10
votes
3 answers

clojure swank server opens public port?

(This question has been downvoted, which I find strange. How have I offended?) Am I right to think that running a swank server usually opens port 4005 to the world, not bound to localhost-only connections? So anyone hacking in a café is not only…
John Lawrence Aspden
  • 17,124
  • 11
  • 67
  • 110
1 2 3
99
100