Clojure Examples

I’ve just written a small library for myself that could be useful to anyone who’s learning Clojure: Examples.

It prints examples for the function passed as argument, taking them from

The library is on Clojars, so to use it just add the dependecy to your lein project.clj:

(defproject ...
    :dependencies [[examples \"0.3.0\"]]}

Then add it to your requires:

(:require [examples.core :refer [examples clear-examples-cache])

The usage is pretty simple: just call (examples function) in your REPL, and you’ll get all the examples printed.

E.g. (examples map)

(map inc [1 2 3 4 5])
;;=> (2 3 4 5 6)

;; map can be used with multiple collections. Collections will be consumed
;; and passed to the mapping function in parallel:
(map + [1 2 3] [4 5 6])
;;=> (5 7 9)

;; When map is passed more than one collection, the mapping function will
;; be applied until one of the collections runs out:
(map + [1 2 3] (iterate inc 1))
;;=> (2 4 6)

;; map is often used in conjunction with the # reader macro:
(map #(str \"Hello \" % \"!\" ) [\"Ford\" \"Arthur\" \"Tricia\"])
;;=> (\"Hello Ford!\" \"Hello Arthur!\" \"Hello Tricia!\")

;; A useful idiom to pull \"columns\" out of a collection of collections. 
;; Note, it is equivalent to:
;; user=> (map vector [:a :b :c] [:d :e :f] [:g :h :i])

(apply map vector [[:a :b :c]
                   [:d :e :f]
                   [:g :h :i]])
;;=> ([:a :d :g] [:b :e :h] [:c :f :i])

;; From


user=> (map #(vector (first %) (* 2 (second %)))
            {:a 1 :b 2 :c 3})
            ([:a 2] [:b 4] [:c 6])
user=> (into {} *1)
{:a 2, :b 4, :c 6}


Cache can be cleared with (clear-examples-cache [fun]). If name is omitted, the entire cache will be cleared.

I hope it will useful to someone!

Related Posts

Automatically adding a prefix to a record's labels in Purescript

Adding static files to Yesod

Planning a simple Reddit clone with Yesod

React-router-dom bindings for Reason

Parsing complex foreign objects in PureScript

A way to deal with big objects and FFI.

Post requests with PureScript Affjax and Argonaut

Simple ajax calls to an API example

Functor, Applicative e Monad illustrati

Una guida chiara

Arbitrary length lists with QuickCheck

Using sized to build arbitrary length lists for QuickCheck

PureScript Impressions

Trying to put some sense in JavaScript

Toggling DELL 9350's touchpad on Ubuntu 15.10

How to set up a hotkey to toggle DELL 9350's touchpad on Ubuntu 15.10