-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils-flow.r
565 lines (494 loc) · 12.4 KB
/
utils-flow.r
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
REBOL [
; -- Core Header attributes --
title: "program flow control utility code."
file: %utils-flow.r
version: 1.0.0
date: 2013-9-12
author: "Maxim Olivier-Adlhoch"
purpose: {Collection of generic, re-useable program flow functions}
web: http://www.revault.org/modules/utils-flow.rmrk
source-encoding: "Windows-1252"
note: {slim Library Manager is Required to use this module.}
; -- slim - Library Manager --
slim-name: 'utils-flow
slim-version: 1.2.1
slim-prefix: none
slim-update: http://www.revault.org/downloads/modules/utils-flow.r
; -- Licensing details --
copyright: "Copyright © 2013 Maxim Olivier-Adlhoch"
license-type: "Apache License v2.0"
license: {Copyright © 2013 Maxim Olivier-Adlhoch
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.}
;- / history
history: {
2013-01-03 - v1.0.0
-creation
-MULTI-SWITCH() added
v1.0.1 - 2013-09-12
-license changed to Apache v2}
;- \ history
;- / documentation
documentation: ""
;- \ documentation
library: {[
level: 'intermediate
platform: 'all
type: [tool module]
domain: [external-library file-handling]
tested-under: [win view 2.7.8]
support: "same as author"
license: 'Apache-v2
see-also: http://www.revault.org/modules/utils-flow.rmrk
]}
]
slim/register [
;--------------------------------------
; unit testing setup
;--------------------------------------
;
; test-enter-slim 'utils-flow
;
;--------------------------------------
;- .
;-----------------------------------------------------------------------------------------------------------
;-
;- CLASSES
;-
;-----------------------------------------------------------------------------------------------------------
;- .
;-----------------------------------------------------------------------------------------------------------
;-
;- FUNCTIONS
;-
;-----------------------------------------------------------------------------------------------------------
;--------------------------
;- multi-switch()
;--------------------------
; purpose: Iteratively descend the tree, given one or more items to select with.
; when the path is given as a block, each element of the block is one selection.
;
; We match items using the 'SWITCH metaphor, rather than 'SELECT.
;
; this means we can supply several items which match the same block.
;
; we must wrap the data to return within a block, since we will execute the block (just like switch)
;
; inputs: -blocks hierarchy
; -browsing path (as a lit path or a block of items to match, at each depth).
; -parens are legal within path BLOCK!
;
; returns: The item within tree which your path refers to.
;
; When browsing is not able to find your path (not in tree or path is deeper than tree)
; we return none.
;
;
; notes: -none-transparent
;
; -when path arguments are given using the 'PATH! datatype, they are converted to a block of words/parens,
;
; -paren! path items are evaluated before browsing.
;
; -if you provide partial paths within the tree, you MUST also call multi-switch with the /only refinement.
;
; tests:
; fruits: [
; red green [
; apple [ 1 ]
; tomato [ 2 ]
; ]
;
; orange [
; orange [ 3 ]
; ]
;
; blue purple black [
; berry [ 4 ]
; blueberry [ 5 ]
; blackberry [ 6 ]
; ]
; ]
;
; probe multi-switch [black berry] fruits
; probe multi-switch 'red/tomato fruits
; probe multi-switch 'red/tomato/joj fruits
; probe multi-switch [green orange] fruits
; probe multi-switch/only 'red fruits
;--------------------------
select*: :select
all*: :all
multi-switch: funcl [
path [block! lit-path! path! word!]
tree [block!]
/only "do not execute the last item, only return the block as-is"
/select "use select-mode semantics(chose what follows, not just the next block), implies /only"
/all
/catch
][
;vin "multi-switch()"
result: none
if select [only: true]
if lit-path? path [
path: to-block path
]
if path? path [
path: to-block path
]
path: compose [(path)]
;v?? path
;v?? tree ; careful, on long blocks this can jam app
foreach item path [
;vprint "======================"
;v?? item
;vprint ["tree: " copy/part mold tree 50 ]
either block? tree [
if paren? item [
item: do item
]
; switch type?/word :item [
; word! [
; ;vprint "WORD !!!!"
; item: to-lit-word item
; ]
; none! [
; ;vprint "NONE !!!!"
; ; we return the datatype, not the value
; ; this is because none, as a rule, is a no-op which creates endless cycles in parse loops.
; item: none!
; ]
; ]
;vprobe type? item
;vprobe item
either select [
;vprint "using select-mode"
tree: select* tree :item
][
tree: all* [
blk: find tree :item
blk: find blk block!
first blk
]
]
; parse tree [
; (tree: none)
; some [
; ;set yyy
; ;(v?? yyy)
; here: item
; (
; either tree: find here block! [
; tree: first tree
; ;v?? tree
; ][
; ;---
; ; this is a fallback for the case where we use this function for selection.
; tree: pick here 2
; ]
; here: tail here
; )
; :here
; |
; ;set zzz
; skip
; ;(v?? zzz)
; ]
; ]
; ]
][
; skip the rest of the path, we already failed or found the item.
break
]
]
;v?? tree
; did we browse to where we wanted?
if tree [
result: either only [
tree
][
catch do tree
]
]
;vout
;v?? result
; we return none if browse isn't successful
result
]
;--------------------------
;- deep-select()
;--------------------------
; purpose: like multi-switch but with select semantics.
;
; if series is none! we just stay none transparent and return none as well
;--------------------------
deep-select: funcl [
series [block! none!]
selector [block! lit-path! path! word!]
][
either series [
multi-switch/select selector series
][
none
]
]
;--------------------------
;- at-each()
;--------------------------
; purpose:
;
; inputs:
;
; returns:
;
; notes:
;
; tests:
; test-group [ at-each flow utils-flow.r ] []
; [ total: 0 ( 10 = at-each blk [1 2 3 4] [ total: total + first blk ] ) ]
; end-group
;--------------------------
at-each: func [
'word
series
code
/local ctx rval
][
ctx: context reduce [to-set-word word #[none]]
code: bind/copy code ctx
while [not tail? series] [
set in ctx word series
rval: do code
series: next series
]
rval
]
;--------------------------
;- partial()
;--------------------------
; purpose: attempt to evaluate ALL expressions, but notify if incomplete.
;
; inputs: block of code to evaluate
;
; returns: returns true when at least one expression returns NONE or FALSE
;
; notes: like 'ALL, the value of the last item is also taken into consideration.
;
; to do:
;
; tests:
;--------------------------
partial: funcl [
eval-block [block!]
][
not all eval-block
]
;--------------------------
;- btype()
;--------------------------
; purpose: a complement to forboth returning a special type based on first element of given block
;--------------------------
btype: funcl [
data
][
case [
(tail? data) [
#[none]
]
(block? first data) [
'block
]
'default [
'item
]
]
]
;--------------------------
;- forboth()
;--------------------------
; purpose: execute a block on each item of a pair of datasets.
;
; inputs: using /deep will enter sub-blocks and fail if the whole block structure is not symmetric
;
; returns: a block containing both series at their head
;--------------------------
forboth: funcl [
'serie-a [word!]
'serie-b [word!]
body [block!]
/deep "go into sub-blocks"
/trace "print trace of function registers while looping"
][
result: none
stack-a: copy []
stack-b: copy []
word-a: serie-a
word-b: serie-b
serie-a: get serie-a
serie-b: get serie-b
if trace [
?? serie-a
?? serie-b
?? word-a
?? word-b
]
ctx: copy []
append ctx reduce [to-set-word word-a #[none]]
append ctx reduce [to-set-word word-b #[none]]
append ctx compose/only [eval-blk: (body)]
if trace [?? ctx]
ctx: context ctx
if trace [?? ctx]
; loop over both datasets, and run body setting both series words.
; if the datasets are not symmetric in block structure, we raise an error at that point.
until [
if trace [print "--------------------"]
ba: btype serie-a
bb: btype serie-b
unless deep [
ba: not not ba
bb: not not bb
]
if trace [
?? ba
?? bb
?? serie-a
?? serie-b
?? stack-a
?? stack-b
]
case [
((ba) <> (bb)) [ ; if we hit a structure difference
to-error "structure difference"
]
(all [empty? stack-a empty? serie-a])[ ; we are at end of all data.
if trace [print "TAIL!"]
result: reduce [head serie-a head serie-b]
]
(ba = 'block) [
if trace [print "SUB BLOCK DETECTED"]
append/only stack-a next serie-a
append/only stack-b next serie-b
serie-a: first serie-a
serie-b: first serie-b
false
]
(none? ba) [
if trace [print "AT END OF SUBBLOCK"]
serie-a: last stack-a
serie-b: last stack-b
remove back tail stack-a
remove back tail stack-b
false
]
'default [
if trace [ print ">>>>"]
set in ctx word-a serie-a
set in ctx word-b serie-b
do ctx/eval-blk
serie-a: next serie-a
serie-b: next serie-b
false
]
]
]
;print "WE SHOULD NEVER REACH HERE!"
result
]
;--------------------------
;- getc()
;--------------------------
; purpose: low-level character input method.
;--------------------------
getc: has [ console-port char ][
if console-port: open/binary [scheme: 'console] [
wait console-port
char: to-char first console-port
;?? char
close console-port
char
]
]
;--------------------------
;- askchar()
;--------------------------
; purpose: like ASK but returns after a single char, from a given list.
;
; inputs: a block of options which are pairs of [ char! word! ...]
; /fail allows you to fail if wrong character is pressed, we return none in such a case.
;
; returns:
;
; notes: - unless you use /fail the function doesn't return until a valid char is pressed.
; - you CAN use any value a part char! for the value part of the selection
;
; to do:
;
; tests:
;--------------------------
askchar: funcl [
selection [block!]
/fail
][
vin "askchar()"
spec: copy []
either fail [
char: getc
rval: select selection char
][
forever [
char: getc
if find selection char [
rval: select selection char
break
]
]
]
vout
rval
]
;--------------------------
;- confirm()
;--------------------------
; purpose: a helper around the askchar method for y/n confirmation.
;
; inputs:
;
; returns: true or none.
;
; notes:
;
; to do:
;
; tests:
;--------------------------
confirm: funcl [
/msg message [ string! block! ]
][
if msg [
if block? message [
message: rejoin message
]
print message
]
askchar [
#"y" #[true]
#"Y" #[true]
#"n" #[none]
#"N" #[none]
]
]
]
;------------------------------------
; We are done testing this library.
;------------------------------------
;
; test-exit-slim
;
;------------------------------------