on master help sign in
Create groovy.md
commite20c699113739246a888cdbe5b55d2883ac803af authorKohányi Róbert <kohanyi.robert@gmail.com> dateSun 29 May 2016, 01:09 (10 years ago) parent968a3b9
1 file changed+141
docs/nock/implementations/groovy.md+141
docs/nock/implementations/groovy.mdnew file+141
@@ -0,0 +1,141 @@
1+---
2+navhome: /docs
3+title: Groovy
4+sort: 8
5+next: true
6+---
7+
8+# Groovy
9+
10+From [Kohányi Róbert](https://github.com/kohanyirobert/gnock/blob/master/gnock.groovy):
11+
12+```
13+def n(def a) {
14+ if (a in List) {
15+ if (a.size() == 1) {
16+ n(a[0])
17+ } else if (a.size() == 2) {
18+ [n(a[0]), n(a[1])]
19+ } else if (a.size() > 2) {
20+ [n(a[0]), n(a.tail())]
21+ } else {
22+ throw new IllegalStateException()
23+ }
24+ } else if (a in Integer && a >= 0) {
25+ a
26+ } else {
27+ throw new IllegalStateException()
28+ }
29+}
30+
31+def wut(def a) {
32+ a in Integer ? 1 : 0
33+}
34+
35+def lus(def a) {
36+ if (wut(a) == 0) {
37+ throw new IllegalStateException()
38+ }
39+ 1 + a
40+}
41+
42+def tis(def a) {
43+ if (wut(a) == 1) {
44+ throw new IllegalStateException()
45+ }
46+ a[0] == a[1] ? 0 : 1
47+}
48+
49+def fas(def a) {
50+ if (wut(a) == 1) {
51+ throw new IllegalStateException()
52+ }
53+ def h = a[0]
54+ if (!(h in Integer)) {
55+ throw new IllegalStateException()
56+ }
57+ def t = a[1]
58+ if (h == 0) {
59+ throw new IllegalStateException()
60+ } else if (h == 1) {
61+ t
62+ } else {
63+ if (t in Integer) {
64+ throw new IllegalStateException()
65+ } else if (h == 2) {
66+ t[0]
67+ } else if (h == 3) {
68+ t[1]
69+ } else {
70+ def i = h.intdiv(2)
71+ if (h.mod(2) == 0) {
72+ fas([2, fas([i, t])])
73+ } else {
74+ fas([3, fas([i, t])])
75+ }
76+ }
77+ }
78+}
79+
80+def tar(def a) {
81+ if (wut(a) == 1) {
82+ throw new IllegalStateException()
83+ }
84+ def s = a[0]
85+ def f = a[1]
86+ if (wut(f) == 1) {
87+ throw new IllegalStateException()
88+ }
89+ def o = f[0]
90+ def v = f[1]
91+ if (wut(o) == 0) {
92+ [tar([s, o]), tar([s, v])]
93+ } else {
94+ if (o == 0) {
95+ fas([v, s])
96+ } else if (o == 1) {
97+ v
98+ } else if (o == 3) {
99+ wut(tar([s, v]))
100+ } else if (o == 4) {
101+ lus(tar([s, v]))
102+ } else if (o == 5) {
103+ tis(tar([s, v]))
104+ } else {
105+ if (wut(v) == 1) {
106+ throw new IllegalStateException()
107+ }
108+ def x = v[0]
109+ def y = v[1]
110+ if (o == 2) {
111+ tar([tar([s, x]), tar([s, y])])
112+ } else if (o == 7) {
113+ tar(n([s, 2, x, 1, y]))
114+ } else if (o == 8) {
115+ tar(n([s, 7, [[7, [0, 1], x], 0, 1], y]))
116+ } else if (o == 9) {
117+ tar(n([s, 7, y, 2, [0, 1], 0, x]))
118+ } else if (o == 10) {
119+ if (wut(x) == 1) {
120+ tar([s, y])
121+ } else {
122+ def r = x[0]
123+ def t = x[1]
124+ tar(n([s, 8, t, 7, [0, 3], y]))
125+ }
126+ } else {
127+ if (wut(y) == 1) {
128+ throw new IllegalStateException()
129+ }
130+ def p = y[0]
131+ def q = y[1]
132+ if (o == 6) {
133+ tar(n([s, 2, [0, 1], 2, [1, p, q], [1, 0], 2, [1, 2, 3], [1, 0], 4, 4, x]))
134+ } else {
135+ throw new IllegalStateException()
136+ }
137+ }
138+ }
139+ }
140+}
141+```