on 6d45ede help sign in
206 lines 3.5 kB history raw
1---
2navhome: /docs
3title: Groovy
4sort: 7
5next: true
6---
8# Groovy
10From [Kohányi Róbert](https://github.com/kohanyirobert/gnock/blob/master/gnock.groovy):
12```
13@Memoized
14def i(def a) {
15 a.class in [
16 byte, Byte,
17 char, Character,
18 short, Short,
19 int, Integer,
20 long, Long,
21 BigInteger
22 ] && a >= 0
25@Memoized
26def n(def a) {
27 def r
28 n(a, { r = it })
29 r
32@TailRecursive
33def n(def a, def r) {
34 if (a in List) {
35 if (a.size() == 1) {
36 r(a[0])
37 } else if (a.size() >= 2) {
38 n(a[0], { t ->
39 n(a.size() == 2 ? a[1] : a.tail(), { h ->
40 r([t, h])
41 })
42 })
43 } else {
44 throw new IllegalStateException()
45 }
46 } else if (i(a)) {
47 r((BigInteger) a)
48 } else {
49 throw new IllegalStateException()
50 }
53@Memoized
54def wut(def a) {
55 i(a) ? 1 : 0
58@Memoized
59def lus(def a) {
60 if (wut(a) == 0) {
61 throw new IllegalStateException()
62 }
63 1 + a
66@Memoized
67def tis(def a) {
68 if (wut(a) == 1) {
69 throw new IllegalStateException()
70 }
71 a[0] == a[1] ? 0 : 1
74@Memoized
75def fas(def a) {
76 def r
77 fas(a, { r = it })
78 r
81@TailRecursive
82def fas(def a, def r) {
83 if (wut(a) == 1) {
84 throw new IllegalStateException()
85 }
86 def h = a[0]
87 if (!i(h)) {
88 throw new IllegalStateException()
89 }
90 def t = a[1]
91 if (h == 0) {
92 throw new IllegalStateException()
93 } else if (h == 1) {
94 r(t)
95 } else {
96 if (i(t)) {
97 throw new IllegalStateException()
98 }
99 if (h == 2) {
100 r(t[0])
101 } else if (h == 3) {
102 r(t[1])
103 } else {
104 fas([h.intdiv(2), t], { p ->
105 fas([2 + h.mod(2), p], { q ->
106 r(q)
107 })
108 })
109 }
110 }
113@Memoized
114def tar(def a) {
115 def r
116 tar(a, { r = it})
117 r
120@TailRecursive
121def tar(def a, def r) {
122 if (wut(a) == 1) {
123 throw new IllegalStateException()
124 }
125 def s = a[0]
126 def f = a[1]
127 if (wut(f) == 1) {
128 throw new IllegalStateException()
129 }
130 def o = f[0]
131 def v = f[1]
132 if (wut(o) == 0) {
133 tar([s, o], { p ->
134 tar([s, v], { q ->
135 r([p, q])
136 })
137 })
138 } else {
139 if (o == 0) {
140 r(fas([v, s]))
141 } else if (o == 1) {
142 r(v)
143 } else if (o == 3) {
144 tar([s, v], {
145 r(wut(it))
146 })
147 } else if (o == 4) {
148 tar([s, v], {
149 r(lus(it))
150 })
151 } else if (o == 5) {
152 tar([s, v], {
153 r(tis(it))
154 })
155 } else {
156 if (wut(v) == 1) {
157 throw new IllegalStateException()
158 }
159 def x = v[0]
160 def y = v[1]
161 if (o == 2) {
162 tar([s, x], { p ->
163 tar([s, y], { q ->
164 tar([p, q], {
165 r(it)
166 })
167 })
168 })
169 } else if (o == 7) {
170 tar(n([s, 2, x, 1, y]), {
171 r(it)
172 })
173 } else if (o == 8) {
174 tar(n([s, 7, [[7, [0, 1], x], 0, 1], y]), {
175 r(it)
176 })
177 } else if (o == 9) {
178 tar(n([s, 7, y, 2, [0, 1], 0, x]), {
179 r(it)
180 })
181 } else if (o == 10) {
182 if (wut(x) == 1) {
183 tar([s, y], {
184 r(it)
185 })
186 } else {
187 tar(n([s, 8, x[1], 7, [0, 3], y]), {
188 r(it)
189 })
190 }
191 } else {
192 if (wut(y) == 1) {
193 throw new IllegalStateException()
194 }
195 if (o == 6) {
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 })
199 } else {
200 throw new IllegalStateException()
201 }
202 }
203 }
204 }
206```