Skip to content

Commit fa1c423

Browse files
committed
Adapted K by EXAMPLE for Goal
1 parent 5859a44 commit fa1c423

File tree

2 files changed

+180
-1
lines changed

2 files changed

+180
-1
lines changed

goal_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func getMatchTests(glob string) ([]matchTest, error) {
206206

207207
// Adapted from Goal implementation.
208208
func getScriptMatchTests(glob string) ([]matchTest, error) {
209-
d := os.DirFS("testing/scripts")
209+
d := os.DirFS("testing/scripts") // currently unused
210210
fnames, err := fs.Glob(d, glob)
211211
if err != nil {
212212
return nil, err

testing/goal-by-example-test.goal

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/Heavily adapted from:
2+
/K by EXAMPLE
3+
/ K is product of Kx Inc. http://kx.com
4+
/ 2005.06.29. Attila Vrabecz (VrAbi) http://vrabi.web.elte.hu/k
5+
/based on J by EXAMPLE by
6+
/ 06/11/2005 (C) Oleg Kobchenko http://olegykj.sourceforge.net
7+
/simple arithmetic ===============================================
8+
tt.t{4~2+2} /comment is ' /': left of /: whitespace or nothing
9+
tt.t{-1~2-3} /negative numbers
10+
tt.t{14~2*3+4} /no precedence, right to left
11+
tt.t{10~(2*3)+4} /parentheses changes order
12+
tt.t{0.75~3%4} /division represented by '%'
13+
tt.t{16.0~4 exp 2} /square
14+
tt.t{2.0~sqrt 4} /square root
15+
/operations using lists ==========================================
16+
tt.t{(2 4 6)~2*1 2 3} /numeric list with space separators
17+
tt.t{(0.5 0.5 0.5)~1 2 3%2 4 6} /list to list operations, same size
18+
tt.t{3~#1 2 3} /size of vector
19+
tt.t{(1 1 1)~3#1} /generate sequence of same numbers
20+
tt.t{(1 2 1 2 1)~5#1 2} /or from a list of given elements
21+
/ list elements ===================================================
22+
tt.t{1~*1 2 3} /first element
23+
tt.t{(3 2 1)~|1 2 3} /reverse
24+
tt.t{3~*|1 2 3} /last element
25+
tt.t{(2 3)~1_1 2 3} /rest without first element
26+
tt.t{(1 2)~-1_1 2 3} /rest without last element
27+
/ indexing and sorting ============================================
28+
tt.t{2~1 2 3@1} /indexing is zero-based
29+
tt.t{(2 1)~1 2 3@1 0} /index can be vector too
30+
tt.t{(0 1 2)~!3} /generate zero-based sequence
31+
tt.t{1~2 4 6?4} /index of given element(s)
32+
tt.t{(1 0 2)~<2 1 6} /indices of sorted order (grade)
33+
tt.t{(1 2 6)~{x@<x}2 1 6} /sort vector (application)
34+
tt.t{(1 2 6)~{x[<x]}2 1 6} /sort vector (bracket syntax)
35+
/ list aggregation ================================================
36+
tt.t{(1 2 3 10 20)~1 2 3,10 20} /join vectors
37+
tt.t{6~1+2+3} /sum of elements
38+
tt.t{6~+/1 2 3} /insert '+' between elements
39+
tt.t{(1 3 6)~+\1 2 3} /running sum of elements
40+
tt.t{(1 3 6)~1,(1+2),(1+2+3)} /same as this
41+
tt.t{(1 2;2 3;3 4;4 5)~-2^1 2 3 4 5} / pairs (windows of 2 items)
42+
tt.t{10 14~+/-2^1 2 3 4 5} /sums of columns
43+
tt.t{10 14~(1 2)+(2 3)+(3 4)+(4 5)} /same as this
44+
tt.t{3 5 7 9~+/'-2^1 2 3 4 5} /sums of rows
45+
tt.t{3 5 7 9~((1+2);(2+3);(3+4);(4+5))} /same as this
46+
tt.t{"A"~@((1 2);(3 4 6);(7 6))} /list, can be non-rectangular
47+
tt.t{(3 4 6)~*(3 4 6;7 6)} /first item in the list
48+
/ function combinations ===========================================
49+
sq:{x exp 2}
50+
tt.t{20.0~{x+sq x}4} /a + a^2
51+
tt.t{(2.0 16.0)~(sqrt;sq)@`4} /[sqrt(a), a^2]
52+
tt.t{25.0~sq@+/2 3} /(a+b)^2
53+
tt.t{13.0~+/sq 2 3} /a^2 + b^2
54+
tt.t{25.0~{+/(sq x),2*/x}2 3} /(a + b)^2 = a^2 + b^2 + 2ab
55+
tt.t{5.0~sqrt+/sq 3 4} /sqrt(a^2 + b^2)
56+
/ user defined functions and arguments ============================
57+
d1:- /dyadic projection
58+
tt.t{1~d1[3;2]} /called
59+
d2:{x-y} /explicit dyad
60+
tt.t{1~d2[3;2]} /called
61+
m1:-: /monadic projection
62+
tt.t{(-1 -2 -3)~m1 1 2 3} /called
63+
m2:0- /monadic projection
64+
tt.t{(-1 -2 -3)~m2 1 2 3} /called
65+
m3:{-x} /explicit monad
66+
tt.t{(-1 -2 -3)~m3 1 2 3} /called
67+
tt.t{(1 1)~(d1;d2).`3 2} /dyads
68+
tt.t{(-1 -1 -1)~(m1;m2;m3).`1} /monads
69+
/ exponent and logarithm ==========================================
70+
e:exp 1 /exponent, Euler's number
71+
tt.t{2.718281828459045~e} /decimal representation
72+
tt.t{1.0~log e} /logarithm, ln e
73+
tt.t{2.0~log exp 2} /logarithm, ln e^2
74+
tt.t{65536.0~2 exp 16} /exponent base 2, 2^16
75+
tt.t{16.0~2 log 65536} /logarithm, log2 65536
76+
tt.t{16~(log 65536)%log 2} /expressed as division of natural logarithms
77+
/ trigonometry ====================================================
78+
tt.t{math.pi~math.π} /pi, provided by Go
79+
acos:{2*atan[sqrt(1-x)%1+x]} /acos
80+
tt.t{-1~cos math.pi} /identity cos
81+
tt.t{math.π~acos -1} /identity acos
82+
:a:(math.pi;2*math.pi;sq math.π) /pi, 2pi, pi^2
83+
tt.t{a~3.141592653589793 6.283185307179586 9.869604401089358} /tested
84+
:t:(+/sq(cos;sin)@`) /theorem of trigonometry
85+
tt.t{1.0~t math.pi} /tested
86+
tt.t{(1 1 1.0)~t a} /test theorem at angles (pi, 2pi, pi^2)
87+
/ matrices ========================================================
88+
tt.t{(1 2 3;2 4 6;3 6 9)~1 2 3*´1 2 3} /each right; multiply (outer product: multiplication table)
89+
/ (1 2 3
90+
/ 2 4 6
91+
/ 3 6 9)
92+
tt.t{(1 0 0;0 1 0;0 0 1)~{x=´x}@!3} /identity matrix
93+
/ (1 0 0
94+
/ 0 1 0
95+
/ 0 0 1)
96+
tt.t{(0 1 2;3 4 5)~2$!6} /cut; generate matrix with x rows
97+
/ (0 1 2
98+
/ 3 4 5)
99+
tt.t{(0 1;2 3;4 5)~-2$!6} /cut; generate matrix with x cols
100+
/ (0 1
101+
/ 2 3
102+
/ 4 5)
103+
tt.t{(0 1;1 1)~2$0 1 1 1} /cut given vector to matrix
104+
/ (0 1
105+
/ 1 1)
106+
/ structural transforms ===========================================
107+
:N:2$'2$!12 /2x2x3 matrix
108+
/ ((0 1 2
109+
/ 3 4 5)
110+
/ (6 7 8
111+
/ 9 10 11))
112+
tt.t{0 1 2 3 4 5 6 7 8 9 10 11~,//N} /ravel: list of atoms
113+
tt.t{(0 1 2;3 4 5;6 7 8;9 10 11)~,/N} /join top-level items, in this case 2x3 matrices
114+
/ (0 1 2
115+
/ 3 4 5
116+
/ 6 7 8
117+
/ 9 10 11)
118+
tt.t{(0 1 2 3 4 5;6 7 8 9 10 11)~,/'N} /ravel each sub-matrix
119+
/ (0 1 2 3 4 5
120+
/ 6 7 8 9 10 11)
121+
:M:1+3$!9 /3x3 matrix
122+
say'(:: ;+: ;|: ;|:'; 1 rotate)@`M
123+
/ ((1 2 3 / :: returns argument
124+
/ 4 5 6
125+
/ 7 8 9)
126+
/ (1 4 7 / +: transposes/flips
127+
/ 2 5 8
128+
/ 3 6 9)
129+
/ (7 8 9 / |: reverses items, here top-level arrays
130+
/ 4 5 6
131+
/ 1 2 3)
132+
/ (3 2 1 / |:' reverses each items
133+
/ 6 5 4
134+
/ 9 8 7)
135+
/ (4 5 6 / (rotate) rotates items
136+
/ 7 8 9
137+
/ 1 2 3))
138+
/ selection ======================================================
139+
N /reminder of N
140+
/ ((0 1 2
141+
/ 3 4 5)
142+
/ (6 7 8
143+
/ 9 10 11))
144+
tt.t{10~((N 1)1)1} /repetitive selection of items From list
145+
tt.t{10~3@[;1]/N} /apply select 3 times
146+
tt.t{10~N[1;1;1]} /scatter select
147+
tt.t{10~N . 1 1 1} /scatter select too
148+
/ factorial ==========================================
149+
tt.t{(1 2 6 24 120)~(f:{?[x<0;0;*/1.+!x]})'1+!5} /factorial
150+
tt.t{(1 2 6 24 120)~*\1+!5} /running product
151+
/ randomness and probability ======================================
152+
:A:?5 /5 random floats from 0..1
153+
/ 0.03076858832772078 0.05949331939073166 0.8927460717308945 0.1388339346947738 0.1718002917289505
154+
tt.t{5~#A} /5 of them
155+
tt.t{*/(A<1)&(0<A)} /all (0,1)
156+
:B:10?2 /coin toss
157+
/ 1 0 0 1 0 1 1 1 1 1
158+
tt.t{10>+/B}
159+
:C:-3?3 /deal 3 out of 3 cards in certain order
160+
/ 2 0 1
161+
tt.t{(!3)~^C} /each value is represented
162+
tt.t{(0.03076858832772078 0.8927460717308945)~(&/;|/)@`A} /min and max over the list
163+
B?0 /first zero
164+
/ 1
165+
{(+/x)%#x}C~´10000{-3?3}\_n /method monte carlo
166+
/ 0.16528347165283472
167+
/ unique elements =================================================
168+
S:1 2 3 3 2 3 3 2 4 4 2 /mississippi
169+
:D:?S
170+
tt.t{(1 2 3 4)~D} /? is unique/distinct
171+
:K:D?´S /find (?) indexes
172+
tt.t{(0 1 2 2 1 2 2 1 3 3 1)~K} /test it
173+
=.K
174+
/ (,0
175+
/ 1 1 1 1
176+
/ 2 2 2 2
177+
/ 3 3)
178+
tt.t{(,0;1 1 1 1;2 2 2 2;3 3)~=.K} /= is group, group keys
179+
tt.t{(1 4 4 2)~#'=.K} /frequencies

0 commit comments

Comments
 (0)