on master help sign in
Merge pull request #12 from kohanyirobert/patch-1
eac6ea5Galen Wolfe-Pauly
docs/nock/implementations/groovy.md+85−32
@@ -10,6 +10,7 @@ next: true
10From [Kohányi Róbert](https://github.com/kohanyirobert/gnock/blob/master/gnock.groovy):
10From [Kohányi Róbert](https://github.com/kohanyirobert/gnock/blob/master/gnock.groovy):
11
11
12```
12```
13+@Memoized
13def i(def a) {
14def i(def a) {
14 a.class in [
15 a.class in [
15 byte, Byte,
16 byte, Byte,
@@ -21,28 +22,40 @@ def i(def a) {
21 ] && a >= 0
22 ] && a >= 0
22}
23}
23
24
25+@Memoized
24def n(def a) {
26def n(def a) {
27+ def r
28+ n(a, { r = it })
29+ r
30+}
31+
32+@TailRecursive
33+def n(def a, def r) {
25 if (a in List) {
34 if (a in List) {
26 if (a.size() == 1) {
35 if (a.size() == 1) {
27− n(a[0])
36+ r(a[0])
28− } else if (a.size() == 2) {
37+ } else if (a.size() >= 2) {
29− [n(a[0]), n(a[1])]
38+ n(a[0], { t ->
30− } else if (a.size() > 2) {
31− [n(a[0]), n(a.tail())]
39+ n(a.size() == 2 ? a[1] : a.tail(), { h ->
40+ r([t, h])
41+ })
42+ })
32 } else {
43 } else {
33 throw new IllegalStateException()
44 throw new IllegalStateException()
34 }
45 }
35 } else if (i(a)) {
46 } else if (i(a)) {
36− (BigInteger) a
47+ r((BigInteger) a)
37 } else {
48 } else {
38 throw new IllegalStateException()
49 throw new IllegalStateException()
39 }
50 }
40}
51}
41
52
53+@Memoized
42def wut(def a) {
54def wut(def a) {
43 i(a) ? 1 : 0
55 i(a) ? 1 : 0
44}
56}
45
57
58+@Memoized
46def lus(def a) {
59def lus(def a) {
47 if (wut(a) == 0) {
60 if (wut(a) == 0) {
48 throw new IllegalStateException()
61 throw new IllegalStateException()
@@ -50,6 +63,7 @@ def lus(def a) {
50 1 + a
63 1 + a
51}
64}
52
65
66+@Memoized
53def tis(def a) {
67def tis(def a) {
54 if (wut(a) == 1) {
68 if (wut(a) == 1) {
55 throw new IllegalStateException()
69 throw new IllegalStateException()
@@ -57,7 +71,15 @@ def tis(def a) {
57 a[0] == a[1] ? 0 : 1
71 a[0] == a[1] ? 0 : 1
58}
72}
59
73
74+@Memoized
60def fas(def a) {
75def fas(def a) {
76+ def r
77+ fas(a, { r = it })
78+ r
79+}
80+
81+@TailRecursive
82+def fas(def a, def r) {
61 if (wut(a) == 1) {
83 if (wut(a) == 1) {
62 throw new IllegalStateException()
84 throw new IllegalStateException()
63 }
85 }
@@ -69,27 +91,34 @@ def fas(def a) {
69 if (h == 0) {
91 if (h == 0) {
70 throw new IllegalStateException()
92 throw new IllegalStateException()
71 } else if (h == 1) {
93 } else if (h == 1) {
72− t
94+ r(t)
73 } else {
95 } else {
74 if (i(t)) {
96 if (i(t)) {
75 throw new IllegalStateException()
97 throw new IllegalStateException()
76 }
98 }
77 if (h == 2) {
99 if (h == 2) {
78− t[0]
100+ r(t[0])
79 } else if (h == 3) {
101 } else if (h == 3) {
80− t[1]
102+ r(t[1])
81 } else {
103 } else {
82− def x = h.intdiv(2)
104+ fas([h.intdiv(2), t], { p ->
83− if (h.mod(2) == 0) {
105+ fas([2 + h.mod(2), p], { q ->
84− fas([2, fas([x, t])])
106+ r(q)
85− } else {
107+ })
86− fas([3, fas([x, t])])
87− }
108+ })
88 }
109 }
89 }
110 }
90}
111}
91
112
113+@Memoized
92def tar(def a) {
114def tar(def a) {
115+ def r
116+ tar(a, { r = it})
117+ r
118+}
119+
120+@TailRecursive
121+def tar(def a, def r) {
93 if (wut(a) == 1) {
122 if (wut(a) == 1) {
94 throw new IllegalStateException()
123 throw new IllegalStateException()
95 }
124 }
@@ -101,18 +130,28 @@ def tar(def a) {
101 def o = f[0]
130 def o = f[0]
102 def v = f[1]
131 def v = f[1]
103 if (wut(o) == 0) {
132 if (wut(o) == 0) {
104− [tar([s, o]), tar([s, v])]
133+ tar([s, o], { p ->
134+ tar([s, v], { q ->
135+ r([p, q])
136+ })
137+ })
105 } else {
138 } else {
106 if (o == 0) {
139 if (o == 0) {
107− fas([v, s])
140+ r(fas([v, s]))
108 } else if (o == 1) {
141 } else if (o == 1) {
109− v
142+ r(v)
110 } else if (o == 3) {
143 } else if (o == 3) {
111− wut(tar([s, v]))
144+ tar([s, v], {
145+ r(wut(it))
146+ })
112 } else if (o == 4) {
147 } else if (o == 4) {
113− lus(tar([s, v]))
148+ tar([s, v], {
149+ r(lus(it))
150+ })
114 } else if (o == 5) {
151 } else if (o == 5) {
115− tis(tar([s, v]))
152+ tar([s, v], {
153+ r(tis(it))
154+ })
116 } else {
155 } else {
117 if (wut(v) == 1) {
156 if (wut(v) == 1) {
118 throw new IllegalStateException()
157 throw new IllegalStateException()
@@ -120,29 +159,43 @@ def tar(def a) {
120 def x = v[0]
159 def x = v[0]
121 def y = v[1]
160 def y = v[1]
122 if (o == 2) {
161 if (o == 2) {
123− tar([tar([s, x]), tar([s, y])])
162+ tar([s, x], { p ->
163+ tar([s, y], { q ->
164+ tar([p, q], {
165+ r(it)
166+ })
167+ })
168+ })
124 } else if (o == 7) {
169 } else if (o == 7) {
125− tar(n([s, 2, x, 1, y]))
170+ tar(n([s, 2, x, 1, y]), {
171+ r(it)
172+ })
126 } else if (o == 8) {
173 } else if (o == 8) {
127− tar(n([s, 7, [[7, [0, 1], x], 0, 1], y]))
174+ tar(n([s, 7, [[7, [0, 1], x], 0, 1], y]), {
175+ r(it)
176+ })
128 } else if (o == 9) {
177 } else if (o == 9) {
129− tar(n([s, 7, y, 2, [0, 1], 0, x]))
178+ tar(n([s, 7, y, 2, [0, 1], 0, x]), {
179+ r(it)
180+ })
130 } else if (o == 10) {
181 } else if (o == 10) {
131 if (wut(x) == 1) {
182 if (wut(x) == 1) {
132− tar([s, y])
183+ tar([s, y], {
184+ r(it)
185+ })
133 } else {
186 } else {
134− def r = x[0]
135− def t = x[1]
136− tar(n([s, 8, t, 7, [0, 3], y]))
187+ tar(n([s, 8, x[1], 7, [0, 3], y]), {
188+ r(it)
189+ })
137 }
190 }
138 } else {
191 } else {
139 if (wut(y) == 1) {
192 if (wut(y) == 1) {
140 throw new IllegalStateException()
193 throw new IllegalStateException()
141 }
194 }
142− def p = y[0]
143− def q = y[1]
144 if (o == 6) {
195 if (o == 6) {
145− tar(n([s, 2, [0, 1], 2, [1, p, q], [1, 0], 2, [1, 2, 3], [1, 0], 4, 4, x]))
196+ tar(n([s, 2, [0, 1], 2, [1, y[0], y[1]], [1, 0], 2, [1, 2, 3], [1, 0], 4, 4, x]), {
197+ r(it)
198+ })
146 } else {
199 } else {
147 throw new IllegalStateException()
200 throw new IllegalStateException()
148 }
201 }
docs/nock/implementations/hoon.md+1−1
@@ -1,7 +1,7 @@
1---
1---
2navhome: /docs
2navhome: /docs
3title: Hoon
3title: Hoon
4−sort: 8
4+sort: 9
5---
5---
6
6
7# Hoon
7# Hoon
docs/nock/implementations/scheme.mdnew file+128
@@ -0,0 +1,128 @@
1+---
2+navhome: /docs
3+title: Scheme
4+sort: 8
5+next: true
6+---
7+
8+# Scheme
9+
10+From [Kohányi Róbert](https://github.com/kohanyirobert/snock/blob/master/snock.ss):
11+
12+```
13+(import (rnrs (6)))
14+
15+(define (i a) a)
16+
17+(define (n a r)
18+ (cond
19+ ((list? a)
20+ (let ((l (length a)))
21+ (cond
22+ ((equal? l 0) (raise 1))
23+ ((equal? l 1) (r (car a)))
24+ (else
25+ (let ((t (cond
26+ ((equal? l 2) (cadr a))
27+ (else (cdr a)))))
28+ (n (car a)
29+ (lambda (p) (n t
30+ (lambda (q) (r (cons p q)))))))))))
31+ ((fixnum? a) (r a))
32+ (else (raise 2))))
33+
34+(define (wut a)
35+ (cond
36+ ((fixnum? a) 1)
37+ (else 0)))
38+
39+(define (lus a)
40+ (cond
41+ ((equal? (wut a) 0) (raise 3))
42+ (else (+ 1 a))))
43+
44+(define (tis a)
45+ (cond
46+ ((equal? (wut a) 1) (raise 4))
47+ ((equal? (car a) (cdr a)) 0)
48+ (else 1)))
49+
50+(define (fas a r)
51+ (cond
52+ ((equal? (wut a) 1) (raise 5))
53+ (else
54+ (let ((h (car a))
55+ (t (cdr a)))
56+ (cond
57+ ((not (fixnum? h)) (raise 6))
58+ ((equal? h 0) (raise 7))
59+ ((equal? h 1) (r t))
60+ ((fixnum? t) (raise 8))
61+ ((equal? h 2) (r (car t)))
62+ ((equal? h 3) (r (cdr t)))
63+ (else
64+ (fas (cons (div h 2) t)
65+ (lambda (p) (fas (cons (+ 2 (mod h 2)) p)
66+ (lambda (q) (r q)))))))))))
67+
68+(define (tar a r)
69+ (cond
70+ ((equal? (wut a) 1) (raise 9))
71+ (else
72+ (let ((s (car a))
73+ (f (cdr a)))
74+ (cond
75+ ((equal? (wut f) 1) (raise 10))
76+ (else
77+ (let ((o (car f))
78+ (v (cdr f)))
79+ (cond
80+ ((equal? (wut o) 0) (tar (cons s o)
81+ (lambda (p) (tar (cons s v)
82+ (lambda (q) (r (cons p q)))))))
83+ ((equal? o 0) (r (fas (cons v s) i)))
84+ ((equal? o 1) (r v))
85+ ((equal? o 3) (tar (cons s v)
86+ (lambda (p) (r (wut p)))))
87+ ((equal? o 4) (tar (cons s v)
88+ (lambda (p) (r (lus p)))))
89+ ((equal? o 5) (tar (cons s v)
90+ (lambda (p) (r (tis p)))))
91+ ((equal? (wut v) 1) (raise 11))
92+ (else
93+ (let ((x (car v))
94+ (y (cdr v)))
95+ (cond
96+ ((equal? o 2) (tar (cons s x)
97+ (lambda (p) (tar (cons s y)
98+ (lambda (q) (tar (cons p q)
99+ (lambda (u) (r u))))))))
100+ ((equal? o 7) (tar (n (list (list s) 2 (list x) 1 (list y)) i)
101+ (lambda (p) (r p))))
102+ ((equal? o 8) (tar (n (list (list s) 7 (list (list 7 (list 0 1) (list x)) 0 1) (list y)) i)
103+ (lambda (p) (r p))))
104+ ((equal? o 9) (tar (n (list (list s) 7 (list y) 2 (list 0 1) 0 (list x)) i)
105+ (lambda (p) (r p))))
106+ ((equal? o 10) (cond
107+ ((equal? (wut x) 1) (tar (cons s y)
108+ (lambda (p) (r p))))
109+ (else (tar (n (list (list s) 8 (list (cdr x)) 7 (list 0 3) (list y)) i)
110+ (lambda (p) (r p))))))
111+ ((equal? (wut y) 1) (raise 12))
112+ ((equal? o 6) (tar (n (list
113+ (list s)
114+ 2
115+ (list 0 1)
116+ 2
117+ (list 1 (list (car y)) (list (cdr y)))
118+ (list 1 0)
119+ 2
120+ (list 1 2 3)
121+ (list 1 0)
122+ 4
123+ 4
124+ (list x))
125+ i)
126+ (lambda (p) (r p))))
127+ (else (raise 13)))))))))))))
128+```