-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsplit-lab.red
323 lines (283 loc) · 7.86 KB
/
split-lab.red
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
Red [
title: "Split Lab"
author: "Gregg Irwin"
File: %split-lab.red
Icon: %split-lab.ico
]
#include %split.red
input-type: string!
input: none
rule: none
output: none
saved: copy []
saved-file: %saved-tests.dat
if exists? saved-file [saved: load saved-file]
set-test-values: does [
input: fld-input/text
if input-type = block! [
input: load input
]
rule: load fld-rule/text
if 'charset = first rule [
rule: do rule
]
output: none
]
run-test: does [
set-test-values
;output: split input rule
print [
"Input:" tab mold input newline
"Rule:" tab mold rule newline
"Output:" tab mold output newline
]
]
save-test: has [new-entry] [
if none? output [
alert "Please run the test before saving."
exit
]
new-entry: #[]
new-entry/input: input
new-entry/rule: rule
new-entry/output: output
append saved new-entry
save saved-file saved
]
;-------------------------------------------------------------------------------
; split-type input rule output
;-------------------------------------------------------------------------------
samples: [
[split "abcdefghi" 2]
[split "abcdefghi" 3]
[split "abcdefghi" 4]
[split "abc,def,ghi" comma]
]
;-------------------------------------------------------------------------------
input-samples: [
"a,b,c:d,e,f"
]
split-types: [
"At every delimiter" delim
"Once at a delimiter" [once at]
"Before every delimiter" [before delim]
"Once before a delimiter" [once before delim]
"After every delimiter" [after delim]
"Once after a delimiter" [once after delim]
"Into 2 parts, at a position or delimiter" [once at]
"Into parts of size N" parts-of-size-N
"Into N parts" into-N-parts ;[into delim parts]
"Into uneven parts" uneven
"Using a pass/fail test function" filter
"Using multiple test functions" partition
"Using a simple parse rule" delim
"First using one rule, then another" multi
]
string-rule-samples: [
#","
{charset ",:"}
]
block-rule-samples: [
[]
[]
[]
]
;-------------------------------------------------------------------------------
set-split-type-info: has [face] [
face: fld-split-type
key: pick face/data 2 * face/selected - 1
print [mold key tab mold split-types/:key]
]
view [
across
text "I want to split:" fld-split-type: drop-list 400 data split-types on-change [
set-split-type-info
] return
; text "Rule is a" pad 0x-5
; radio "String" on [rule-type: string!]
; radio "Block" [rule-type: block!] return
; hint {char! OR string! OR charset ",:" OR integer! OR block!}
;pad 90x0 text snow navy "If String is checked, rule used as-is; otherwise loaded and used as a Red value." return
pad 90x0 text snow navy "Rule is evaluated (reduced) and used as a Red value; format accordingly." return
pad 0x-10
text "Rule:" fld-rule: field 400 hint {charset ",:"}
;chk-str-rule?: check "String" off return
return
; text "Rule is a" pad 0x-5
; radio "String" [rule-type: string!]
; radio "Value" on [rule-type: default!] return
pad 0x10
pad 0x10
pad 90x0 text snow navy "If String is checked, value used as-is; otherwise loaded and parsed as Red data." return
pad 0x-10
;text "Input:" fld-input: field 400 hint "a,b,c:d,e,f"
text "Input:" fld-input: drop-down 400 data input-samples hint "a,b,c:d,e,f"
chk-str-input?: check "String" on return
text "Input is a" pad 0x-5
radio "String" on [input-type: string!]
radio "Block" [input-type: block!] return
pad 0x10
text "Enter the delimiting rule as a Red value." return
pad 0x-10
text "Output:" fld-output: area 400x250
return
pad 210x0
button "Got what I wanted"
button "NOT what I expected" [ask-for-expected-result]
return
pad 210x0
button "Run" [run-test]
button "Save" [save-test]
button "Help" [show-help]
button "Quit" [quit]
]
view [
space 4x2
tab-panel 800x500 [
"Split by size or postion" [
space 4x2
check "Split by ..." return
pad 20x0 text font-color navy "example..." return
pad 20x0 text "How often?" text "Rarely" sld-x: slider text "A lot" return
pad 20x0 check "This is worth supporting"
]
"Split by delimiter" []
"Split by predicate" []
"Split at 2 levels" []
] return
check "Split by ..." return
text font-color navy "example..." return
text "How often?" text "Rarely" sld-x: slider text "A lot" return
check "This is worth supporting"
]
;-------------------------------------------------------------------------------
comment {
What about The Hatchet Challenge. How fast can you pass the split tests?
Have a set of tests, try to solve each one in as short a time or fewest attempts.
Preset test suites can let people challenge for high score.
practice rehearse train exercise drill work-out gym studio kata dojo
muscle-memory
}
task: #[
desc: ""
input: "" ; string or block
goal: [] ; expected output, always a block
time: none
tries: [] ; be able to see what they actually tried
report: none ; [input goal rule note]
]
help-text: {
Sharpen your skills.
Hone that code to a fine edge.
Hone your splitting skills.
keyword: [
'every | 'once | 'into | 'as-delim
| 'first | 'next | 'then | 'by ("first/next/then/by go with multi-split")
| 'times ("Goes with count")
| ordinal
| delim-modifier
]
}
inputs: [
{"a,b,c"}
{"1234567812345678"}
{"PascalCaseNameAndMoreToo"}
{"abc<br>de<br>fghi<br>jk"}
{{line 1;^/line 2;^/line 3;^/}} ; curly braces needed due to newlines
{{line_1:^/line_2:^/line_3:^/}}
{{key-a=1^/key-b=2:^/key-c=3}}
{"PascalCaseName"}
{"camelCaseName"}
{"<br>abc<br>de<br><para><br>fghi<br>jk<br>"} ; [by <para> then <br>]
{"1.2.3-alpha.b+2045.RC3"} ; semver
{"PascalCaseName camelCaseName dash-marked-name under_marked_name"} ;compose/deep [by (space) then (charset [#"A" - #"Z" "-_"])]
{"YYYYMMDD/HHMMSS"}
{"Mon, 24 Nov 1997"}
;{}
{[1 2 3 4 5 6]}
{[1 2 3 4 5 6 3 7 8 9]}
{[1 2 [3] 4 5 6 [3] 7 8 9]}
{[1 2.3 /a word "str" #iss x: :y]}
{[1 2.3 /a word "str" #iss x: :y <T>]}
{[1 2 3 space 4 5 6 space 7 8 9]} ; compose [by ['space] then (:even?)]
{[1 2.3 /mark word "str" /mark #iss x: :y]}
]
split-it: does [
; Need to figure out how best to handle the rule, because it could be
; comma
; charset ""
; before x which has to be [[before x]] if we DO it.
; etc.
if error? set/any 'err try [
txt-result/data: mold split load txt-input/text make-rule fld-rule/data
][
print mold err
; TBD: format error for display
alert mold err
]
]
next-task: does [
txt-input/data: random/only inputs
]
make-rule: function [
data "Content from user input field"
][
;print [type? data mold data]
val: data
case [
any [word? val get-word? val][get val]
block? val [
case [
parse val [['charset | 'make 'bitset!] set arg [string! | block!]] [
charset arg
]
; TBD: /only
parse val ['compose/deep set arg [string! | block!]] [
compose/deep arg
]
parse val ['compose set arg [string! | block!]] [
compose arg
]
parse val ['reduce set arg [string! | block!]] [
reduce arg
]
'else [
val
]
]
]
'else [ ; char! integer! string!
val
]
]
]
view/options [
style lbl: text 150
text 600x30 font-size 14 "Your goal is to get this result:" return
txt-goal: text 400x100 bold font-color navy "[ ... ^/^/^/^/^ ... ]" return
lbl "Here is your input:" txt-input: text bold font-color navy 400x50 {"a,b,c"} return
lbl "Put your rule here:" fld-rule: field 400 on-enter [split-it]
button "&Split!" [split-it] return
lbl "Here's the result you got:" return
pad 20x0
txt-result: text 400x175 bold font-color navy "[ ... ^/^/^/^/^/^/^/^/^/^/ ... ]" return
lbl "I think I found a bug" fld-bug-note: area 400x75
button "Report" [] return
pad 450x20
button "&Instructions" []
button "&Next Task >>" [next-task]
][
actors: make object! [
on-key: function [face event] [
;print ['on-key event/key event/flags]
case [
event/ctrl? [
switch event/key [
#"^N" [next-task]
#"^S" [split-it]
]
]
]
]
]
]