Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 64 lines (41 sloc) 1.436 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
(ns bolo.op-demo
  (:use [bolo.base :only (new-op)]))

;; example of how an operator works

;; an operator is similar to a Clojure record. The main difference is
;; that an operator presents itself as a Clojure List, whereas a record
;; presents itself as a Clojure Map.


;; define a new operator:

(new-op foo [x y z])

;; create an instance of our new operator

(foo 1 2 3)
;; => (foo 1 2 3)

;; for each operator a new Java Class is generated

(class (foo 1 2 3))
;; => bolo.ops.Foo

;; the operator instances are associative. Fields are accessed by keyword

(:x (foo 1 2 3))
;; => 1

;; operators can be constructed in trees

(foo 1 (foo 10 20 30) 3)
;; => (foo 1 (foo 10 20 30) 3)

(:z (:y (foo 1 (foo 10 20 30) 3)))
;; => 30

;; the instances are Clojure lists.

(list? (foo 1 2 3))
;; => true

;; and Clojure seqs

(seq? (foo 1 2 3))
;; => true

(first (foo 1 2 3))
;; => foo

(second (foo 1 2 3))
;;=> 1

(class (first (foo 1 2 3)))
;; => clojure.lang.Symbol

;; The facts that operator instances are Clojure seqs and that the
;; first item in them are unqualified symbols governs how
;; operator instances are evaluated by the Clojure evaluator. This
;; evalution "trick" is a bit subtle to understand at first, but it is
;; the key to the usefulness of operators. The context in which an
;; operator instance is evaluated will dictate which definition of the
;; symbols is used and thus what the result of the evaluation is.
Something went wrong with that request. Please try again.