10From [Matt Earnshaw](https://github.com/mattearnshaw/anock/blob/master/src/anock/core.clj):
14 (:import anock.NockException))
16(declare atom? cell? cell)
19 "A noun is an atom or a cell."
22 (or (atom? noun) (cell? noun))))
25 "An atom is a natural number."
28 (and (integer? noun) (>= noun 0))))
31 "A cell is an ordered pair of nouns."
36 (not= 2 (count noun)) false
37 :else (and (noun? (first noun))
38 (noun? (second noun)))))
41 "= (pronounced 'tis') tests a cell for equality."
43 (if (atom? noun) (throw (anock.NockException. "Cannot tis an atom."))
48 "? (pronounced 'wut') tests whether a noun is a cell."
53 :else (throw (anock.NockException. "Invalid noun."))))
56 "+ (pronounced 'lus') adds 1 to an atom."
58 (if (atom? noun) (inc noun)
59 (throw (anock.NockException. "Can only lus atoms."))))
62 "/ (pronounced 'fas') is a tree address function."
64 (if (atom? noun) (throw (anock.NockException. "Cannot fas an atom."))
65 (let [[a b] (cell noun)]
66 (assert (and (pos? a) (atom? a)) "Subject of fas must be a positive atom.")
67 (if (and (not (coll? b)) (or (= 2 a) (= 3 a)))
68 (throw (anock.NockException. (str "Cannot fas noun: " noun))))
73 (even? a) (fas [2 (fas [(/ a 2) b])])
74 (odd? a) (fas [3 (fas [(/ (dec a) 2) b])])))))
77 "* (pronounced 'tar') means Nock"
79 (if (atom? noun) (throw (anock.NockException. "Cannot tar an atom."))
81 (let [noun (cell noun) [x [y z]] noun]
83 (cell? y) (cell (tar [x y]) (tar [x z]))
86 (= 3 y) (wut (tar [x z]))
87 (= 4 y) (lus (tar [x z]))
88 (= 5 y) (tis (tar [x z]))
91 (= 2 y) (tar [(tar [x p]) (tar [x q])])
92 (= 6 y) (tar [x 2 [0 1] 2 [1 (first q) (second q)]
93 [1 0] 2 [1 2 3] [1 0] 4 4 p])
94 (= 7 y) (tar [x 2 p 1 q])
95 (= 8 y) (tar [x 7 [[7 [0 1] p] 0 1] q])
96 (= 9 y) (tar [x 7 q 2 [0 1] 0 p])
97 (= 10 y) (if (cell? p)
98 (tar [x 8 (second p) 7 [0 3] q])
100 (catch RuntimeException e
101 (throw (anock.NockException. (str "Cannot tar the noun " noun)))))))
105; Some convenience functions
107 (if (and (= 1 (count x)) (coll? (first x)))
112 "[a b c] -> [a [b c]]"
116 (and (noun? a) (apply noun? b)) (vec c)
117 (apply noun? b) (apply vector (bracket a) b)
118 (noun? a) [a (apply* bracket b)]
119 :else [(bracket a) (apply* bracket b)])))
122 (if (apply atom? nouns)
123 (throw (anock.NockException. "Cannot convert atom to cell."))
124 (apply* bracket nouns)))
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list