-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore_.s
More file actions
1423 lines (1176 loc) · 32.5 KB
/
core_.s
File metadata and controls
1423 lines (1176 loc) · 32.5 KB
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
export say bad new_macro meta methods typeof subtype_of
rand_get rand_set rand_push rand_pop zip setters_ getters_ atan2
btland btjump bterror btrap `..`
_.new_fn_ = No
_.free =
//No acts as an identity element in arithmetics
no.`+` B = B
no.`-` B = B
no.`*` B = B
no.`++` = 1
no.`--` = -1
float.`++` = Me + 1.0
float.`--` = Me - 1.0
no.'<' B =
if B.is_int: ret B < 0
0
no.'>' B =
if B.is_int: ret B > 0
0
no.`<<` B =
if B.is_int: ret B << 0
0
no.`>>` B =
if B.is_int: ret B >> 0
0
_.bool = 1
int.bool = if Me: 1 else 0
float.bool = Me <> 0.0
text.bool =
@"0 for the empty text, 1 otherwise."
if Me.end: 0 else 1
no.bool = 0
text.`+` B = "[Me][B]"
_._ = [Me]
list._ = Me
_list_._ = Me
_empty_._ = Me
hard_list._ = Me
// `_.><`/`_.<>`/`_.<<`/`_.>`/`_.>>` all come from runtime/bltin.c
// as direct C-level raw-dyn comparators -- 1 builtin call (~50 ns)
// instead of the chained Symta-side `same Me B` / `not Me >< B`
// (~250 ns). Type-specific overrides (int.<, text.<, list.<, ...)
// still win via METHOD_FN dispatch before falling here.
_.is_int = 0
int.is_int = 1
_.is_float = 0
float.is_float = 1
_.is_fn = 0
fn.is_fn = 1
_.is_list = 0
list.is_list = 1
_.is_text = 0
text.is_text = 1
_.is_fixtext = 0
_fixtext_.is_fixtext = 1
//_.is_hard_list = 0
//list.is_hard_list = 1
_.is_table = 0
_.copy = Me
list.copy =
@"Shallow copy of a list -- elements are shared."
map X Me X
_.deep_copy = Me
list.deep_copy =
@"Recursive deep copy of a list. Nested lists are copied too."
map X Me X.deep_copy
methods Object =
@"Return the table of methods defined for the given value.
Example: methods 42 // table of int methods"
Object^methods_.t
// TS-2: type-system introspection. `typeof X` is the design's
// chosen name for "what type is this value"; today it aliases
// the existing `typename` builtin which returns the runtime
// sname -- already normalised so that the four concrete list
// tags (T_LIST | T_VIEW | T_CONS | T_BYTES) all return "list"
// and the two text tags (T_TEXT | T_FIXTEXT) return "text",
// matching the multi-tag union semantics the type system uses.
//
// `subtype_of X T` is a type-equality check at the `typename`
// granularity (i.e. T_LIST and T_CONS both subtype-of `list`).
// Inheritance walk along the runtime parent chain (so e.g.
// `subtype_of P object` where P is an int returns 1) is a
// TS-2.1 follow-up that needs a runtime primitive exposing
// types[].super.
//
// T may be a text ("int") or a tag-symbol (\int) -- both compare
// equal to typename's text result via the existing `><` overload.
typeof X =
@"Return the type name of X as text. Alias for `typename` --
the type-system surface uses `typeof` as the canonical verb."
typename X
subtype_of X T =
@"Return 1 if X's type is T or T is an ancestor of X's
type. T may be a text or tag-symbol naming a primitive
or a user-defined type. Walks the runtime super chain
via parents_of_ -- the list starts with X's own type-name
then climbs through each ancestor up to the root."
Ps parents_of_ X
got Ps.find(? >< T)
_.`()` A = A.Me
// RT-9: fn.`()` is now a C-side BUILTIN_VARARGS in runtime/bltin.c
// (b_fn_call) -- avoids the @As collection that previously made it
// the top emitter of size-1 LIST allocations.
_.`{}` F = $map(F)
int.sign =
@"-1 for negative, 0 for zero, 1 for positive."
if Me < 0 then -1
else if Me > 0 then 1
else 0
float.sign =
@"Same as int.sign but returns floats."
if Me < 0.0 then -1.0
else if Me > 0.0 then 1.0
else 0.0
list.sign =
@"Elementwise sign on a numeric list (each element -> -1, 0, or 1)."
map X Me X.sign
int.abs =
@"Absolute value of an integer. Note: `abs(-5)` does NOT work because
`(-5)` is parsed as a function call. Use `(0-5).abs` instead."
_abs Me
float.abs =
@"Absolute value of a float."
_abs Me
float.log2 = $log/2.0.log
list.metric =
R 0.0
map X Me: R += @float X*X
R
list.abs =
R 0.0
map X Me: R += @float X*X
R.sqrt
list.normalize = Me / $abs
list.orth = [$1 -$0].normalize //perpendicular vector to a given
list.loop I =
S $n
if I<0 then Me.(S - I%S) else Me.(I%S)
list.neg = dup I $n -$I
list.`+` Ys = dup I $n $I+Ys.I
list.`-` Ys = dup I $n $I-Ys.I
list.`*` Ys =
if Ys.is_list: dup I $n $I*Ys.I
else map X Me: X*Ys
list.`/` A = map X Me: X/A
list.`%` A = map X Me: X%A
list.float =
@"Convert every element of a numeric list to float."
map X Me: X.float
list.int =
@"Convert every element of a numeric list to int."
map X Me: X.int
list.round = Me{?round}
zip Xs Ys =
@"Element-wise pair two lists into a list of 2-element lists.
Example: zip \[1 2 3\] \[a b c\] // ((1 a) (2 b) (3 c))
The result is truncated to the length of the shorter input."
Xs Xs.l
Ys Ys.l
Sz min Xs.n Ys.n
dup I Sz: Xs.I,Ys.I
list.zip =
@"Transpose a list of equal-length lists.
Example: \[\[1 2 3\] \[a b c\]\].zip // ((1 a) (2 b) (3 c))"
dup I Me.0.n: map Xs Me Xs.I
text.`<` B =
less B.is_text: bad "cant compare string `[Me]` with [B]"
AS $n
BS B.n
if AS < BS
then | times I AS:
| AC $I.code
| BC B.I.code
| when AC <> BC: ret AC < BC
| 1
else | times I BS:
| AC $I.code
| BC B.I.code
| when AC <> BC: ret AC < BC
| 0
text.`*` N = @text: dup N: Me
text.is_upcase =
@"Test whether every character is an uppercase ASCII letter."
times I $n:
| C $I.code
| when C < 'A'.code or 'Z'.code < C: ret 0
1
text.is_downcase =
@"Test whether every character is a lowercase ASCII letter."
times I $n:
| C $I.code
| when C < 'a'.code or 'z'.code < C: ret 0
1
text.is_digit =
@"Test whether every character is an ASCII digit (0-9)."
times I $n:
| C $I.code
| when C < '0'.code or '9'.code < C: ret 0
1
text.is_alpha =
@"Test whether every character is an ASCII letter (a-z or A-Z)."
times I $n:
| C $I.code
| when C < 'a'.code or 'z'.code < C:
| when C < 'A'.code or 'Z'.code < C:
| ret 0
1
//shorthands to make text matching easier
text.is_d = $is_digit
text.is_a = $is_alpha
text.has F = bad "text.has is obsolete"
/*
text.has F =
| if F.is_fn then
| times I $n: when F($I): ret 1
else
| times I $n: when $I >< F: ret 1
| 0
*/
text.u =
@"Upcase a text. Letters A-Z and a-z only; non-ASCII passes through.
Example: \"Hello\".u // \"HELLO\""
Ys map Char $l
| C Char.code
| if C < 'a'.code or 'z'.code < C then Char
else (C - 'a'.code + 'A'.code).char
Ys.text
text.d =
@"Downcase a text. Letters A-Z and a-z only; non-ASCII passes through.
Example: \"Hello\".d // \"hello\""
Ys map Char $l
| C Char.code
| if C < 'A'.code or 'Z'.code < C then Char
else (C - 'A'.code + 'a'.code).char
Ys.text
text.title =
@"Capitalise the first character of a text.
Example: \"hello world\".title // \"Hello world\" (first char only!)"
less $n: ret Me
if $0.is_upcase then Me else "[$0.u][$tail]"
_.is_keyword = 0
text.is_keyword =
@"A text is a \"keyword\" (variable) if it starts with an uppercase letter.
Example: \"Foo\".is_keyword // 1
\"foo\".is_keyword // 0"
not: $n and $0.is_upcase
text.trim s/' ' i/0 l/1 r/1 =
| @"Strip leading and trailing instances of a separator (default space).
Keyword args: s/separator (default \" \"), l/1, r/1 select which sides.
Example: \" hi \".trim // \"hi\"
\"***hi***\".trim(s/\\*) // \"hi\""
| Xs $l
| when L:
| It case Xs [$S@Zs] Zs
| while It: Xs = It
| when R
| Xs Xs.f
| It case Xs [$S@Zs] Zs
| while It: Xs = It
| Xs = Xs.f
| Xs.text
text.begin T =
@"Test whether this text starts with the given prefix (or any of a list).
Example: \"hello world\".begin(\"hello\") // 1"
if T.is_text: ret $upto(T.n) >< T
T.any: X => $upto(X.n) >< X
int.map F = dup I Me: F(I)
int.keep F = $l.keep(F)
int.skip F = $l.skip(F)
list.i =
@"Pair each element with its index, producing a list of \[I value\] pairs.
Useful for indexed iteration via destructured map bodies."
dup I $n: [I Me^pop]
text.i = $l.i
list.`.` K =
times I K: Me = $tail
$head
list.del K =
@"Remove the element at index K. Returns a new list."
[@$take(K) @$drop(K+1)]
list.insert K V =
@"Insert value V at index K, shifting later elements right."
[@$take(K) V @$drop(K)]
list.change K V =
@"Replace the element at index K with value V."
[@$take(K) V @$drop(K+1)]
text.del K = [@$take(K) @$drop(K+1)].text
text.insert K V = [@$take(K) V @$drop(K)].text
text.change K V = [@$take(K) V @$drop(K+1)].text
`..` X N = dup N X
list.n =
@"Length of a list -- O(1) on regular lists."
S 0^int
till $end
| Me = $tail
| S+
S
list.end =
@"Test whether a list is empty. Returns 1 if empty, else 0.
Use this instead of .n equal-test -- O(1) on every list representation."
_eq $n 0
text.end = _eq $n 0
bytes.bytes = Me
list.bytes =
N $n
as Ys N.bytes: times I N: Ys.I = pop Me
list.utf8 = $bytes.utf8
list.head =
@"First element of a list. Returns No on an empty list.
See also: .tail, .end (test for empty)."
$0
list.tail =
@"All elements after the first. Returns the empty list when
the input has zero or one elements.
See also: .head, .lead (all but last)."
$l.tail
no.'*head' = No
list.'*head' = if $end: No else $head
text.'*head' = if $end: No else $head
list.`++` = if $end: No else $tail
list.`><` B =
less B.is_list: ret 0
till $end or B.end: less Me^pop >< B^pop: ret 0
$end and B.end
hard_list.`><` B =
less B.is_list: ret 0 //FIXME: cons_list B will be O(n^2) slow
N $n
when _ne N B.n: ret 0
times I N: less $I >< B.I: ret 0
1
_list_.`><` B =
less B.is_list: ret 0 //FIXME: cons_list B will be O(n^2) slow
when _ne (_tag B) 9: B = B.l //convert B to _list_
N $n
when _ne N B.n: ret 0
times I N: less (_lget Me I) >< (_lget B I): ret 0
1
list.`<` Xs =
A B 0
times I $n:
| A = $I
| B = Xs.I
| when A <> B: ret A < B
ret A < B
list.`>` Xs =
A B 0
times I $n:
| A = $I
| B = Xs.I
| when A <> B: ret A > B
ret A > B
list.`<<` Xs =
A B 0
times I $n:
| A = $I
| B = Xs.I
| when A <> B: ret A << B
ret A << B
list.`>>` Xs =
A B 0
times I $n:
| A = $I
| B = Xs.I
| when A <> B: ret A >> B
ret A >> B
list.f =
@"Reverse a list (flip). Returns a new list; the original is untouched."
N $n
Ys dup N
while N
| N-
| Ys.N = pop Me
Ys
hard_list.f =
N $n
dup N
| N-
| $N
text.f = $l.f.text
text.cnt C = $l.cnt^C
list.map F =
@"Apply a function to every element, collecting results.
Example: Xs.map(X => X * X) squares each element.
Use `&fn` to pass a named function: Xs.map(&sq).
The `Xs{Body}` operator is the same thing with a richer body grammar."
dup $n: F(Me^pop)
hard_list.map F = dup I $n: F($I)
_list_.map F = dup I $n: F(|_lget Me I)
text.map F = $l.map(F)
list.`^^` X = map Y Me Y^^X
list.fold Run F =
@"Left fold with an explicit initial value.
Example: Xs.fold 100 (Acc X => Acc + X) -- 100 plus sum of Xs.
Same shape as foldl in Haskell or reduce in JS. For sum use .z."
for X Me: Run = F(Run X)
Run
list.e F = till $end: F(Me^pop)
hard_list.e F = times I $n: F($I)
list.z =
@"Sum a list of numbers. Mixed int/float promotes to float.
Empty list returns 0."
S 0
till $end: S += pop Me
S
hard_list.z =
S 0
times I $n: S += $I
S
list.cnt F =
C 0
if F.is_fn
then till $end: when F(Me^pop): C+
else till $end: when F><Me^pop: C+
C
hard_list.cnt F =
C I 0
if F.is_fn
then times I $n: when F($I): C+
else times I $n: when F><$I: C+
C
list.keep F =
@"Keep elements for which the predicate returns truthy.
Example: Xs.keep(X => X > 0) keeps positives.
Use `&fn` to pass a named predicate: Xs.keep(&is_odd).
See also: .skip (inverse), the `:Cond` form for in-line filters."
Ys:
if F.is_fn
then for X Me: when F(X): Ys = [X@Ys]
else for X Me: when F >< X: Ys = [X@Ys]
Ys.f
list.skip F =
@"Drop elements for which the predicate returns truthy. Inverse of .keep."
Ys:
if F.is_fn
then for X Me: less F(X): Ys = [X@Ys]
else for X Me: less F >< X: Ys = [X@Ys]
Ys.f
list.j =
@"Concatenate a list of lists into a single flat list.
Sometimes spelled \"join\" or \"flatten\" elsewhere."
Rs dup $map(?n).z
I 0
for Ys Me: for Y Ys: Rs.(I+) = Y
Rs
_list_.l = Me
_bytes_.l = dup I $n $I
text.l = dup I $n $I
list.l =
N $n
Ys dup N
times I N: Ys.I = pop Me
Ys
int.l = dup I Me: I //iota operator
list.apply F =
@"Call a function with this list as its argument list.
Example: \[1 2 3\].apply(min) // min(1, 2, 3) -> 1"
$l.apply(F)
list.apply_method F = $l.apply_method(F)
list.text @As =
@"Join a list of text values into one text, optionally with a separator.
Example: Xs.text(\", \") joins with comma-space. Non-text elements are coerced via interpolation."
R $l
if As.n then R.text(As.0) else R.text
text.text = Me
list.x @As = Me{as_text}.text(@As)
list.split S =
F if S.is_fn then S else X => S >< X
Ys:
P $locate(F)
while got P:
| Ys = [$take(P)@Ys]
| Me = $drop(P+1)
| P = $locate(F)
[Me@Ys].f
text.split F =
@"Split a text on a delimiter. Returns a list of texts.
Empty pieces are kept."
$l.split(F).map(X=>X.text)
text.all F = $l.all(F)
text.any F = $l.any(F)
text.rows = $split '\n'
text.get = get_file_ Me
text.getText = get_text_file_ Me
text.lines = $getText.rows
text.set Value =
if Value.is_text then set_text_file_ Me Value else set_file_ Me Value.bytes
0
text.exists = file_exists_ Me
text.time = file_time_ Me
text.mkpath = mkpath_ Me
text.paths @As =
Path if '/' >< $~ then Me else "[Me]/"
Xs if As.n then $items(all) else $items
map X Xs "[Path][X]"
text.urls = Me.paths{url}
text.folders = Me.items{url}.keep(?(:_ '' '')){?0.lead}
text.url =
Name Ext ""
Xs $l.f
Sep Xs.locate(?><'/')
Dot Xs.locate(?><'.')
when got Dot and (no Sep or Dot < Sep):
| Ext = Xs.take(Dot).f.text
| Xs = Xs.drop(Dot+1)
| when got Sep: Sep -= Dot+1
Folder Name No
if got Sep
then | Folder = "[Xs.drop(Sep+1).f.text]/"
| Name = Xs.take(Sep).f.text
else | Folder = ''
| Name = Xs.f.text
[Folder Name Ext]
list.unurl =
[Folder Name Ext] Me
when Ext <> '': Ext = ".[Ext]"
"[Folder][Name][Ext]"
list.take N =
@"First N elements of a list. Returns the whole list if N >= length."
dup N: Me^pop
hard_list.take N = dup I N $I
list.drop N =
@"All elements after the first N. Returns empty if N >= length."
times I N Me^pop
Me
hard_list.drop S = dup $n-S: Me.|S+
text.drop S = $l.drop(S).text
text.take S = $l.take(S).text
text.~ = $($n-1)
text.head = $0
text.tail = $drop(1)
text.lead = $take($n-1)
text.inc Val = Me.l("[Me]0":
@T '-' D<+d? = if T.end: "[T.text][-D.text.int+Val]"
else "[T.text]-[D.text.int+Val]"
@T D<+d? = "[T.text][D.text.int+Val]")
text.`++` = $inc 1
text.`--` = $inc -1
list.~ = $($n-1)
list.suf X = [@Me X]
list.lead = $take($n-1)
// take up to N elements
list.upto N = $take(|max 0: min $n N)
text.upto N = $take(|max 0: min $n N)
text.`.!` I = if I < $n and I >> 0: Me.I
// drop up to N elements
list.wout N = $drop(|max 0: min $n N)
text.wout N = $drop(|max 0: min $n N)
list.cut P S = $drop(P).take(S)
text.cut P S = $drop(P).take(S)
list.slice B E S =
Xs $l
N Xs.n
if B < 0:
B = N+B
if B < 0: B = 0
else if B > N: B = N
if E < 0:
E = N+E
if E < 0: E = 0
else if E > N: E = N
if B < E:
N (E-B+S-1)/S
B -= S
dup N: Xs.|B += S
else
N (B-E+S-1)/S
E += S*N
dup N: Xs.|E -= S
text.slice B E S = $l.slice(B E S).text
int.bes E S = //implementation of [B:E:S]
B Me
if B<E:
N (E-B+S)/S
B -= S
dup N: B += S
else
N (B-E+S)/S
E += S*N
dup N: E -= S
text.bes E S = [Me.code:E.code:S]{char}
text.keep Item = $l.keep(Item).text
text.skip Item = $l.skip(Item).text
list.takeIf F =
R:
while not $end and F(Me.0): push Me^pop R
R
list.dropIf F =
while not $end and F(Me.0): Me^pop
Me
text.dropIf F = $l.dropIf(F).text
text.takeIf F = $l.takeIf(F).text
_.rmap F = F(Me)
list.rmap F = map X Me X.rmap(F)
list.infix Item =
@"Insert a value between every two adjacent elements.
Example: \[\\a \\b \\c\].infix(\\sep) // (a sep b sep c)"
N $n*2-1
if N < 0 then [] else dup I N: if I%2 then Item else Me^pop
list.locate F =
@"Return the INDEX of the first matching element, or No."
less F.is_fn: F = (X => F >< X)
for (I 0; not $end; I+): when F(Me^pop): ret I
hard_list.locate F =
if F.is_fn then times I $n: when F($I): ret I
else times I $n: when F >< $I: ret I
text.locate F =
if F.is_fn then times I $n: when F($I): ret I
else times I $n: when F >< $I: ret I
list.find F =
@"Return the first element for which the predicate is truthy, or No."
if F.is_fn
then for (I 0; not $end; I+):
| It Me^pop; when F(It): ret It
else for (I 0; not $end; I+):
| It Me^pop
| when F><It: ret It
hard_list.find F =
if F.is_fn
then | times I $n:
| It $I
| when F(It): ret It
else | times I $n:
| It $I
| when F><It: ret It
_list_.find F =
if F.is_fn
then | times I Me^_size:
| It _lget Me I
| when F(It): ret It
else | times I Me^_size:
| It _lget Me I
| when F><It: ret It
list.group N =
@"Group a list into chunks of N elements.
Example: \[1 2 3 4 5 6\].group(2) // ((1 2) (3 4) (5 6))
\[1 2 3 4 5\].group(2) // ((1 2) (3 4) (5)) -- trailing partial"
Y Ys []
I 0
till $end
| push Me^pop Y
| I+
| when I >< N
| push Y.f Ys
| Y = []
| I = 0
when Y.n: push Y.f Ys
Ys.f
text.group N = $l.group(N){?text}
list.all F =
if F.is_fn then for X Me: less F(X): ret 0
else for X Me: less F >< X: ret 0
1
list.any F =
if F.is_fn then for X Me: when F(X): ret 1
else for X Me: when F >< X: ret 1
0
list.max =
@"Largest element of a list, by < comparison. Empty list returns No."
when $end: ret No
M $head
for X Me: when X > M: M = X
M
list.min =
@"Smallest element of a list, by < comparison. Empty list returns No."
when $end: ret No
M $head
for X Me: when X < M: M = X
M
list.maxBy F =
when $end: ret No
M $head
FM F(M)
for X Me:
| FX F(X)
| when FX > FM:
| M = X
| FM = FX
M
list.minBy F =
when $end: ret No
M $head
FM F(M)
for X Me:
| FX F(X)
| when FX < FM:
| M = X
| FM = FX
M
//list.has Item = got $find(Item)
list.has Item = bad "list.has is obsolete"
HexChars '0123456789ABCDEF'
int.x =
less Me: ret '0'
Cs:
S ''
when Me < 0
| S = '-'
| Me = -Me
while Me > 0
| Cs = [HexChars.(Me%16) @Cs]
| Me /= 16
[S@Cs].text
_.as_text = "#([Me^typename] [Me^gid_.x])"
no.as_text = 'No'
int.as_text =
less Me: ret '0'
Cs:
S ''
when Me < 0
| S = '-'
| Me = -Me
while Me > 0
| Cs = [HexChars.(Me%10) @Cs]
| Me /= 10
[S@Cs].text
plain_char C =
N C.code
if ('a'.code << N and N << 'z'.code)
or ('A'.code << N and N << 'Z'.code)
or ('0'.code << N and N << '9'.code)
or '_'.code >< N
then 1
else 0
text.as_text =
less $n: ret '``'
case Me 'if'+'then'+'else'+'or'+'and': ret "`[Me]`"
Cs:
Q $0.is_digit
for C Me
| less plain_char C: Q = 1
| when '`' >< C: C = '\\`'
| when '\\' >< C: C = '\\\\'
| push C Cs
if Q then ['`' @['`' @Cs].f].text else Me
list.as_text = "([(map X Me X.as_text).text(' ')])"
_.textify_ = $as_text
text.textify_ = Me
say @As =
@"Print one or more values, separated by spaces, followed by a newline.
Lists print as parenthesised forms; tables print as @-key-val groups.
Double-quoted text with bracketed expressions is interpolated.
See also: say_ (no newline), bad (raise an error)."
say_ "[As{"[?]"}.text(' ')]\n"
bad @Text =
@"Raise a runtime error with the joined-text argument list as the message.
Caught by `btrap` on the call path; otherwise terminates with the error and source location.
Example: less B: bad 'division by zero'
See also: btrap (catch), bterror (test for error value)."
rterr_ "[Text.text ' ']"
tbl.__ Method Args =
if _gt Args.n 1
then Args.0.(Method^_method_name.tail) = Args.1 // strip `assign indicator`
else Me.(Method^_method_name)
tbl.map F =
@"Apply a function to each key-value pair, collecting the results."
$l.map(F)
tbl.as_text =
@"Render the table as its @-curly key!value literal text form."
"@{[$l{"[?0.as_text]![?1.as_text]"}.text(' ')]}"
tbl.copy =
@"Shallow copy of a table. Values shared; keys are."
$l.t
tbl.deep_copy =
@"Recursive deep copy of a table."
$l.t
tbl.s @As =
@"Sort a tables key-value pairs the same way list.s does for a list of pairs."
$l.s @As
list.t =
@"Convert a list of pair-lists to a table. Inverse of `tbl.l`."
T!
for [K V] Me: T.K = V
T
list.bag = Me{?,1}.t
list.uniq =
@"Remove consecutive duplicates, preserving first occurrence.
Use .s first if you want to dedupe over the whole list regardless of order."
Seen!
$skip(X => got Seen.X or (Seen.X = 1) and 0)
text.pad Count Item =
X "[Item]"
when X.n > 1: bad "pad item: [X]"
N +Count - $n
when N < 0: bad "text is larger than [Count.abs]: '[Me]'"
Pad @text: dup N X
if Count < 0: "[Pad][Me]" else "[Me][Pad]"
list.pad Count Item =
N +Count - $n
when N < 0: bad "list is larger than [Count.abs]: '[Me]'"
Pad dup N Item
if Count < 0: [@Pad @Me] else [@Me @Pad]
int.digits @Base =
B if Base.end: 10 else Base.head
Ys:
while Me > 0:
push Me%B Ys
Me /= B
Ys.l
list.digits @Base =
B if Base.end: 10 else Base.head
R 0
for X Me:
R *= B
R += X
R
type macro @new_macro N E: name!N expander!E
// `meta A B` -- attach a meta value B (typically a source-position
// triple) to A. For heap-allocated A we stash (A -> B) in the
// runtime's weak hashtable via `meta_attach_` and return A
// unchanged; consumers still read it back via A.meta_ (which goes
// through `meta_lookup_`). This means AST nodes coming out of the
// parser are plain lists -- no wrapper struct intercepting method
// dispatch -- which is what unblocks `text.parse` from switching
// to the consolidated C reader (`parse_strip_c_`).
//
// For immediates (int, fixtext, No, T_TAG, ...) we fall back to
// the legacy `meta_wrapper` type: immediates have no GC identity
// to key on in the weak table. The parser doesn't attach meta to
// immediates today, but other consumers (e.g. ui_old's widget
// rects) might.
//
//~ means meta_wrapper doesn't inherit from _, so all methods route
// through __ -- preserving the "forwarding wrapper" semantics for
// the immediate case.
// `meta` -- wrapper type that pairs an object with a meta value