-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaimio.dm
3117 lines (2358 loc) · 93.9 KB
/
daimio.dm
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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<div class="page-header" id="welcome">
<h2>Preface</h2>
</div>
This document serves as a primer, tutorial, specification, test suite and REPL for the Daimio language.
Daimio is a framework for building programmable web applications, as well as the dataflow language used within that framework.
On this page all Daimio statements are wrapped in braces. Any line which begins with an open brace will be processed as a Daimio statement, and the following line indicates the desired outcome. Green means it passed, red indicates failure. Output is converted to JSON for display in the REPL and the examples below.
<div class="page-header" id="pronunciation">
<h2>Pronunciation guide</h2>
</div>
<h4>Daimio introduces as little syntax as possible, but there's a few symbols you may not be familiar with. Here's how you read them.</h4>
<strong>></strong> reads 'put', because it looks like a sideways highway
<strong>@</strong> reads 'port', because it looks like a wormhole
<strong>_</strong> reads 'read', as in read-only
<strong>$</strong> reads 'space variable', or 'spacevar'
<h4>Combo elements:</h4>
<strong>>@</strong> reads 'put port'
<strong>>$</strong> reads 'put spacevar'
<strong>__</strong> reads 'read last'
<h5>We'll look at each of those in more detail later, but now you know how to say it!</h5>
<div class="page-header" id="id_daimio_primer">
<h2>Daimio Primer</h2>
</div>
Some basics
numbers, natural and otherwise
{65535}
65535
{4.669200}
4.6692
{-3.14159}
-3.14159
strings are double quoted for protection
{"Help I'm trapped in a string factory"}
Help I'm trapped in a string factory
or colonized if they're small
{:Liechtenstein}
Liechtenstein
lists use parens and whitespace
{( 2 3 5 )}
[2,3,5]
{(:one 1 :two 2)}
["one",1,"two",2]
sometimes lists sneak inside other lists
{( (2 3 5 7) (4 6 8 9) )}
[[2,3,5,7],[4,6,8,9]]
{( () (()) (() (())) )}
[[],[[]],[[],[[]]]]
list can also be keyed
{* (:a 1 :b 2)}
{"a":1,"b":2}
and heterogenous
{* (:a {* (:aa 1 :ab 2)} :b 2 )}
{"a":{"aa":1,"ab":2},"b":2}
Commands
commands have a handler and a method
{list count}
0
and named parameters
{list count data (13 17 19)}
3
parameter order is irrelevant
{math add value 22 to 20}
42
{math add to 20 value 22}
42
pipes link commands
{(13 17 19) | list count}
3
{22 | math add to 20}
42
aliases reduce typing
{(13 17 19) | count}
3
{22 | add 20}
42
double underscore reads the last value
{21 | add __}
42
{21 | add 21 to __}
42
{21 | add __ to 21}
42
{21 | add __ to __}
42
Comments
slash comments one segment
{/string join}
{401 /comment | add 1}
402
double slash comments all remaining segments
{401 //comment | add 1}
401
{// 401 | add 1}
Blocks
blocks are delimited by begin/end, and contain strings
{begin foo}some string{end foo}
some string
or code
{begin bar}{21 | add 21}{end bar}
42
or both
{begin block} {:hello} world {end block}
hello world
double quotes create inline blocks
{"{:hello} world"}
hello world
blocks are lambdas
{"{__ | add 1}" | map data ( 1 2 3 )}
[2,3,4]
{begin block | map data ( 1 2 3 )}{__ | add 1}{end block}
[2,3,4]
__in reads the process input
{( 1 2 3 ) | map block "{__in | add 1}"}
[2,3,4]
__ reads the last value, which is the process input here
{( 1 2 3 ) | map block "{__ | add 1}"}
[2,3,4]
further inside a pipeline __ reads the previous pipeline segment
{( 1 2 3 ) | map block "{__ | add 5 | (__ __in)}"}
[[6,1],[7,2],[8,3]]
More pipes
a piped value fills the first available parameter
{math add value 10 to 32}
42
{10 | math add to 32} {// here it fills 'value'}
42
{32 | math add value 10} {// here it fills 'to'}
42
a double pipe prevents implicit param filling
{32 || math add value 10} {// 'to' is unfilled}
10
{32 || math add value 10 to __}
42
Labels
pipe values can be labeled for later use
{8 | >eight | (_eight _eight 2) | add _eight | add}
42
but pipeline vars only work within the same block
{_eight}
and can't be modified
{2 | >two | 4 | >two | (_two)}
[2]
Variables
space variables are mutable
{2 | >$foo | ( 1 2 3 ) | >$foo}
[1,2,3]
and available across all blocks in the same space
{$foo | add $foo}
[2,4,6]
Lists
you can peek into data structures
{* (:a 1 :b 2 :c 3) | list peek path :b}
2
{(2 4 6) | peek "#1"}
2
{* (:a (1 2) :b (2 3) :c (3 4)) | peek (:b "#1")}
2
dots are peek sugar
{(1 2 3) | __.#2}
2
{* (:a (1 2) :b (2 3) :c (3 4)) | __.b.#1}
2
poke to push
{(1 2 3) | list poke value 4}
[1,2,3,4]
or otherwise modify
{* (:a 1 :b 2 :c 3) | poke 4 path :d}
{"a":1,"b":2,"c":3,"d":4}
dots are poke sugar too
{5 | >$foo.#2}
[1,5,3]
And that's everything there is to know about Daimio. Well, almost. Let's explore a couple ideas in greater depth.
<div class="page-header" id="id_commands">
<h2>In Depth: Commands</h2>
</div>
Commands in Daimio provide all of the functionality in the language -- everything else is just for wiring commands together. In particular, control statements (like 'if' and 'cond') and looping constructs (like 'each' and 'map') are commands rather than built-in primitives.
There's a few disadvantages to this approach in a traditional language, like awkward syntax for if-then-else (which becomes a function that takes a boolean expression and two callbacks). But we find that in a dataflow language like Daimio the convention is quite natural, and any semantic clumsiness is outweighed by the advantages:
- no distinction between built-ins and application functionality means you can easily create new low-level control constructs.
- "everything is a command" reduces conceptual weight: all commands return a value, all commands take named parameters, parameter evaluation timing is consistent, and so on.
- facilities that affect commands (like aliases) can be used on anything in the system -- no special cases.
We saw in the primer that commands have a handler, a method and named parameters, and that parameter order is irrelevant.
{list range length 3 start 1 step 2}
[1,3,5]
{list range step 2 length 3 start 1}
[1,3,5]
We also saw that you can pipe a value in to the command, and it will fill the "first available" empty parameter slot.
{3 | list range}
[1,2,3]
What does "first available" mean if parameter order is irrelevant? It means the order the parameters are given in the command definition, which also happens to mirror the order in the REPL's autocompletion dialog.
{2 | list range length 3}
[2,3,4]
In the case of {list range}, the order is 'length', 'start', 'step'.
{3 | list range start 2}
[2,3,4]
{3 | list range start 2 length 3}
[2,5,8]
Most commands only have one parameter that would generally take a pipe value. In those cases we can use an alias that is preconfigured with that parameter name.
{3 | range}
[1,2,3]
{range 3}
[1,2,3]
The above works because the alias 'range' is replaced with 'list range length'. Aliases work by simple substitution: if the first word of a command matches something in the alias list, it is replaced.
What if we don't supply a value for the trailing parameter name, like 'length'?
{range}
[]
No error -- interesting. 'add' is an alias for 'math add value'.
{2 | add 3}
5
{add 2 to 3}
5
So what about these?
{5 | add}
5
{add to 5}
5
{range start 3}
[]
{3 | range start 3}
[3,4,5]
Ah. So it looks like the trailing param value is negated if the word after the alias is a parameter name instead of a param value. (Param names are always bare words, param values never are.) It then becomes filled in through the pipe via the natural piping process. Interesting.
We just learned that param values are never bare words. What kinds of things can be param values?
- numbers: 1, 0.45
- strings: "foo" :foo
- lists: ((1 2) (3 4))
- pipelines: {:foo}
- variables: _xyz $abc
There are two things that can be pipeline segments but can't be parameter values:
- bare commands, e.g. 'math add' doesn't work -- put it in a pipeline
- 'put' expressions: >x >$y >@z
<!--
(:barbera :belvest :brioni)
["selvedge","balmoral","aglet","placket","plimsolls"]
-->
<div class="page-header" id="id_lists">
<h2>In Depth: Lists</h2>
</div>
Lists are the basic data structure of Daimio. Spaces separate items. List items can be any valid expression.
Notice that data structures are implicitly converted to JSON when forced to take string form.
A list of random numbers:
{(8 6 7 5 3 0 9)}
[8,6,7,5,3,0,9]
A list of pleonasms:
{("free gift" "true fact" "revert back" "hot water heater" "tired cliche")}
["free gift","true fact","revert back","hot water heater","tired cliche"]
Numbers and strings:
{(1 "a sandbox" 2 "a sandbox")}
[1,"a sandbox",2,"a sandbox"]
A list of lists:
{((1 2 3) (:once :twice :thrice) (:primary :secondary :tertiary))}
[[1,2,3],["once","twice","thrice"],["primary","secondary","tertiary"]]
The first three ordinals:
{( () (()) (() (())) )}
[[],[[]],[[],[[]]]]
Keyed lists
A keyed list (aka hash, map, hash map, dictionary, associative array, key-value store, etc etc etc) is a function that takes keys and returns values. Every list in Daimio can take keys. There's a special command for transforming an unkeyed list into a new keyed list called 'list pair'.
Here is the command written out:
{list pair data (:one :first :two :second)}
{"one":"first","two":"second"}
And in its much more common aliased form:
{* (:one :first :two :second)}
{"one":"first","two":"second"}
As you can see, the * operator (which is really just an alias for a command) uses the first value in the list as a key, the second as its value, the third as the second key, the fourth as its value, and so on. While this seems a bit messy on a single line, with proper whitespacing it's very easy to read.
Lists -- including keyed lists -- are always sorted, so we can use the #N notation on them:
{* (:one :first :two :second) | >$x || $x.#2}
second
[Integer keys in maps can mess up the sorting in the JS implementation]
a list of hashes:
{( {* (:one 1 :two 2)} {* (:three 3 :four 4)} )}
[{"one":1,"two":2},{"three":3,"four":4}]
a nested hash:
{* (:A {* (:one 1 :two 2)} :B {* (:three 3 :four 4)})}
{"A":{"one":1,"two":2},"B":{"three":3,"four":4}}
---- talk about what can go in a list
A list can have commands in it
{({string split value "shantung weft repp slub" on " "} 1 2 4 8)}
[["shantung","weft","repp","slub"],1,2,4,8]
<div class="page-header" id="id_pipes">
<h2>In Depth: Pipes</h2>
</div>
You can use the pipe (<code>|</code>) to pass the output of one command into an input of another.
Split, then join:
{string split value "shantung weft repp slub" on " " | string join on ", "}
shantung, weft, repp, slub
Split, filter, join:
{string split value "shantung weft repp slub" on " " | string grep on :s | string join on ", "}
shantung, slub
In some ways the pipe is syntactic sugar for <strong>commands as parameter values</strong>, though the two differ slightly in implementation. The following commands are essentially equivalent to the above pipelines.
Split, then join:
{string join on ", " value {string split value "shantung weft repp slub" on " "}}
shantung, weft, repp, slub
Split, filter, join:
{string join on ", " value {string grep on :s value {string split value "shantung weft repp slub" on " "}}}
shantung, slub
Pipelines can make complicated Daimio statements much easier to read, and lend themselves to dataflow-style programming. They're generally preferred over embedded commands.
You can also pipe parameters, which sometimes improves readability:
{(:bebop :hardbop :cool :swing) | string join on ", "}
bebop, hardbop, cool, swing
A single pipe at the end of a command passes the value through:
{:mypipes|}
mypipes
A double pipe in a command squelches the pipe value:
{:doublepipes ||}
Which is equivalent to:
{:asdf | ""}
Squelching is useful sometimes, when you want to continue a pipeline without passing the previous value:
{:asdf | string join value (:one :two)}
oneasdftwo
{:asdf || string join value (:one :two)}
onetwo
Though you could also do that like this:
{:asdf ||}{string join value (:one :two)}
onetwo
the <code>__</code> is pronounced "magic," because "double underscore" is a mouthful.
the add command is powerful.
{21 | (__ __) | add}
42
{20 | (__ __) | add 1 | add}
42
{10 | (__ __) | add __ | add 1 | add}
42
Magic Pipe Tests
The magic pipe has two uses: to explicitly connect two segments, and to access the Process's input value
Case 1: explicit connection. Each of these also has an implicit connection, but only the first example uses it because the others have no additional param space (add only takes two params).
{21 | add __}
42
{21 | add 21 to __}
42
{21 | add __ to 21}
42
{21 | add __ to __}
42
Case 1a: blocking implicit connection. The double pipe doesn't pass values implicitly, but you can still use the magic pipe to explicitly link them. Useful for commands that have multiple parameters.
{42 || add __}
42
{21 || add 21 to __}
42
Case 1b: carry along. You can use a pipe to carry the value through segments.
{42 | __}
42
{42 | __ | __}
42
Case 1c: duplication. You can use pipes in lists to duplicate the previous value
{42 | (__)}
[42]
{42 | (__ __)}
[42,42]
{42 | (:x __ __)}
["x",42,42]
{42 | (__ :x __)}
[42,"x",42]
{42 | (__ __ :x)}
[42,42,"x"]
{21 | add (__)}
[42]
{21 | add (__ __)}
[42,42]
{21 | add (-1 __ __)}
[20,42,42]
{21 | add (__ -1 __)}
[42,20,42]
{21 | add (__ __ -1)}
[42,42,20]
Case 2: process input
-- "{__}" links to process input
-- {__} links to previous value
so: if it's in a string, it's input.
if it's in a list or param value it's previous value.
{(1 2 3) | map block "{__ | add 4}"}
[5,6,7]
{(1 2 3) | map block "{add __ to 4}"}
[5,6,7]
{(1 2 3) | map block "{add __ to __}"}
[2,4,6]
{(1 2 3) | map block "{__}"}
[1,2,3]
{(1 2 3) | map block "{__ | __}"}
[1,2,3]
{(1 2 3) | map block "{__ | __ | add 1}"}
[2,3,4]
{(1 2 3) | map block "{__ | add 1 | __ | add 1}"}
[3,4,5]
Case 2a: block-level access. Multiple pipelines in a block can each access the process input.
{begin foo | each data (1 2 3)} {__ | add 3} x {__ | add 7} ::{end foo}
4 x 8 :: 5 x 9 :: 6 x 10 ::
{begin foo | each data (1 2 3)} {add 3 to __} x {add __ to 7} ::{end foo}
4 x 8 :: 5 x 9 :: 6 x 10 ::
You can reframe the above like this:
{(1 2 3) | each block "{__ | (" " {__in | add 3} " x " {__in | add 7} " ::") | join}"}
4 x 8 :: 5 x 9 :: 6 x 10 ::
Notes:
To connect to the process input you must explicitly add the magic pipe to the first segment.
{(1 2 3) | map block "{__ | add to 4}"}
[5,6,7]
{(1 2 3) | map block "{add to 4}" // bad}
[4,4,4]
{(1 2 3) | map block "{__ | add 4}"}
[5,6,7]
{(1 2 3) | map block "{add 4}" // bad}
[4,4,4]
{20 | add {__ | add 2}}
42
{20 | add {__ | add 2} to __}
42
{21 | join (__ " " __) on ""}
21 21
{21 | ( {__ | add 1} " " {__ | add 2} ) | join}
22 23
{21 | ( {add 1 to __} " " {add __ to 2} ) | join}
22 23
{21 | >a | ( {_a | add 1} " " {_a | add 2} ) | join}
22 23
{40 | add {__} to 2}
42
{21 | add {__} to __}
42
{21 | add __ to {__}}
42
{21 | add {__} to {__}}
42
{20 | ( {__} 2 __ ) | add}
42
{20 | ( __ 2 {__} ) | add}
42
{20 | ( {__} 2 {__} ) | add}
42
Double pipes are also used to squelch output from a pipeline:
{42 ||}
{(1 2 3) | map block "{__ | add 1 ||}"}
["","",""]
<div class="page-header" id="id_variables">
<h2>In Depth: Variables</h2>
</div>
> implies movement -- putting data somewhere always implies shuffling a copy to somewhere else.
references (in-pipeline mutation, copy-on-write, $foo and _foo (copy-on-read)
----- talk about pipeline vars, injected vars, imported vars, and then space vars
(so pipelines are actually DAGs)
(those labels are only valid inside the block)
(and can only be set once)
Set a space var like this:
{(:one :two :three) | >$bar}
["one","two","three"]
{* (:one 1 :two 2 :three 3) | >$foo}
{"one":1,"two":2,"three":3}
Reference like so:
{$bar}
["one","two","three"]
Reach inside:
{$foo.one}
1
{$foo | list peek path :one}
1
Use an octothorp to find the Nth value in a list:
{$bar.#2}
two
{$bar | list peek path "#2"}
two
Negative indices are also supported:
{$bar.#-1}
three
{$bar | list peek path "#-1"}
three
Works on keyed lists also:
{$foo.#2}
2
We'll see some more ways to reach into variables in a bit.
<div class="page-header" id="id_blocks">
<h2>In Depth: Blocks</h2>
</div>
A block encloses text. [Could be a template, or some Daimio code (or a mix). they're roughly equivalent to a string join + context + var. discuss var scope]
Note that blocks no longer set variables automatically. We may include a {begin $foo}...{end $foo} form in the future to allow automatic var setting, but scope vars should be used carefully so forcing explicit setting is probably good. (We could also consider automatically setting a pipeline var, but for now explicit and simple is better.)
or a string [inlined for the test harness]
[if you put in linebreaks, the output has them also, and then things get weird]
{begin foo}some long multiline string{end foo}
some long multiline string
which is about the same as
{( {:hello} " world" ) | join}
hello world
(except with some extra delayed-execution magic)
Here's a simple block:
{begin foo}Some text{end foo}
Some text
That just returns whatever we put in -- not particularly useful. What if we pipe it into a command?
{begin foo | string split on " "}Some text{end foo}
["Some","text"]
The block can also be stored as a variable:
{begin foo | >$foo ||}Some text{end foo}{$foo}
Some text
{begin foo | >$foo | $foo}Some text{end foo}
Some text
We squelch the output of blocks that don't pipe the 'begin' statement as a convenience. Usually unpiped blocks are built as templates for later use.
Using a block we've previously built:
{$foo | string split on " "}
["Some","text"]
Blocks can also contain Daimio:
{begin foo | >$foo}x{_value}-{end foo}
x-
The each command invokes a Daimio string for every element of a list. These are all equivalent -- the compiler removes the extraneous braces.
{each block "x{__}-" data (1 2 3)}
x1-x2-x3-
{each block {"x{__}-"} data (1 2 3)}
x1-x2-x3-
{each block {{"x{__}-"}} data (1 2 3)}
x1-x2-x3-
This one works differently, because the inner block isn't processed by 'each' and gets escaped into a string on output.
{each block {"{"x{__}-"}"} data (1 2 3)}
x{__}-x{__}-x{__}-
The inner block retains its blockiness though:
{ "{ "x{__}y" }" | map data 1 | __.#1 | map data 42}
["x42y"]
These works like you would expect -- each iteration returns a list, which is JSONified internally.
{each block "{(:x {string join value {__}} "-")}" data (1 2 3)}
["x","1","-"]["x","2","-"]["x","3","-"]
{each block {"{(:x {__ | join} "-")}"} data (1 2 3)}
["x","1","-"]["x","2","-"]["x","3","-"]
These show the strange, sad results of trying to coerce a list into a template.
{:hey | >$value}
hey
{each block (:x $value {$value} "{$value}" {"{$value}"}) data (1 2)}
["x","hey","hey","{$value}","{$value}"]["x","hey","hey","{$value}","{$value}"]
{each block (:x {string join value {$value}}) data (1 2 3)}
["x","hey"]["x","hey"]["x","hey"]
{each block {(:x $value "-")} data (1 2 3)}
["x","hey","-"]["x","hey","-"]["x","hey","-"]
{:hey | >$value}
hey
Pipeline variables, including magic pipes, are reduced prior to templatization.
--> don't use lists as templates!
// {each block (1 __) data (7 8)}
// ["x","hey","hey","hey","hey"]["x","hey","hey","hey","hey"]["x","hey","hey","hey","hey"]
// {each block (:x {string join value {__}}) data (1 2 3)}
// ["x","hey"]["x","hey"]["x","hey"]
// {each block {(:x __ "-")} data (1 2 3)}
// ["x","hey","-"]["x","hey","-"]["x","hey","-"]
{({:8} {8})}
["8",8]
[TODO: upgrade the test suite to allow blocks to spread over lines.]
These two things are almost equivalent:
{begin foo}One{"1 2 3" | string split on " "}Two{end foo}
One["1","2","3"]Two
{string join value (:One {"1 2 3" | string split on " "} :Two)}
One["1","2","3"]Two
Quick 2: Embedded blocks
I put a block in a block for you:
{begin outer}qq {begin inner | add 321}123{end inner} pp {end outer}
qq 444 pp
(Blocks with the same name can't be nested. That would just be weird.)
Quotes in braces
If the nested quotes are in braces, you don't need to use a block:
{string split on " " value {("inside" "here") | string join on " "}}
["inside","here"]
Sometimes nesting quotes in braces and braces in quotes works well:
{join {string split on " " value {"{("Much" "nesting") | string join on " "} is divinest sense" | run}} on " "}
Much nesting is divinest sense
But of course this is prettier
{"{("Much" "nesting") | string join on " "} is divinest sense" | run | split on " " | join on " "}
Much nesting is divinest sense
<div class="page-header" id="id_scope">
<h2>In Depth: Scope</h2>
</div>
so.... blocks don't have a private scope. there's currently pipeline vars and space vars. pipeline vars are single-assignment and can bleed through blocks (but not into called subblocks).
space vars are mutable and persistent. they model state within the space. you should only use them when necessary, and maybe not even then.
TODO: fix this section!
// A pipeline variable created in a block goes away when that block ends:
// {begin block ||}{123 | >$foo}{end block}{foo}
// 123
//
// Variables beginning with '@' are global:
// {begin block}{123 | >$foo}{end block}{$foo}
// 123
//
// Blocks create a new variable:
// {begin block}Hey there{end block}{$block}
// Hey there
//
// Nested blocks are only accessible inside their parent block:
// {begin outer}{begin inner}123{end inner}{end outer}{$inner}
//
//
// Use a global if you need access to it later:
// {begin outer}{begin inner | >$inner}123{end inner}{end outer}{$inner}
// 123
//
// Variables created in the outermost scope (outside of any blocks) can be accessed anywhere:
// {123 | >$x ||}{begin foo}{$x}{end foo}{$x}
// 123
//
// But they aren't actually globals:
// {123 | >$x ||}{begin foo |}{456 | >$x ||}{$x}{end foo}{$x}
// 456123
//
// See what happened there? We overwrote the variable in the inner scope, but when the block ended that scope vanished, leaving behind the original variable value.
//
// Contrast that with a global:
// {123 | >$x ||}{begin foo |}{456 | >$x ||}{$x}{end foo}{$x}
// 456456
//
// Changing a global changes it everywhere, regardless of scope.
//
// Note: you may have heard tell of evil, unhygienic globals afflicting general purpose programming languages. Fortunately, Daimio isn't general purpose. In Daimio globals are cute and cuddly and always floss.
//
// Blocks establish a new variable scope:
// {"asdf" | >$x}{begin foo}{123 | >$x || $x}{end foo}{$x}
//
// But variables starting with '@' are global:
// {"asdf" | >$@x}{begin foo}{123 | >$@x || @x}{end foo}{@x}
<div class="page-header" id="id_peek">
<h2>In Depth: Peek</h2>
</div>
This is a section all about how my list searching got flipped turned upside down. It includes the majority of the peek tests.
{* (:one :one :two :two :three :three) | >$numbers ||}
{* (:one :hashly :two :bashly :three :crashly) | >$hash ||}
{* (:one :local :two "surprise local!" :foo :bar :bar :hello :hash $hash) | >$locals ||}
{( {* (:one :first :two "surprise array!" :locals $locals)} {* (:one :second :two "surprise number also!" :locals $locals)} {* (:one :third :two "surprise me too!" :locals $locals)} ) | >$data ||}
{$hash.one}
hashly
// replacement with command parsing
{$data.{"1"}.one}
second
{$data.{1}.{$numbers.one}}
second
// careful with numerical tests!
{$data.1.one}
second
{$data.#1.one}
first
{$data.#-1.one}
third
{$data.#12.one}
{$data.#-33.one}
// check iteration
{$data.*.one}
["first","second","third"]
{$data.*.locals.hash.one}
["hashly","hashly","hashly"]
{$data.*.locals.foo}
["bar","bar","bar"]
{$data | eq $data.* | then :true else :false}
true
{$data.one | eq $data.*.one | then :true else :false}
false
NOTE ON STARS
$data and $data.* return exactly the same result, but $data.one and $data.*.one return different results. Why is that?
The * pathfind operator lifts the 'guts' of a list up one level, and exposes those items to future pathfinders. So while $data.one finds nothing -- because $data has no key of 'one' -- $data.*.one finds three things, because each item in $data has a key of 'one'.
If however you merely return those lifted results without further operation then this is equivalent to simply removing the keys from the list -- and in $data's case it has no keys, so the two are equal.
{(1 2) | __.*}
[1,2]
{* (:a 1 :b 2) | __.*}
[1,2]
// check stars
{$data | list count}
3
{$data.* | list count}
3
{$data.*.* | list count}
9
{$data.*.*.* | list count}
15
{$data.*.*.*.* | list count}
9
{$data.one}
{$data.*.one}
["first","second","third"]
{$data.*.*.one}
["local","local","local"]
{$data.*.*.*.one}
["hashly","hashly","hashly"]
{$data.*.*.*.*.one}
[]
MORE NOTES ON STARS
Since * exposes its inputs internals to the next operator, you can think of it as a map over the list with the rest of the operators as the block for that map, or the identity block if there are no further operators. This occasionally leads to counterintuitive results like the following, and generally means that * and #N don't play nicely together.
Note that the positional pathfinder #N coerces scalar items into a singleton list, so #1 will continue returning a valid value. This differs from '0' -- the key-based pathfinder -- for unkeyed lists, as 0 does no coercion.
{(1 2 3) | __.0}
1
{(1 2 3) | __.0.0}
{(1 2 3) | __.#1}
1
{(1 2 3) | __.#1.#1}
1
{(1 2 3) | __.#1.#1.#1}
1
{$data.*.*.*.* | ( {__ | unique} {__ | count} )}
[["hashly","bashly","crashly"],9]
Remember, the star operator exposes the list internals to future operators in parallel, so #1 here eats nine scalar values.
{$data.*.*.*.*.#1 | ( {__ | unique} {__ | count} )}
[["hashly","bashly","crashly"],9]
{$data.*.*.*.* | __.#1}
hashly
With star boxing you don't have to split the segments, but remember that the output is always wrapped in a list.
{$data.{(("*" "*" "*" "*"))}.#1}
["hashly"]
{$data.*.*.*.#1}
["local","surprise local!","bar","hello","hashly","local","surprise local!","bar","hello","hashly","local","surprise local!","bar","hello","hashly"]
{$data.*.*.* | __.#1}
local
{$data.*.*.#1}
["first","surprise array!","local","second","surprise number also!","local","third","surprise me too!","local"]
{$data.*.* | __.#1}
first
{$data.*.#1}
["first","second","third"]
{$data.* | __.#1}
{"one":"first","two":"surprise array!","locals":{"one":"local","two":"surprise local!","foo":"bar","bar":"hello","hash":{"one":"hashly","two":"bashly","three":"crashly"}}}
STILL MORE NOTES ON STARS
If you use a * or () pathfind operator, the result will ALWAYS be a list.
{$data.*.*.*.*.*}
[]
{$data.*.*.*.*.*.#1}
[]
{$data.foo.*}
[]
{$data.*.one.foo}
[]
{$data.*.one.#1.foo}
[]
<h3>Tree climbing</h3>
{* (:name "Awesome John" :age :alpha) | >$a-john ||}
{* (:name "Awesome Bobs" :age :beta) | >$a-bobs ||}
{* (:name "Awesome Mary" :age :gamma) | >$a-mary ||}
{* (:name "Awesome Stev" :age :delta) | >$a-stev ||}
{($a-john $a-bobs $a-mary $a-stev) | >$awesome_people ||}
{* (:name "Cool John" :age :alpha) | >$c-john ||}
{* (:name "Cool Bobs" :age :beta) | >$c-bobs ||}
{* (:name "Cool Mary" :age :gamma) | >$c-mary ||}
{* (:name "Cool Stev" :age :delta) | >$c-stev ||}
{($c-john $c-bobs $c-mary $c-stev) | >$cool_people ||}
{* (:name "Neat John" :age :alpha) | >$n-john ||}
{* (:name "Neat Bobs" :age :beta) | >$n-bobs ||}
{* (:name "Neat Mary" :age :gamma) | >$n-mary ||}
{* (:name "Neat Stev" :age :delta) | >$n-stev ||}
{($n-john $n-bobs $n-mary $n-stev) | >$neat_people ||}
{( {* (:name "awesome test co" :employees $awesome_people :boss $a-john)} {* (:name "cool test co" :employees $cool_people :boss $c-john)} {* (:name "neat test co" :employees $neat_people :boss $n-john)} ) | >$companies ||}
NOW QUERY IT
Find by position, then by property names:
{$companies.#-2.boss.name}
Cool John
All employees, grouped by employer
{$companies.*.employees}
[[{"name":"Awesome John","age":"alpha"},{"name":"Awesome Bobs","age":"beta"},{"name":"Awesome Mary","age":"gamma"},{"name":"Awesome Stev","age":"delta"}],[{"name":"Cool John","age":"alpha"},{"name":"Cool Bobs","age":"beta"},{"name":"Cool Mary","age":"gamma"},{"name":"Cool Stev","age":"delta"}],[{"name":"Neat John","age":"alpha"},{"name":"Neat Bobs","age":"beta"},{"name":"Neat Mary","age":"gamma"},{"name":"Neat Stev","age":"delta"}]]
A flat list of all employees
{$companies.*.employees.*}
[{"name":"Awesome John","age":"alpha"},{"name":"Awesome Bobs","age":"beta"},{"name":"Awesome Mary","age":"gamma"},{"name":"Awesome Stev","age":"delta"},{"name":"Cool John","age":"alpha"},{"name":"Cool Bobs","age":"beta"},{"name":"Cool Mary","age":"gamma"},{"name":"Cool Stev","age":"delta"},{"name":"Neat John","age":"alpha"},{"name":"Neat Bobs","age":"beta"},{"name":"Neat Mary","age":"gamma"},{"name":"Neat Stev","age":"delta"}]
All employee names
{$companies.*.employees.*.name}
["Awesome John","Awesome Bobs","Awesome Mary","Awesome Stev","Cool John","Cool Bobs","Cool Mary","Cool Stev","Neat John","Neat Bobs","Neat Mary","Neat Stev"]
All employee names of the second company
{$companies.#2.employees.*.name}
["Cool John","Cool Bobs","Cool Mary","Cool Stev"]
Second employee name of each company