File tree Expand file tree Collapse file tree 1 file changed +27
-13
lines changed Expand file tree Collapse file tree 1 file changed +27
-13
lines changed Original file line number Diff line number Diff line change 1
1
(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.
6
6
7
- Main usage patterns:
7
+ ### Example
8
+
9
+ First we declare a *dynj* named `eff`, which expects a single argument.
8
10
9
11
```
10
12
(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
+ ```
11
22
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]
13
28
(assert (= 4 (eff 2))))
14
-
15
- (defn square [a] (* a a))
29
+ ```
16
30
17
- (foo) ;; => throws exception
18
-
19
- (binding [eff square]
20
- (foo))
31
+ With `binding` we can interpret `eff` as, say, a `square` function locally.
21
32
22
- ((with-bindings* {#'eff square} foo))
33
+ ```
34
+ (defn square [a] (* a a))
23
35
36
+ (binding [eff square]
37
+ (foo)) ;; => this works now
24
38
```
25
39
"
26
40
(:refer-clojure :rename {bound-fn* clj-bound-fn*
You can’t perform that action at this time.
0 commit comments