Skip to content

Commit 6f60983

Browse files
dynj: explain example line by line
1 parent 723a839 commit 6f60983

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

src/active/clojure/dynj.clj

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,40 @@
11
(ns active.clojure.dynj
2-
"Thin layer over dynamic vars for implicit dependency injection. Dynjs
3-
can be used to have better control over side effects, to abstract
4-
over different possible interpretations of an aspect of a program or
5-
to make things easier for testing.
2+
"Thin layer over dynamic vars for implicit dependency injection. *Dynjs* can be
3+
used to have better control over side effects, to abstract over different
4+
possible interpretations of an aspect of a program or to make things easier
5+
for testing.
66
7-
Main usage patterns:
7+
### Example
8+
9+
First we declare a *dynj* named `eff`, which expects a single argument.
810
911
```
1012
(declare-dynj eff [a])
13+
```
14+
15+
Note that `eff` itself can already be called, but it's \"abstract\" in the
16+
sense that without a bound implementation/interpreter, it cannot do anything.
17+
Hence the following will throw an exception:
18+
19+
```
20+
(eff 2)
21+
```
1122
12-
(defn foo []
23+
Let's say `foo` is a usage site of `eff`. (Of course, calling `foo` now will
24+
still throw the same exception as above.)
25+
26+
```
27+
(defn foo [s]
1328
(assert (= 4 (eff 2))))
14-
15-
(defn square [a] (* a a))
29+
```
1630
17-
(foo) ;; => throws exception
18-
19-
(binding [eff square]
20-
(foo))
31+
With `binding` we can interpret `eff` as, say, a `square` function locally.
2132
22-
((with-bindings* {#'eff square} foo))
33+
```
34+
(defn square [a] (* a a))
2335
36+
(binding [eff square]
37+
(foo)) ;; => this works now
2438
```
2539
"
2640
(:refer-clojure :rename {bound-fn* clj-bound-fn*

0 commit comments

Comments
 (0)