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

Namespace confusion and macros

I want to write a macro that uses functions from the clj-time library. In one namespace I would like to call the macro like this: (ns budget.account (:require [budget.time])) (budget.time/next-date interval frequency) The next-date macro would…
user1265564
  • 147
  • 4
10
votes
2 answers

Spring-managed Clojure beans

I'm just wondering if it's possible to create Spring managed beans backed by Clojure code (Clojure namespace I guess, would implement regular Java interface)? If so, the example would be appreciated. Thanks, Dmitriy.
10
votes
4 answers

In Clojure, how to group elements?

In clojure, I want to aggregate this data: (def data [[:morning :pear][:morning :mango][:evening :mango][:evening :pear]]) (group-by first data) ;{:morning [[:morning :pear][:morning :mango]],:evening [[:evening :mango][:evening :pear]]} My problem…
viebel
  • 19,372
  • 10
  • 49
  • 83
10
votes
1 answer

Simple GUI Toolkits For Use With Clojure

The long and short of this question is which GUI tool kits create very simple UIs and are easy to use work with Clojure? Here are further details to narrow down my question. I want to create very simple GUI applications that will run on Windows,…
octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
10
votes
2 answers

How to invoke superclass' method in a Clojure gen-class method?

I'm trying to create a class that extends input stream Clojure via gen-class. If I want to invoke the parent class' method, how do I do that?
BillRobertson42
  • 12,602
  • 4
  • 40
  • 57
10
votes
2 answers

Clojure Model-View-Controller (MVC) design

I am writing a Desktop GUI application in Clojure using Java Swing. Normally when working with Java I will design the application according to a MVC design pattern using the Observer pattern as well. This way the view is decoupled from the model and…
10
votes
1 answer

Destroy variable in clojure

When using the repl, sometimes you want to destroy a variable because it somehow get in the way of your programming (most usually namespace collisions). Is there a way to destroy a variable in clojure? user>(def x…
claj
  • 5,172
  • 2
  • 27
  • 30
10
votes
2 answers

paredit curly brace matching in swank-clojure repl

I am using emacs 24 on Windows 7 and have installed technomancy's clojure-mode along with paredit 23 beta. I load the source file from my leiningen project and get a repl using clojure-jack-in. The problem is that while paredit is enabled in both…
pventura
  • 117
  • 6
10
votes
3 answers

What makes (open) Dylan distinct from other programming languages?

Nowadays there are so many programming languages out there: functional, object oriented, dynamically or statically typed, or mixes of these characteristics. Recently I noticed this release: http://opendylan.org/news/2011/12/10/new_release.html Open…
Michiel Borkent
  • 34,228
  • 15
  • 86
  • 149
10
votes
3 answers

How to use leiningen to develop using local jars?

I realize that this question is pretty much the exact question found here. However, seeing as that question is 1.5 years old (or so), I would like to revisit it. How does one add local dependencies using leiningen? Surely this capability must exist…
benekastah
  • 5,651
  • 1
  • 35
  • 50
10
votes
4 answers

Rotate a list-of-list matrix in Clojure

I'm new to Clojure and functional programming in general. I'm at a loss in how to handle this in a functional way. I have the following matrix: (def matrix [[\a \b \c] [\d \e \f] [\g \h \i]]) I want to transform it into…
Martijn
  • 1,662
  • 15
  • 21
10
votes
1 answer

How are clojure namespaces looked up?

To start off I am not a Java programmer, so it would be helpful if your answers are not defined in terms of Java (inasmuch as that makes sense). I have a leiningen project (specifically a web project using noir) using what seems to be a common…
benekastah
  • 5,651
  • 1
  • 35
  • 50
10
votes
1 answer

Function definition of Clojure built-ins: def vs defn

Why are some (many) of Clojure's built-in functions defined using def, not defn? I know that defn is a shortcut for def, but I'm wondering if there's some other reason such as efficiency/readability/style considerations/bootstrapping.
Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
10
votes
1 answer

Leiningen has problems building a working uberjar

We're trying to build our Clojure project with Leiningen. We've succeeded in creating an uberjar by doing the following: preconditions: project.clj file lists dependencies :main my-project.core in project.clj a core.clj file with a -main…
Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
10
votes
4 answers

How do I expect failure in a unit test?

We're writing unit tests for our code in Clojure using clojure.test. Some of our tests ignore the API and purposely break the code, in order to serve as documentation for latent deficiencies in the code. However, we want to distinguish between…
Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192