on master help sign in
Update groovy.md
817dff8Kohányi Róbert
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 }