3 ms·
My favorite documentation/comments feature of Clojure is the ability to attach examples to the metadata, e.g: (defn prime? "Returns true if given nu
by iLemming 6y ago
My favorite documentation/comments feature of Clojure is the ability to attach examples to the metadata, e.g:
(defn prime?
"Returns true if given number is prime."
{:example
'((prime? 97) "=> true"
(prime? 74) "=> false")}
[n]
,,,
;; implementation
)
(-> #'user/prime? meta :example)
;; => ((prime? 97) "=> true" (prime? 74) "=> false")
You can even incorporate assertions and have tests that would make sure the examples are correct, something like:
(defn prime?
"Returns true if given number is prime."
{:examples
'((-> (prime? 74) false? assert)
(-> (prime? 97) true? assert))}
[n]
,,,
;; implementation
)
(doseq [f (->> #'user/prime? meta :example)] (eval f))