How to write koans

sritchie edited this page Nov 28, 2011 · 1 revision

koans about equality, you would add the following forms to src/koans/equality.clj:

(defn truthy? [x] (boolean x))

(meditations
  "We shall contemplate truth by testing reality, via equality."
  (= __ true)

  "Non-boolean values have a sense of truthiness about them."
  (= __ (truthy? "cake")))

First come any definitions you want to use in your koans, followed by a call to the meditations macro with any number of koans.

Each koan consists of:

  • a descriptive string
  • a code form containing blanks (__).

The student' job is to fill in the blanks in such a way that the code form throws no exceptions and returns a truthy value.

Defining Solutions

To run properly every set of koans must maintain an entry in the :koan-resource file, located by default at resources/koans.clj. The koan-resource file contains a vector of 2-vectors, with each of these holding a koan name and map of answers:

[["equality" {"__" [true
                    true]}]
 ["atoms"    {"__" ["jail"]}]
]

The answer values are substituted into the koan file in the order they appear. lein koan test will check that every koan fails without and passes with answer substitution.