on 8564483 help sign in
153 lines 2.8 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```
13def i(def a) {
14 a.class in [
15 byte, Byte,
16 char, Character,
17 short, Short,
18 int, Integer,
19 long, Long,
20 BigInteger
21 ] && a >= 0
24def n(def a) {
25 if (a in List) {
26 if (a.size() == 1) {
27 n(a[0])
28 } else if (a.size() == 2) {
29 [n(a[0]), n(a[1])]
30 } else if (a.size() > 2) {
31 [n(a[0]), n(a.tail())]
32 } else {
33 throw new IllegalStateException()
34 }
35 } else if (i(a)) {
36 (BigInteger) a
37 } else {
38 throw new IllegalStateException()
39 }
42def wut(def a) {
43 i(a) ? 1 : 0
46def lus(def a) {
47 if (wut(a) == 0) {
48 throw new IllegalStateException()
49 }
50 1 + a
53def tis(def a) {
54 if (wut(a) == 1) {
55 throw new IllegalStateException()
56 }
57 a[0] == a[1] ? 0 : 1
60def fas(def a) {
61 if (wut(a) == 1) {
62 throw new IllegalStateException()
63 }
64 def h = a[0]
65 if (!i(h)) {
66 throw new IllegalStateException()
67 }
68 def t = a[1]
69 if (h == 0) {
70 throw new IllegalStateException()
71 } else if (h == 1) {
72 t
73 } else {
74 if (i(t)) {
75 throw new IllegalStateException()
76 }
77 if (h == 2) {
78 t[0]
79 } else if (h == 3) {
80 t[1]
81 } else {
82 def x = h.intdiv(2)
83 if (h.mod(2) == 0) {
84 fas([2, fas([x, t])])
85 } else {
86 fas([3, fas([x, t])])
87 }
88 }
89 }
92def tar(def a) {
93 if (wut(a) == 1) {
94 throw new IllegalStateException()
95 }
96 def s = a[0]
97 def f = a[1]
98 if (wut(f) == 1) {
99 throw new IllegalStateException()
100 }
101 def o = f[0]
102 def v = f[1]
103 if (wut(o) == 0) {
104 [tar([s, o]), tar([s, v])]
105 } else {
106 if (o == 0) {
107 fas([v, s])
108 } else if (o == 1) {
109 v
110 } else if (o == 3) {
111 wut(tar([s, v]))
112 } else if (o == 4) {
113 lus(tar([s, v]))
114 } else if (o == 5) {
115 tis(tar([s, v]))
116 } else {
117 if (wut(v) == 1) {
118 throw new IllegalStateException()
119 }
120 def x = v[0]
121 def y = v[1]
122 if (o == 2) {
123 tar([tar([s, x]), tar([s, y])])
124 } else if (o == 7) {
125 tar(n([s, 2, x, 1, y]))
126 } else if (o == 8) {
127 tar(n([s, 7, [[7, [0, 1], x], 0, 1], y]))
128 } else if (o == 9) {
129 tar(n([s, 7, y, 2, [0, 1], 0, x]))
130 } else if (o == 10) {
131 if (wut(x) == 1) {
132 tar([s, y])
133 } else {
134 def r = x[0]
135 def t = x[1]
136 tar(n([s, 8, t, 7, [0, 3], y]))
137 }
138 } else {
139 if (wut(y) == 1) {
140 throw new IllegalStateException()
141 }
142 def p = y[0]
143 def q = y[1]
144 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]))
146 } else {
147 throw new IllegalStateException()
148 }
149 }
150 }
151 }
153```