Clojure's standard library, i.e. the clojure.*
namespaces, provide a ton of general-purpose functionality for writing robust, maintainable applications.
Getting a handle on all the functionality you'll want to use can be a little daunting at first, especially if you're coming from object-oriented languages like Java, Ruby, or Python, where behavior is grouped using classes. In Clojure, namespaces are used to group similar behavior and state, and we've outlined a few of the core namespaces to help you find what you're looking for.
We have a page listing all functions, macros, and other vars in Clojure's core library as well.
The largest of the core namespaces, clojure.core
provides the bulk of the functionality you'll be using to build Clojure programs.
There are too many core functions to feature here, but take a look at the quickref to get a breakdown by conceptual arena.
Channel primitives for building asynchronous programs. Core.async is not provided as part of Clojure's standard distribution, and must be included as a dependency.
Core.logic is not provided as part of Clojure's standard distribution, and must be included as a dependency.
clojure.core.server contains functions to expose the Clojure environment through a socket connection and across network boundaries. If the property -Dclojure.server.repl="{:port 8881 :accept clojure.core.server/repl}"
is present when the JDK starts, Clojure will start the related socket server (or more than one) on the specified port. The API in the namespace allows to do this programmatically.
Extensible Data Notation is a subset of the Clojure language used as a data transfer format, designed to be used in a similar way to JSON or XML.
At some point in your adventures in Clojure land, you'll want to deserialize some clojure data structures from a string, and you'll want to use clojure.edn/read or clojure.edn/read-string for that. Do not use the read-*
functions in clojure.core
to deserialize untrusted Clojure code, as they can be unsafe.
Pretty printing utility, really nice for looking at larger-ish data structures. See pprint.
Functions for working on sets (#{1 2 3 4}
) in all the ways you'd expect, e.g. calculating the intersection of two sets, and testing if one set is a subset of another.
Provides most standard string manipulation and processing function that you'd expect in any general-purpose programming language.
In Clojure and ClojureScript strings are represented using the native platform implementation, and can be directly manipulated, e.g. (.toLowerCase "FOO") ;=> "foo"
. The clojure.string
namespace gives you the ability to manipulate strings in an idiomatic way: (clojure.string/lower-case "FOO") ;=> "foo"
.
Something to keep in mind is most (all?) of these functions take the string to act on as the first parameter, lending themselves well for use with the single-thrush operator (->) , as in this contrived example:
(require '[clojure.string :as str]) (-> " .LIRpa ni yAD dloc thgIrb a sAw Ti " str/reverse str/trim str/lower-case (str/replace #"\s+" " ") str/capitalize) ;;=> "It was a bright cold day in april"
Provides basic facilities for unit testing Clojure code.
Functional tree editing and manipulation. One of the core benefits of using Clojure is that you mostly work with immutable data structures. This, in turn, seems to make your programs easier to build and maintain.