koans.app

Clojure Koans

Clojure is a modern Lisp: a small, consistent language built around immutable data and functions that transform it. Programs are made of plain maps, vectors and sets rather than classes, and macros let the language grow with your needs. It runs on the Java platform and, as ClojureScript, in JavaScript, which is how it runs here. These koans cover its data structures, sequences and laziness, functions and destructuring, state with atoms, protocols, records and multimethods, and macros.

79 koans in 18 groups, tested with clojure.test. Each is a small program whose tests fail: replace every ___ until they pass.

Start the path

Awakening

How a koan checks your answers, and what Clojure counts as true.

  1. Assertions
  2. Truth
  3. Nil
  4. Equality
  5. Keywords and symbols

Numbers

Arithmetic, comparisons, and the functions that work on numbers.

  1. Arithmetic
  2. Division and remainders
  3. Comparison
  4. Questions about numbers
  5. Conversion

Strings

Building strings, taking them apart, and searching them with regular expressions.

  1. Building strings
  2. String functions
  3. Searching and replacing
  4. Regular expressions
  5. Strings as sequences

Lists and vectors

Ordered collections: vectors to index into, lists to build from the front.

  1. Vectors
  2. Lists
  3. Collections never change
  4. Finding values

Maps

Keys to values: looking up, adding, changing, and reaching into nested maps.

  1. Maps
  2. Changing maps
  3. Nested maps
  4. Keys and values
  5. Transforming maps

Sets

Collections of unique values, and the operations between them.

  1. Sets
  2. Set operations
  3. Sorted collections

Sequences

One abstraction for every collection: first, rest, and the functions built on them.

  1. The sequence abstraction
  2. Range, take and drop
  3. Combining sequences
  4. Ordering
  5. Into and conversions

Map, filter and reduce

Transforming collections with functions: the heart of Clojure.

  1. Map and filter
  2. Reduce
  3. Some and every
  4. Grouping and counting
  5. Mapcat and for

Functions

Defining functions, and building new ones from old.

  1. Defining functions
  2. Arities and variadic functions
  3. Anonymous functions
  4. Building functions
  5. Closures
  6. Functions as values
  7. Memoize

Destructuring

Taking collections apart right where you name things.

  1. Taking sequences apart
  2. Taking maps apart
  3. Destructuring parameters

Control flow

Choosing what to do: if, when, cond, case, and naming values with let.

  1. If and when
  2. Cond
  3. Case
  4. If-let and when-let
  5. Let

Threading macros

Writing a pipeline of steps from top to bottom.

  1. Thread-first and thread-last
  2. More threading macros

Recursion and laziness

Looping without changing anything, and sequences computed only when needed.

  1. Loop and recur
  2. Lazy sequences
  3. Take-while and drop-while
  4. Your own lazy sequences

State

Changing things safely: atoms, watches, and values computed on demand.

  1. Atoms
  2. Atoms holding maps
  3. Compare-and-set and watches
  4. Delay and volatile

Records, protocols and multimethods

Polymorphism in Clojure: protocols dispatch on types, multimethods on anything.

  1. Records
  2. Protocols
  3. Extending protocols
  4. Multimethods
  5. Hierarchies

Macros

Code is data: quoting it, building it, and writing macros that write code.

  1. Code as data
  2. Syntax quote
  3. Defmacro
  4. Macroexpand

Errors

Throwing and catching, with errors that carry data.

  1. Try and catch
  2. Errors with data
  3. Handling errors

Puzzles

Small programs to read and predict.

  1. FizzBuzz
  2. Word frequencies
  3. Anagrams
  4. Run-length encoding
  5. Roman numerals