Interactive Clojure[script] code snippets

The basics

For basic usage, you just have to embed your code in a markdown tag eval-clojure like this:

```eval-clojure
(map inc [1 2 3])
```

And the result:

    (map inc [1 2 3])

In this chapter, we are going to show how to configure your klipse snippets using html5 data attributes on the DOM element that contains the klipse snippet.

We have to embed the klipse snippet in a <pre><code class="lang-eval-clojure> with the data attributes corresponding to the snippet properties attributes we want to set.

Hiding a code snippet

If you want to evaluate a piece of code, but you don't want it to appear in the layout of the page, you can set the hidden class to the pre element - like this

<pre class="hidden"><code class="lang-eval-clojure">
(def a  42)
</code></pre>

And we can use a in subsequent klipse snippets:

a

Using external libraries

To load a set of javascript libraries into klipse, you set data-external-libs with a comma separated list of javascript files you want to load.

For instance, this is ho we load rambda.js and underscore.js in a hidden klipse snippet:

<pre><code class="lang-eval-clojure" data-external-libs="https://raw.githubusercontent.com/viebel/math.combinatorics/master/src/main/clojure">
(ns my.combinatorics
 (:require [clojure.math.combinatorics :refer [permutations]]))

(permutations [1 2 3])
</code></pre>

    (ns my.combinatorics
     (:require [clojure.math.combinatorics :refer [permutations]]))

    (permutations [1 2 3])
 

Preamble

Sometimes you want to add a preamble to a klipse snippet - a piece of code that should be evaluated before the snippet is evaluated but shouldn't appear in the layout of the snippet. For that purpose, there is data-preamble.

For instance:

<pre><code class="lang-eval-clojure" data-preamble="(def b  (rand))">
b
</code></pre>

Modify a bit the following snippet, and see how a random number is generated again and again:


    b

Running code in a loop

If you want to have a klipse snippet that is evaluated again and again in intervals, you need to use data-loop-msec and specify the time in msec between the evaluations.

For example, this is how we display the date each second:

<pre><code class="lang-eval-clojure" data-loop-msec="1000">
(rand)
</code></pre>

This snippet is evaluated each second - without any user interaction:


(rand)

Running code from a gist

Sometimes, you don't want to keep your code in the source of your gitbook but rather have it in a github gist. It will allow you to modify the code of the snippet without having to republish your gitbook.

For that, you use data-gist-id with the id of your gist - including your username.

For example, I have this gist that defines a function read-several-exps for reading several expressions from a string - as read-string reads only a single expression:

This is how I include in in my page:

<pre><code class="lang-eval-clojure" data-gist-id="viebel/8cb19d258fea39a64146721ce50603d0">
</code></pre>

results matching ""

    No results matching ""