-
Notifications
You must be signed in to change notification settings - Fork 1
/
split-playground-g.red
690 lines (620 loc) · 24.3 KB
/
split-playground-g.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
Red [
Title: "Comparative playground for splitting functions in Red"
Author: "Galen Ivanov"
Notes: "Based on code by Toomas Vooglaid and Gregg Irwin"
Needs: view
Icon: %split-lab.ico
]
#include %split.red
#include %split-r.red
help-new: #include %help-new.txt
help-dia: #include %help-dialect.txt
help-ref: #include %help-refinement.txt
#include %help.red
play: context [
suite: copy orig-suite: #include %practice-split-suite-2.red
tasks: make block! 50
len: length? suite
over-dial:
over-ref:
over-mode:
over-new:
over-load: off
color-right: 60.230.120
color-wrong: brick + 50.70.40
session-file: none
default-dir: none
mode: 'predefined ; or 'sandbox
prompt: "Enter the delimiter and press <Enter> to split"
mode-prompt: [
predefined: "Switch to sandbox mode to play with your own test cases"
sandbox: "Switch to predefined tasks mode"
]
new-prompt: [
predefined: "Start a new predefined session"
sandbox: "Start a new sandbox session"
]
load-prompt: [
predefined: "Load an existing predefined session"
sandbox: "Load an existing sandbox session"
]
tabs: [dialected-delimiter dialected-btn refinement-delimiter
c1 c2 c3 c4 c5 c6 c7 c8 lmt refinement-btn dialected-delimiter
task-notes help-btn new-btn load-btn save-btn dialected-delimiter]
clrs: [unattempted: white correct: color-right incorrect: color-wrong]
clrs-b: [unattempted: (linen - 10.10.10) correct: color-right incorrect: color-wrong]
cur-task: 1
task-stats: make object! [
id: #00
desc: "" ; not used
input: ""
goal: ""
hint: "" ; not used
dial-status: 'unattempted ; [unattempted | wrong | correct]
dial-solution: "" ; stores the last correct solution
dial-tries: []
ref-status: 'unattempted
ref-tries: []
ref-solution: "" ; stores the last correct solution
refinements: [] ; stores the refinements for the correct solution
notes: ""
difficulty: 0 ; task rating 1 - 10
importance: 0
dialected-rating: 0 ; 1 - 10
refinement-rating: 0 ; 1 - 10
category: "" ; not used
]
init-tasks: does [
suite: switch mode [
predefined [orig-suite]
sandbox [collect [loop 21 [keep/only copy []]]]
]
t: collect [
foreach task suite [
keep/only to-block make task-stats task
]
]
if mode = 'sandbox [
n: 0
forall t [t/1/id: n: n + 1] ; enumerate the custom tasks
]
t
]
update-status: func [
n [number!] {Task number}
/local t
][
t: pick tasks n
rejoin [
t/id space
mold t/input " -> "
mold t/goal space
"Dialected: " t/dial-status space
"Refinements: " t/ref-status space
]
]
does-on-over: func [face n][
face/data: not face/data
info-text/text: either face/data [update-status n][""]
]
task-buttons: collect [
repeat n len [
keep compose/deep [
task (form n)
[load-task (n) set-focus dialected-delimiter]
on-over [does-on-over face (n)]
]
]
]
task-boxes: collect [
repeat n len [
left: to-set-word rejoin ["Stat-d-" n] ; dialected
right: to-set-word rejoin ["Stat-r-" n] ; refinement-based
keep compose/deep [
(left) task-status on-over [does-on-over face (n)] pad -3x0
(right) task-status on-over [does-on-over face (n)]
]
]
]
star-callback: func [
name
n
field
][
update-stars name n
tasks/:cur-task/(to-word field): n
]
make-stars: func [
name [string!]
type [word!]
field
][
collect [
keep [pad 0x-13]
repeat n 10 [
keep to-set-word rejoin [name "-" n]
keep type
keep/only compose [star-callback (name) (n) (field)]
keep compose/deep [
on-over [
face/data: not face/data
info-text/text: either face/data [
rejoin ["Rate this task " (n)]
][""]
]
]
keep [pad -8x0]
]
]
]
update-stars: func [name n][
repeat i n [
set to-path reduce [to-word rejoin [name "-" i] 'text] "★"
]
repeat k 10 - n [
set to-path reduce [to-word rejoin [name "-" k + n] 'text] "☆"
]
]
set-ref-checkboxes: func [n][
clear refinements/data
checks: switch tasks/:n/ref-status [
correct [tasks/:n/refinements]
incorrect [last load last tasks/:n/ref-tries]
unattepmted [copy []]
]
foreach-face refs [
case [
face/type = 'check [
if face/data: to-logic find checks w: to-word face/text [
append refinements/data w
]
]
face/type = 'field [
p: find tasks/:n/refinements 'limit
either p [face/text: form p/2 append refinements/data copy/part p 2][clear face/text]
]
]
]
]
update-task-stats: does [
repeat n length? tasks [
left: to-path reduce [to-word rejoin ["Stat-d-" n] 'color] ; dialected
right: to-path reduce [to-word rejoin ["Stat-r-" n] 'color] ; refinement-based
set left do select clrs tasks/:n/dial-status
set right do select clrs tasks/:n/ref-status
]
]
load-task: func [n /local t sol][
t: tasks/:n
id/text: rejoin ["Task " form n]
if mode = 'sandbox [insert id/text "Custom "]
clear input-text/text
unless empty? txt: t/input [input-text/text: mold txt]
clear goal-text/text
unless empty? txt: t/goal [goal-text/text: mold txt]
cur-task: n
clear dialected-delimiter/text
clear dialected-result/text
clear dialected-call/text
clear refinement-delimiter/text
clear refinement-result/text
clear refinement-call/text
clear task-notes/text
task-notes/text: copy tasks/:n/notes
dialected-result/color: do select clrs-b tasks/:n/dial-status
refinement-result/color: do select clrs-b tasks/:n/ref-status
set-ref-checkboxes n
sol: dialected-delimiter/text: switch tasks/:n/dial-status [
correct [copy tasks/:n/dial-solution]
incorrect [copy last tasks/:n/dial-tries]
]
unless empty? sol [check-dialected dialected-delimiter]
sol: refinement-delimiter/text: switch tasks/:n/ref-status [
correct [copy tasks/:n/ref-solution]
incorrect [mold first load last tasks/:n/ref-tries]
]
unless empty? sol [check-refinements refinement-delimiter]
update-stars "star" tasks/:n/difficulty
update-stars "importance" tasks/:n/importance
update-stars "star-d" tasks/:n/dialected-rating
update-stars "star-r" tasks/:n/refinement-rating
]
prev-task: does [
cur-task: cur-task - 1
if zero? cur-task [cur-task: len]
load-task cur-task
]
next-task: does [
cur-task: cur-task % len + 1
load-task cur-task
]
make-rule: function [
data "Content from user input field"
/local fn arg
][
compose=: [
'compose/deep/only | 'compose/only/deep | 'compose/deep | 'compose/only | 'compose
| 'reduce
]
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
]
; compose or reduce
parse val [set fn compose= set arg [string! | block!]] [
do compose [(fn) arg]
]
'else [
val
]
]
]
'else [ ; char! integer! string!
val
]
]
]
check-dialected: func [
face
/local result correct? unknown
][
unknown: false
call: reduce ['split <input>]
append/only call face/data
dialected-call/text: mold/only call
tsk: tasks/:cur-task
if mode = 'sandbox [
tsk/input: load input-text/text
tsk/goal: load goal-text/text
]
if error? set/any 'err try [
result: mold split load input-text/text make-rule face/data
dialected-result/text: result
][
dialected-result/text: "I don't understand that. Maybe try reduce/compose."
unknown: true
]
repend tsk/dial-tries [now copy face/text]
correct?: equal? result mold load goal-text/text
dialected-result/color: reduce pick [color-right color-wrong] correct?
either correct? [
info-text/text: "Correct!"
tsk/dial-status: 'correct
tsk/dial-solution: copy face/text
set to-path reduce [to-word rejoin ["Stat-d-" cur-task] 'color] color-right
][
info-text/text: either unknown ["Error"]["Wrong!"]
unless tsk/dial-status = 'correct [
set to-path reduce [to-word rejoin ["Stat-d-" cur-task] 'color] color-wrong
tsk/dial-status: 'incorrect
]
]
]
check-refinements: func [
face
/local result correct?
][
call: copy [<input>]
delim: face/data
tsk: tasks/:cur-task
if mode = 'sandbox [
tsk/input: load input-text/text
tsk/goal: load goal-text/text
]
;either error? set/any 'err try [do mold delim][
; refinement-result/data: "I don't understand that."
; info-text/text: "Error"
; refinement-call/text: mold/only call
; refinement-result/color: color-wrong
; append tsk/ref-tries now
; append/only tsk/ref-tries mold reduce [face/data copy refinements/data]
; unless tsk/ref-status = 'correct [
; set to-path reduce [to-word rejoin ["Stat-r-" cur-task] 'color] color-wrong
; tsk/ref-status: 'incorrect
; ]
; if found: find r-data: copy refinements/data 'limit [lmt: found/2 remove next found]
; insert/only call to-path append copy [split] r-data
; if found [append call lmt]
; refinement-call/text: mold/only call
; ;print mold err
;][
case [
find [word! get-word! path! get-path!] type?/word :delim [append call delim delim: get :delim]
all [
block? delim
empty? intersect refinements/data [value rule]
not find/match trim/head face/text #"["
word? delim/1
any-function? get delim/1
][append call delim delim: do delim]
'else [append/only call delim]
]
refinement-result/text: result: mold either empty? refinements/data [
insert call 'split
split-r load input-text/text :delim
][
if found: find r-data: copy refinements/data 'limit [lmt: found/2 remove next found]
insert/only call to-path append copy [split] r-data
if found [append call lmt]
split-r/with load input-text/text :delim refinements/data
]
refinement-call/text: mold/only call
append tsk/ref-tries now
append/only tsk/ref-tries mold reduce [face/data copy refinements/data]
correct?: equal? result mold load goal-text/text
refinement-result/color: reduce pick [color-right color-wrong] correct?
either correct? [
info-text/text: "Correct!"
tsk/ref-status: 'correct
tsk/ref-solution: copy face/text
tsk/refinements: copy refinements/data
set to-path reduce [to-word rejoin ["Stat-r-" cur-task] 'color] color-right
][
info-text/text: "Wrong!"
unless tsk/ref-status = 'correct [
set to-path reduce [to-word rejoin ["Stat-r-" cur-task] 'color] color-wrong
tsk/ref-status: 'incorrect
]
]
;]
]
move-focus: func [face][set-focus get select tabs face] ; naive tab support
difficulty: make-stars "star" 'star2 "difficulty"
importance: make-stars "importance" 'star2 "importance"
stars-d: make-stars "star-d" 'star2 "dialected-rating"
stars-r: make-stars "star-r" 'star2 "refinement-rating"
confirm: has [answer][
answer: false
view/no-wait/flags [
Title "Save changes?"
below
text "Do you want to save the current session?"
across
button "Yes" [answer: true unview]
button "No" [answer: false unview]
][modal no-min no-max]
do-events
answer
]
make-filename: does [
t: now
rejoin [t/year "-" t/month "-" t/day "-" t/hour "-" t/minute "-" to-integer t/second]
]
load-session: func [
/latest /local files
][
default-dir: select [predefined: %sessions/ sandbox: %custom-sessions/] mode
session-file: either latest [
files: sort/reverse read default-dir
unless empty? files [
while [
all [
not find/match files/1 "practice-split-"
not tail? files
]
][
files: next files
]
if files [rejoin [default-dir files/1]]
]
][
request-file/file default-dir
]
either session-file [
either exists? session-file [
tasks: reduce load session-file
; check the format
either empty? difference extract tasks/1 2 words-of task-stats [
update-task-stats
info-text/text: form session-file
load-task 1
][
alert "Invalid format! Starting a new session."
start-session
]
][
start-session
]
][
start-session
]
]
save-session: does [
dirize session-file
write session-file mold tasks
info-text/text: form rejoin [session-file " was saved"]
]
start-session: does [
tasks: init-tasks
update-task-stats
session-file: rejoin [
normalize-dir default-dir
'practice-split-
make-filename
%.red
]
]
new-session: does [
start-session
update-task-stats
load-task 1
]
switch-modes: does [
if confirm [save-session]
switch mode [
predefined [
mode: 'sandbox
mode-btn/text: "Predefined mode"
input-text/enabled?: true
input-text/color: (linen + 20.20.20)
goal-text/enabled?: true
goal-text/color: (linen + 20.20.20)
id/text: "Custom task 01"
make-dir %custom-sessions/
load-session/latest
]
sandbox [
mode: 'predefined
mode-btn/text: "Sandbox mode"
input-text/enabled?: false
input-text/color: (linen - 5.5.5)
goal-text/enabled: false
goal-text/color: (linen - 5.5.5)
id/text: "Task 01"
load-session/latest
]
]
]
;print "start"
view/options compose [
title "Compare dialected and refinement-based split"
backdrop linen
style task: button 30x30 data off
style task-status: base 15x3 white data off
style lbl: text 375 font-color black font-size 10
style dark: field 360 (linen - 5.5.5) disabled font-color black font-size 12
style dark-short: field 310 (linen - 5.5.5) font-color black font-size 10
style label: text 55 font-size 10 font-color black
style fld: field 320 (linen + 20.20.20) font-color black font-size 10 data off
style hlp: button 80X35
style btn: button 35
style question: text 320 font-color black font-size 10
style star: base 28x28 linen "☆" font-size 23 font-color gold
style star2: base 25x25 linen "☆" font-size 20 font-color gold
below
across space 5x5 (task-buttons) return
across (task-boxes) return
across
id: text 200 "Task 01" font-size 15 font-color black
pad 470x5
mode-btn: button "Sandbox mode" [switch-modes]
on-over [info-text/text: either over-mode: not over-mode [select mode-prompt mode][""]]
return pad 0x-10
across
panel linen [
below
lbl "Here is your input:"
pad 0x-10
input-text: dark
]
panel linen[
below
lbl "Your goal is to get this result:"
pad 0x-10
goal-text: dark
]
return
pad 0x-15
below
across
panel linen [ ; Dialected
below
lbl "Please specify appropriate delimiter for dialected split:"
pad 0x-10 across
dialected-delimiter: fld [check-dialected face]
on-over [info-text/text: either over-dial: not over-dial [prompt][""]]
on-key-down [if event/key = tab [move-focus 'dialected-delimiter]]
dialected-btn: btn "Split" [check-dialected dialected-delimiter]
on-key-down [if event/key = tab [move-focus 'dialected-btn]]
return below
panel 375x50 linen [
text 350x50 "[before | after | first | last | once | times | into | as-delim | first by then by]"
font-size 10 font-color black
]
across label "Your call:"
dialected-call: dark-short "" return
across label "Result"
dialected-result: dark-short
]
panel linen[ ; Refinemets
below
at 0x0 refinements: field hidden data []
lbl "Please specify delimiter and select appropriate refinements:"
pad 0x-10 across
refinement-delimiter: fld [check-refinements face]
on-over [info-text/text: either over-ref: not over-ref [prompt][""]]
on-key-down [if event/key = tab [move-focus 'refinement-delimiter]]
refinement-btn: btn "Split" [check-refinements refinement-delimiter]
on-key-down [if event/key = tab [move-focus 'refinement-btn]]
return below
refs: panel linen [
origin 0x0
style ref: check 70 [alter refinements/data to-word next face/text]
c1: ref "/before" on-key-down [if event/key = tab [move-focus 'c1]]
c3: ref "/first" on-key-down [if event/key = tab [move-focus 'c3]]
c5: ref "/parts" on-key-down [if event/key = tab [move-focus 'c5]]
c7: ref "/rule" on-key-down [if event/key = tab [move-focus 'c7]]
pad -2x5 text 30 "/limit"
return
pad 0x-15
c2: ref "/after" on-key-down [if event/key = tab [move-focus 'c2]]
c4: ref "/last" on-key-down [if event/key = tab [move-focus 'c4]]
c6: ref "/group" on-key-down [if event/key = tab [move-focus 'c6]]
c8: ref "/value" on-key-down [if event/key = tab [move-focus 'c8]]
pad -2x0
lmt: field 40 hint "limit" on-unfocus [
either integer? face/data [
either find refinements/data 'limit [
put refinements/data 'limit face/data
][
append refinements/data compose [limit (face/data)]
]
][
if found: find refinements/data 'limit [remove/part found 2]
]
]
on-key-down [if event/key = tab [move-focus 'lmt]]
]
across label "Your call:"
refinement-call: dark-short "" return
across label "Result"
refinement-result: dark-short
] return
question "How convenient was the dialected solution?" pad 85x0
question "How convenient was the refinement-based solution?" return
(stars-d) pad 190x15 (stars-r) return
question "How hard was this task?"
pad 90x0 question "Please write down your comments on this task:" return
(difficulty) return
question "How important is this usecase?" return
(importance)
pad 190x-35 task-notes: area 370x70 (linen + 20.20.20)
on-unfocus [tasks/:cur-task/notes: copy face/text]
return
hlp "General Help" [show-help/with help-new];%help-new.txt]
hlp "Dialect notes" [show-help/with help-dia];%help-dialect.txt]
hlp "Refinements notes" [show-help/with help-ref];%help-refinement.txt]
pad 390x10
new-btn: button "New" [if confirm [save-session] new-session]
on-over [info-text/text: either over-new: not over-new [select new-prompt mode][""]]
load-btn: button "Load" [if confirm [save-session] load-session]
on-over [info-text/text: either over-load: not over-load [select load-prompt mode][""]]
on-create [load-task 1]
return
info-text: text 775x18 (linen - 10.10.10) "" font-color black font-size 10
][
text: "Practice Split"
actors: make object! [
on-create: function [face event] [
make-dir %sessions/
either last sort read %sessions/ [load-session/latest][start-session]
info-text/text: rejoin ["Session: " copy/part session-file find session-file dot]
set-focus dialected-delimiter
]
on-close: function [face event] [save-session]
on-key: function [face event] [
switch event/key [
F1 [show-help/with help-new];%help-new.txt]
F2 [show-help/with help-dia];%help-dialect.txt]
F3 [show-help/with help-ref];%help-refinement.txt]
page-up [prev-task]
page-down [next-task]
]
]
]
]
]