Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 64 lines (41 sloc) 1.436 kb
e08722c added files
David McNeil authored
1 (ns bolo.op-demo
2 (:use [bolo.base :only (new-op)]))
3
4 ;; example of how an operator works
5
6 ;; an operator is similar to a Clojure record. The main difference is
7 ;; that an operator presents itself as a Clojure List, whereas a record
8 ;; presents itself as a Clojure Map.
9
10
11 ;; define a new operator:
12
13 (new-op foo [x y z])
14
15 ;; create an instance of our new operator
16
17 (foo 1 2 3)
18 ;; => (foo 1 2 3)
19
20 ;; for each operator a new Java Class is generated
21
22 (class (foo 1 2 3))
23 ;; => bolo.ops.Foo
24
25 ;; the operator instances are associative. Fields are accessed by keyword
26
27 (:x (foo 1 2 3))
28 ;; => 1
29
30 ;; operators can be constructed in trees
31
32 (foo 1 (foo 10 20 30) 3)
33 ;; => (foo 1 (foo 10 20 30) 3)
34
35 (:z (:y (foo 1 (foo 10 20 30) 3)))
36 ;; => 30
37
38 ;; the instances are Clojure lists.
39
40 (list? (foo 1 2 3))
41 ;; => true
42
43 ;; and Clojure seqs
44
45 (seq? (foo 1 2 3))
46 ;; => true
47
48 (first (foo 1 2 3))
49 ;; => foo
50
51 (second (foo 1 2 3))
52 ;;=> 1
53
54 (class (first (foo 1 2 3)))
55 ;; => clojure.lang.Symbol
56
57 ;; The facts that operator instances are Clojure seqs and that the
58 ;; first item in them are unqualified symbols governs how
59 ;; operator instances are evaluated by the Clojure evaluator. This
60 ;; evalution "trick" is a bit subtle to understand at first, but it is
61 ;; the key to the usefulness of operators. The context in which an
62 ;; operator instance is evaluated will dictate which definition of the
63 ;; symbols is used and thus what the result of the evaluation is.
Something went wrong with that request. Please try again.