-
Notifications
You must be signed in to change notification settings - Fork 1
/
seq.hoon
executable file
·1918 lines (1918 loc) · 55.9 KB
/
seq.hoon
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
:: /lib/seq/hoon
:: |seq
:: operations for working with values of type list
::
|%
:: +all-pairs: [(list T1) (list T2)] -> (list [T1 T2])
::
:: Returns a new list that contains all pairings of elements from two non-empty
:: lists.
:: Examples
:: > (all-pairs ~['a' 'b' 'c'] ~[1 2 3])
:: ~[['c' 3] ['c' 2] ['c' 1]
:: ['b' 3] ['b' 2] ['b' 1]
:: ['a' 3] ['a' 2] ['a' 1]]
:: Source
++ all-pairs
|* [p=(list) q=(list)]
=/ a=(list _?>(?=(^ p) i.p)) p
=/ res=(list [_?>(?=(^ p) i.p) _?>(?=(^ q) i.q)]) ~
=/ scnd=(list _?>(?=(^ q) i.q)) q
|- ^- (list [_?>(?=(^ p) i.p) _?>(?=(^ q) i.q)])
?~ a res
?~ scnd $(a t.a, scnd q)
$(res [[i.a i.scnd] res], scnd +.scnd)
:: +append: [(list) (list)] -> (list)
::
:: Returns a new list that contains the elements of the first list followed by
:: elements of the second list.
:: Examples
:: > (append "urb" "it")
:: "urbit"
:: > (append (limo [1 2 ~]) (limo [3 4 ~]))
:: ~[1 2 3 4]
:: Source
++ append weld
:: +average: (list @) -> @ud
::
:: Returns the average of the values in a non-empty list.
:: Examples
:: > (average `(list @ud)`~[1 2 3 4])
:: 2
:: Source
++ average
|* a=(list @)
(div (roll a add) (lent a))
:: +average-by: [(list T) projection:$-(T @ud)] -> @ud
::
:: Returns the average of values in a list generated by applying a function to
:: each element of the list.
:: Examples
:: > (average-by (limo ~[[1 1] [2 2] [3 3] [4 4]]) |=([a=@ b=@] (add a b)))
:: 5
:: Source
++ average-by
|* [a=(list) b=$-(* @ud)]
(div (roll (turn a b) add) (lent a))
:: +choose: [(list T1) $-(T1 (unit T2))] -> (list T2)
::
:: Applies a function to each element in a list and then returns a list of
:: values v where the applied function returns (unit v).
:: Returns an empty list when the input list is empty or when the applied
:: chooser function returns ~ for all elements.
:: Examples
:: > =a |=(a=@ ?.((gte a 2) ~ (some (add a 10))))
:: > (choose `(list @)`[0 1 2 3 ~] a)
:: [i=12 t=[i=13 t=~]]
:: Source
++ choose murn
:: +chunk-by-size: [(list T) chunkSize:@] -> (list (list T))
::
:: Divides the input list into lists (chunks) with a positive number of at
:: most chunkSize elements. Returns a new list containing the generated lists
:: (chunks) as its elements. Returns an empty list when the input list is empty
:: Examples
:: > (chunk-by-size (limo ~[1 2 3 4 5 6 7]) 2)
:: [i=~[1 2] t=[i=~[3 4] t=~[~[5 6] ~[7]]]]
:: Crash
:: 'chunk size is 0'
:: Source
++ chunk-by-size
|* [p=(list) q=@ud]
?: =(0 q) ~|('chunk size is 0' !!)
=/ res=(list (list _?>(?=(^ p) i.p))) ~
=/ i=@ud 0
=/ next=(list _?>(?=(^ p) i.p)) ~
|- ^- (list (list _?>(?=(^ p) i.p)))
?~ p (flop [(flop next) res])
?: =(i q) $(i 0, res [(flop next) res], next ~)
$(i +(i), next [i.p next], p t.p)
:: +collect: [(list T1) mapping:$-(T1 (list T2))] -> (list T2)
::
:: For each element of the list, applies the given function. Concatenates all
:: the results and return the combined list.
:: Examples
:: > (collect (limo ~[1 2 3]) |=(a=* (limo ~[a a])))
:: ~[1 1 2 2 3 3]
:: Source
++ collect
|* [p=(list) q=$-(* (list))]
=/ res=(list) ~
|-
?~ p (flop res)
=/ c=(list) (q i.p)
|-
?~ c ^$(p t.p)
$(c t.c, res [i.c res])
:: +compare: [(list T) (list T) comparer:$-([T T] ?)] -> (list ?)
::
:: Compares two lists element by element using the given comparison function.
:: When an entry exists for both lists, returns the result of the comparer
:: function. When the second list is longer than the first returns %.y for
:: entries of second where first does not exist. When first list is longer
:: returns %.n for entries of first where second does not exist.
:: Examples
:: > (compare "when" "than" aor)
:: ~[%.n %.y %.n %.y]
:: Source
++ compare
|* [b=(list) c=(list) a=$-([* *] ?)]
=/ res=(list ?) ~
=/ bb b
=/ cc c
|- ^- (list ?)
?~ bb ?~ cc (flop res) $(cc t.cc, res [%.y res])
?~ cc $(bb t.bb, res [%.n res])
$(bb t.bb, cc t.cc, res [(a i.bb i.cc) res])
:: +concat: (list (list)) -> (list)
::
:: Returns a new list that contains the elements of each of the lists in order.
:: Examples
:: > (concat (limo [(limo ['a' 'b' 'c' ~]) (limo ['e' 'f' 'g' ~])
:: (limo ['h' 'i' 'j' ~]) ~]))
:: ~['a' 'b' 'c' 'e' 'f' 'g' 'h' 'i' 'j']
:: > (concat (limo [(limo [1 'a' 2 'b' ~]) (limo [3 'c' 4 'd' ~]) ~]))
:: ~[1 97 2 98 3 99 4 100]
:: Source
++ concat zing
:: +contains: [(list) value:*] -> ?
::
:: Tests if the list contains the specified element.
:: Examples
:: > (contains "yep" `(list tape)`~["nope" "yep"])
:: %.y
:: Source
++ contains
|* [p=(list) q=*]
|- ^- ?
?~ p %.n
?: =(q i.p) %.y
$(p t.p)
:: +count-by: [(list T1) projection:$-(T1 T2)] -> (list [T2 @ud])
::
:: Applies a key-generating function to each element of a list and returns a
:: list yielding unique keys and their number of occurrences in the original
:: list.
:: Examples
:: > %: count-by
:: (limo ~["where" "when" "there" "then"])
:: |=(a=tape (scag 2 a))
:: ==
:: ~[[[i=t' t="h"] 2] [[i='w' t="h"] 2]]
:: Source
++ count-by
|* [p=(list) q=$-(* *)]
=/ res=(map _?>(?=(^ p) (q i.p)) @ud) ~
|- ^- (list [_?>(?=(^ p) (q i.p)) @ud])
?~ p ~(tap by res)
=/ key=_?>(?=(^ p) (q i.p)) (q i.p)
=/ val (~(get by res) key)
?~ val $(res `(map _?>(?=(^ p) (q i.p)) @ud)`(~(put by res) key 1), p t.p)
%= $
res `(map _?>(?=(^ p) (q i.p)) @ud)`(~(put by res) key +(`@ud`(need val)))
p t.p
==
:: +distinct: (list) -> (list)
::
:: Returns a list that contains no duplicate entries according to generic hash
:: and equality comparisons on the entries. If an element occurs multiple times
:: in the list then the later occurrences are discarded.
:: Examples
:: > (distinct `(list tape)`~["tape1" "tape0" "tape1" "tape0"])
:: ~["tape1" "tape0"]
:: Source
++ distinct
|* a=(list)
=/ b=(list _?>(?=(^ a) i.a)) ~
=/ c=(set) ~
|- ^+ a
?~ a (flop b)
?: (~(has in c) i.a) $(a t.a)
$(a t.a, b [i.a b], c (~(put in c) i.a))
:: +distinct-by: [(list T1) projection:$-(T1 T2)] -> (list T1)
::
:: Returns a list that contains no duplicate entries according to the generic
:: hash and equality comparisons on the keys returned by the given
:: key-generating function. If an element occurs multiple times in the list
:: then the later occurrences are discarded.
:: Examples
:: > =foo `(list [@ @])`~[[1 1] [1 2] [1 3] [2 1]]
:: > (distinct-by foo |=([a=@ b=@] (add a b)))
:: ~[[1 1] [1 2] [1 3]]
:: Source
++ distinct-by
|* [p=(list) q=$-(* *)]
=/ b=(list _?>(?=(^ p) i.p)) ~
=/ c=(set) ~
|- ^+ p
?~ p (flop b)
?: (~(has in c) (q i.p)) $(p t.p)
$(p t.p, b [i.p b], c (~(put in c) (q i.p)))
:: +empty: $ -> *(list $)
::
:: Returns an empty list typed by the mold. (The bunt of the typed list.)
:: Examples
:: > (empty @t)
:: ~
:: Source
++ empty
|* a=mold
*(list a)
:: +exactly-one: (list T) -> T
::
:: Returns the only element of the list. Crashes if list has different length.
:: Examples
:: > (exactly-one (limo ~["tape"]))
:: "tape"
:: Crash
:: 'list length is not 1'
:: Source
++ exactly-one
|* a=(list)
?: =(1 (lent a)) -.a
~|('list length is not 1' !!)
:: +except: [(list T) items-to-exclude:(list T)] -> (list T)
::
:: Returns a new list with the distinct elements of the input list which do not
:: appear in the items-to-exclude list, using generic hash and equality
:: comparisons to compare values.
:: Examples
:: > %: except
:: (limo ~["able" "baker" "charlie" "dog"])
:: (limo ~["baker" "dog"])
:: ==
:: ~[[i='a' t="ble"] [i='c' t="harlie"]]
:: Source
++ except
|* [p=(list) q=(list)]
=/ qq (silt q)
=/ b=(list _?>(?=(^ p) i.p)) ~
|- ^+ p
?~ p (flop b)
?: (~(has in qq) i.p) $(p t.p)
$(p t.p, b [i.p b], qq (~(put in qq) i.p))
:: +exists: [(list T) predicate:$-(T ?)] -> ?
::
:: Tests if any element of the list satisfies the given predicate.
:: Examples
:: > =foo |= a=tape
:: ?: ?&(=(4 (lent a)) =('a' -.a)) %.y %.n
:: > (exists (limo ~["aaa" "able" "baker" "charlie" "dog"]) foo)
:: %.y
:: Source
++ exists
|* [a=(list) b=$-(* ?)]
|- ^- ?
?~ a %.n
?: (b i.a) %.y
$(a t.a)
:: +exists2: [(list T1) (list T2) predicate:$-([T1 T2] ?)] -> ?
::
:: Tests if any pair of corresponding elements of the lists satisfies the given
:: predicate.
:: Examples
:: > =foo |= [a=tape b=tape]
:: ?: =(-.a -.b) %.y %.n
:: > (exists2 (limo ~["cat" "betty"]) (limo ~["able" "butter"]) foo)
:: %.y
:: Source
++ exists2
|* [a=(list) b=(list) c=$-([* *] ?)]
|- ^- ?
?~ a %.n
?~ b %.n
?: (c [i.a i.b]) %.y
$(a t.a, b t.b)
:: +filter: [(list T) predicate:$-(T ?)] -> (list T)
::
:: Returns a new list containing only the elements of the list for
:: which the given predicate returns "true"
:: Examples
:: > =a |=(a=@ (gth a 1))
:: > (filter `(list @)`[0 1 2 3 ~] a)
:: [i=2 t=~[3]]
:: Source
++ filter skim
::
:: +findx: [(list T) predicate:$-(T ?)] -> T
::
:: Returns the first element for which the given function returns True.
:: Crashes if no such element exists.
:: Examples
:: > (findx (gulf [1 30]) |=(a=@ud ?&(=(0 (mod a 3)) =(0 (mod a 5)))))
:: 15
:: Crash
:: 'not found'
:: Source
++ findx
|* [a=(list) b=$-(* ?)]
|- ^- _?>(?=(^ a) i.a)
?~ a ~|('not found' !!)
?: (b i.a) i.a $(a t.a)
:: +find-all: [(list T) predicate:$-(T ?)] -> (list T))
::
:: Returns all elements for which the given function returns True.
:: Crashes if no such element exists.
:: Examples
:: > (find-all (gulf [1 30]) |=(a=@ud ?&(=(0 (mod a 3)) =(0 (mod a 5)))))
:: ~[15 30]
:: Crash
:: 'not found'
:: Source
++ find-all
|* [a=(list) b=$-(* ?)]
=/ c=(list _?>(?=(^ a) i.a)) ~
|-
?~ a ?~ c ~|('not found' !!) (flop c)
?: (b i.a) $(a t.a, c [i.a c]) $(a t.a)
:: +find-all-by-list: [(list T) arg:(list T) -> (list @)]
::
:: Produces list of indices of all occurrences of the argument list sequence in
:: the sequence of the source list.
:: Examples
:: > (find-all-by-list "cbabab" "ba")
:: ~[1 3]
:: Source
++ find-all-by-list
|* [hstk=(list) nedl=(list)]
^- (list @)
(fand nedl hstk)
:: +find-all-by-unit: [(list T1) chooser:$-(T1 (unit T2))] -> (list T2)
::
:: Applies the given function to successive elements, returning the list of
:: elements where the function returns Some(x).
:: Crashes when no such element exists.
:: Examples
:: > %: find-all-by-unit
:: (limo ~[1 2 3 4])
:: |=(a=@ ?:(=((mod a 2) 0) `a ~))
:: ==
:: ~[2 4]
:: Crash
:: 'not found'
:: Source
++ find-all-by-unit
|* [hstk=(list) nedl=$-(* (unit *))]
=/ b=(list _?>(?=(^ hstk) i.hstk)) ~
|- ^- (list _?>(?=(^ hstk) i.hstk))
?~ hstk ?~ b ~|('not found' !!)
(flop b)
=/ x (nedl i.hstk)
?~ x $(hstk t.hstk)
$(hstk t.hstk, b [(need x) b])
:: +find-back: [(list T) predicate:$-(T ?)] -> T
::
:: Returns the last element for which the given function returns True.
:: Crashes if no such element exists.
:: Examples
:: > %: find-back
:: (gulf [1 30])
:: |=(a=@ud ?&(=(0 (mod a 3)) =(0 (mod a 5))))
:: ==
:: 30
:: Crash
:: 'not found'
:: Source
++ find-back
|* [a=(list) b=$-(* ?)]
?~ a ~|('not found' !!)
(findx (flop a) b)
:: +find-back-by-list: [(list T) arg:(list T)] -> @
::
:: Produces the index of the last occurrence of the argument list sequence in
:: the sequence of the source list.
:: Examples
:: > (find-back-by-list "cbabab" "ba")
:: 3
:: Source
++ find-back-by-list
|* [hstk=(list) nedl=(list)]
(tail-end (fand nedl hstk))
:: +find-back-by-unit: [(list T1) chooser:$-(T1 (unit T2))] -> T2
::
:: Applies the given function to successive elements from the end back,
:: returning the first result where function returns Some(x) for some x.
:: Crashes when no such element exists.
:: Examples
:: > %: find-back-by-unit
:: (limo ~[1 2 3 4])
:: |=(a=@ ?:(=((mod a 2) 0) `a ~))
:: ==
:: 4
:: Crash
:: 'not found'
:: Source
++ find-back-by-unit
|* [hstk=(list) nedl=$-(* (unit *))]
^- _?>(?=(^ hstk) i.hstk)
(find-by-unit (flop hstk) nedl)
:: +find-by-list: [(list T) arg:(list T)] -> @
::
:: Produces the index of the first occurrence of the argument list sequence in
:: the sequence of the source list.
:: Examples
:: > (find-by-list "cbabab" "ab")
:: 2
:: Crash
:: 'not found'
:: Source
++ find-by-list
|* [hstk=(list) nedl=(list)]
^- @
=/ x (find nedl hstk)
?~ x ~|('not found' !!) (need x)
:: +find-by-unit: [(list T1) chooser:$-(T1 (unit T2))] -> T2
::
:: Applies the given function to successive elements, returning the first
:: result where function returns Some(x) for some x. Crashes when no such
:: element exists.
:: Examples
:: > (find-by-unit (limo ~[1 2 3 4]) |=(a=@ ?:(=((mod a 2) 0) `a ~)))
:: 2
:: Crash
:: 'not found'
:: Source
++ find-by-unit
|* [hstk=(list) nedl=$-(* (unit *))]
|- ^- _?>(?=(^ hstk) i.hstk)
?~ hstk ~|('not found' !!)
=/ x (nedl i.hstk)
?~ x $(hstk t.hstk)
(need x)
:: +find-index: [(list T) predicate:$-(T ?)] -> @
::
:: Returns the index of the first element in the list that satisfies the given
:: predicate. Crashes if no such element exists.
:: Examples
:: > %: find-index
:: (gulf [1 30])
:: |=(a=@ud ?&(=(0 (mod a 3)) =(0 (mod a 5))))
:: ==
:: 14
:: Crash
:: 'not found'
:: Source
++ find-index
|* [a=(list) b=$-(* ?)]
=/ i 0
|- ^- @
?~ a ~|('not found' !!)
?: (b i.a) i $(a t.a, i +(i))
:: +find-index-all: [(list T) predicate:$-(T ?)] -> (list @)
::
:: Returns the list of indices in the list that satisfies the given predicate.
:: Examples
:: > %: find-index-all
:: (gulf [1 30])
:: |=(a=@ud ?&(=(0 (mod a 3)) =(0 (mod a 5))))
:: ==
:: ~[14 29]
:: Source
++ find-index-all
|* [a=(list) b=$-(* ?)]
=/ c=(list @) ~
=/ i 0
|- ^- (list @)
?~ a (flop c)
?: (b i.a) $(a t.a, c [i c], i +(i)) $(a t.a, i +(i))
:: +find-index-back: [(list T) predicate:$-(T ?)] -> @
::
:: Returns the index of the last element in the list that satisfies the given
:: predicate. Crashes if no such element exists.
:: Examples
:: > %: find-index-back
:: (gulf [1 30])
:: |=(a=@ud ?&(=(0 (mod a 3)) =(0 (mod a 5))))
:: ==
:: 29
:: Crash
:: 'not found'
:: Source
++ find-index-back
|* [a=(list) c=$-(* ?)]
?~ a ~|('not found' !!)
=/ b (flop a)
=/ i (dec (lent a))
|- ^- @ud
?~ b ~|('not found' !!)
?: (c i.b) i
?: =(0 i) ~|('not found' !!)
$(b t.b, i (dec i))
:: +first-n: [(list T) count:@ud] -> (list T)
::
:: Returns the first N elements of the list.
:: Examples
:: > (first-n `(list @)`[1 2 3 4 ~] 2)
:: [i=1 t=~[2]]
:: > (first-n `(list @)`[1 2 3 4 ~] 10)
:: [i=1 t=~[2 3 4]]
:: Source
++ first-n
|* [p=(list) q=@]
(scag q p)
:: +fold: [(list T1) state:T2 folder:$-([T1 T2] T2)] -> T2
::
:: Applies a function to each element of the list, threading an
:: accumulator argument through the computation. Take the second argument, and
:: apply the function to it and the first element of the list. Then feed this
:: result into the function along with the second element and so on. Return the
:: final result. If the input function is f and the elements are i0...iN then
:: computes f (... (f s i0) i1 ...) iN.
:: Examples
:: > (fold (gulf 1 5) 0 |=([n=@ state=@] (add state (mul n n))))
:: 55
:: Source
++ fold
|* [a=(list) b=* c=_|=(^ [** +<+])]
|- ^- _b
?~ a b
$(a t.a, b (c i.a b))
:: +fold2: [(list T1) (list T2) state:T3 folder:$-([T1 T2 T3] T3)] -> T3
::
:: Applies a function to corresponding elements of two lists, threading
:: an accumulator argument through the computation. The lists must have
:: identical sizes. If the input function is f and the elements are i0...iN and
:: j0...jN then computes f (... (f s i0 j0)...) iN jN.
:: Examples
:: > %: fold2
:: (limo ~["Tails" "Head" "Tails"])
:: (limo ~["Tails" "Head" "Head"])
:: 0
:: |=([n1=tape n2=tape state=@] ?:(=(n1 n2) +(state) state))
:: ==
:: 2
:: Crash
:: 'lists of unequal length'
:: Source
++ fold2
|* [a=(list) b=(list) c=* d=_|=(^ [** ** +<+])]
|- ^- _c
?~ a ?~ b c ~|('lists of unequal length' !!)
?~ b ~|('lists of unequal length' !!)
$(a t.a, b t.b, c (d i.a i.b c))
:: +fold-back: [(list T1) state:T2 folder:$-([T1 T2] T2)] -> T2
::
:: Applies a function to each element of the list, starting from the end,
:: threading an accumulator argument through the computation. If the input
:: function is f and the elements are i0...iN then computes f i0 (...(f iN s)).
:: Examples
:: > =less-hungry |= [n=tape state=(list [tape @])]
:: ^+ state
:: ?~ state (limo ~[[n 1]])
:: [[n +(+.i.state)] state]
:: > %: fold-back
:: (limo ~["Apple" "Pear" "Orange"])
:: `(list [tape @])`~
:: less-hungry
:: ==
:: [i=["Apple" 3] t=~[["Pear" 2] ["Orange" 1]]]
:: Source
++ fold-back
|* [a=(list) b=* c=$-([* *] *)]
(fold (flop a) b c)
:: +fold-back2: [(list T1) (list T2) state:T3 folder:$-([T1 T2 T3] T3)] -> T3
::
:: Applies a function to corresponding elements of two lists, threading
:: an accumulator argument through the computation. The lists must have
:: identical sizes. If the input function is f and the elements are i0...iN and
:: j0...jN then computes f i0 j0 (...(f iN jN s)).
:: Examples
:: > %: fold-back2
:: (limo ~["Tails" "Head" "Tails"])
:: (limo ~["Tails" "Head" "Head"])
:: `(list tape)`~
:: |=([n1=tape n2=tape state=(list tape)] [(weld n1 n2) state])
:: ==
:: ~["TailsTails" "HeadHead" "TailsHead"]
:: Source
++ fold-back2
|* [a=(list) b=(list) c=* d=$-([* * *] *)]
(fold2 (flop a) (flop b) c d)
:: +forall: [(list T) predicate:$-(T ?)] -> ?
::
:: Tests if all elements of the list satisfy the given predicate.
:: Examples
:: > (forall (limo ~[2 4 8]) |=(a=@ ?:(=(0 (mod a 2)) %.y %.n)))
:: %.y
:: > (forall (limo ~[2 1 8]) |=(a=@ ?:(=(0 (mod a 2)) %.y %.n)))
:: %.n
:: Source
++ forall
|* [a=(list) predicate=$-(* ?)]
|- ^- ?
?~ a %.y
?. (predicate i.a) %.n
$(a t.a)
:: +forall2: [(list T1) (list T2) predicate:$-([T1 T2] ?)] -> ?
::
:: Tests if all corresponding elements of the list satisfy the given
:: predicate pairwise. Crashes on lists of unequal length
:: Examples
:: > %: forall2
:: (limo ~[1 4 8])
:: (limo ~[3 4 8])
:: |=([a=@ b=@] ?:(=(0 (mod (add a b) 2)) %.y %.n))
:: ==
:: %.y
:: > %: forall2
:: (limo ~[1 5 8])
:: (limo ~[3 4 8])
:: |=([a=@ b=@] ?:(=(0 (mod (add a b) 2)) %.y %.n))
:: ==
:: %.n
:: Crash
:: 'lists of unequal length'
:: Source
++ forall2
|* [a=(list) b=(list) predicate=$-(* ?)]
?. =((lent a) (lent b)) ~|('lists of unequal length' !!)
|- ^- ?
?~ a %.y
?~ b ~|('cant get here' !!)
?. (predicate i.a i.b) %.n
$(a t.a, b t.b)
:: +from-map: (map) -> (list [* *])
::
:: Produces the list of all key element pairs in map.
:: Examples
:: > =map-00 (to-map (limo ~[['aa' 1] ['bb' 2] ['cc' 3] ['dd' 4]]))
:: > (from-map map-00)
:: ~[[p='bb' q=2] [p='dd' q=4] [p='cc' q=3] [p='aa' q=1]]
:: Source
++ from-map
|* a=(map)
~(tap by a)
:: +from-set: -> (list)
::
:: Produces the list of element in set
:: Examples
:: > (from-set `(set tap)`[[n="tlon"] l=["urbit" ~ ~] r=["uqbar" ~ ~]])
:: Source
++ from-set
|* a=(set)
~(tap in a)
:: +get-head: (list T) -> T
::
:: Returns the first element of the list. Crashes on empty list.
:: Examples
:: > (get-head ~[1 2])
:: 1
:: Crash
:: 'empty list'
:: Source
++ get-head
|* a=(list)
?~ a ~|('empty list' !!)
i.a
:: +get-tail: (list T) -> (list T)
::
:: Returns the list after removing the first element.
:: Examples
:: > (get-tail ~[1 2])
:: ~[2]
:: Crash
:: 'empty list'
:: Source
++ get-tail
|* a=(list)
?~ a ~|('empty list' !!)
t.a
:: +group-by: [(list T1) projection:$-(T1 T2)] -> (list [T2 (list T1)])
::
:: Applies a key-generating function to each element of a list and yields a
:: list of unique keys. Each unique key contains a list of all elements that
:: match to this key.
:: Examples
:: > (group-by (gulf 1 5) |=(a=@ (mod a 2)))
:: ~[[p=0 q=[i=4 t=~[[2 0]]]] [p=1 q=[i=5 t=~[[3 [1 0] 0]]]]]
:: Source
++ group-by
|* [a=(list) b=$-(* *)]
=/ d=(map * (list *)) ~
|-
?~ a ~(tap by d)
=/ e (b i.a)
=/ f (~(get by d) e)
?~ f $(a t.a, d (~(put by d) e ~[i.a]))
$(a t.a, d (~(put by d) e ~[i.a (need f)]))
:: +indexed: (list T) -> (list [@ T])
::
:: Returns a new list whose elements are the corresponding elements of the
:: input list paired with the index (from 0) of each element.
:: Examples
:: > (indexed (gulf 1 3))
:: ~[[0 1] [1 2] [2 3]]
:: Source
++ indexed
|* a=(list)
=/ b=(list [@ _?>(?=(^ a) i.a)]) ~
=/ i 0
|- ^- (list [@ _?>(?=(^ a) i.a)])
?~ a (flop b)
$(a t.a, b [[i i.a] b], i +(i))
:: +init: [length:@ud initializer:$-(@ud T)] -> (list T)
::
:: Creates a list by calling the given generator on each index.
:: Crashes on length = 0.
:: Examples
:: > (init 3 |=(a=@ (add a 5)))
:: ~[6 7 8]
:: Crash
:: 'empty list'
:: Source
++ init
|* [len=@ ini=$-(@ *)]
=/ a=(list *) ~
?: =(0 len) ~|('empty list' !!)
|-
?: =(0 len) a
$(len (dec len), a [(ini len) a])
:: +insert-at: [(list T) index:@ value:T] -> (list T)
::
:: Return a new list with a new item inserted before the given index.
:: Examples
:: > (insert-at (limo ~[2 3 4]) 1 11)
:: ~[2 11 3 4]
:: Source
++ insert-at into
:: +insertManyAt: [(list T) values=(list T) index=@] -> (list T)
::
:: Return a new list with new items inserted before the given index.
:: Examples
:: > (insert-many-at (limo ~[1 2 5 6 7]) (limo ~[3 4]) 2)
:: ~[1 2 3 4 5 6 7]
:: Crash
:: 'out of range'
:: Source
++ insert-many-at
|* [a=(list) values=(list) i=@]
?: =(0 (lent values)) a
=/ len-a (lent a)
?: (gth i len-a) ~|('out of range' !!)
?~ values a
?: =(0 i) (weld values a)
?: =(len-a i) (weld a values)
=/ start (scag i a)
=/ end (slag i a)
(weld (weld start values) end)
:: +is-empty: (list) -> ?
::
:: Returns true if the list contains no elements, false otherwise.
:: Examples
:: > (is-empty `(list @)`~[2])
:: %.n
:: > (is-empty `(list @)`~)
:: %.y
:: Source
++ is-empty
|* a=(list)
?~ a %.y %.n
:: +item: [(list T) index:@] -> T
::
:: Indexes into the list. The first element has index 0.
:: Examples
:: > (item `(list @)`~["aa" "bb" "cc" "dd"] 2)
:: "cc"
:: Source
++ item
|* [a=(list) i=@]
(snag i a)
:: +iter: [(list T) fun:$-(T *)] -> ~
::
:: Applies the given function to each element of the list, dropping the
:: results.
:: Examples
:: > (iter (limo ~["tape1" "tape2"]) |=(a=tape (lent a)))
:: ~
:: Source
++ iter
|* [a=(list) fun=$-(* *)]
=/ b (turn a fun)
~
:: +iter2: [(list T1) (list T2) fun:$-(T1 T2 *)] -> ~
::
:: Applies the given function to two lists simultaneously, dropping the
:: results. The lists must have identical size.
:: Examples
:: > %: iter2
:: (limo ~["tape1" "tape2"])
:: (limo ~["tape3" "tape4"])
:: |=([a=tape b=tape] (add (lent a) (lent b)))
:: ==
:: ~
:: Crash
:: 'lists of unequal length'
:: Source
++ iter2
|* [a=(list) b=(list) fun=$-(* *)]
=/ b (map2 a b fun)
~
:: +iteri: [(list T) fun:$-([@ T] *)] -> ~
::
:: Applies the given function to each element of the list and the index
:: of element, dropping the results.
:: Examples
:: > (iteri (limo ~[1 2 3]) |=([a=@ b=@] (add a b)))
:: ~
:: Source
++ iteri
|* [a=(list) fun=$-(* *)]
=/ b (mapi a fun)
~
:: +iteri2: [(list T1) (list T2) fun:$-(@ T1 T2 *)] -> ~
::
:: Applies the given function to two lists and the current index,
:: dropping the results. The lists must have identical size.
:: Examples
:: > %: iteri2
:: (limo ~[1 2 3])
:: (limo ~[4 5 6])
:: |=([a=@ b=@ c=@] (add (add a b) c))
:: ==
:: ~
:: Crash
:: 'lists of unequal length'
:: Source
++ iteri2
|* [a=(list) b=(list) fun=$-(* *)]
=/ b (mapi2 a b fun)
~
:: +last-n: [(list T) count:@] -> (list T)
::
:: Returns the last N elements of the list.
:: Examples
:: > (last-n `(list @)`[1 2 3 4 ~] 2)
:: [i=3 t=~[4]]
:: > (last-n `(list @)`[1 2 3 4 ~] 10)
:: [i=1 t=~[2 3 4]]
:: Source
++ last-n
|* [p=(list) q=@]
(flop (scag q (flop p)))
:: +length: (list T) -> @u
::
:: Returns the length of the list.
:: Examples
:: > (length [1 2 3 4 ~])
:: 4
:: > (length [1 'a' 2 'b' (some 10) ~])
:: 5
:: Source
++ length lent
:: +map-seq: [(list T1) mapping:$-(T1 T2)] -> (list T2)
::
:: Builds a new list whose elements are the results of applying the given
:: gate to each of the elements of the list.
:: Examples
:: > (map-seq (limo [104 111 111 110 ~]) @t)
:: <|h o o n|>
:: > =a |=(a=@ (add a 4))
:: > (map-seq (limo [1 2 3 4 ~]) a)
:: ~[5 6 7 8]
:: Source
++ map-seq turn
:: +map2: [(list T1) (list T2) mapping:$-([T1 T2] T3)] -> (list T3)
::
:: Builds a new list whose elements are the results of applying the given
:: function to the corresponding elements of the two lists pairwise.
:: Crashes if lists are of unequal length
:: Examples
:: > (map2 (limo ~[1 2 3 4]) (limo ~[5 6 7 8]) |=(a=[@ @] (add -.a +.a)))
:: ~[6 8 10 12]
:: Crash
:: 'lists of unequal length'
:: Source
++ map2
|* [a=(list) b=(list) c=gate]
=/ d=(list) ~
|-
?~ a ?~ b (flop d) ~|('lists of unequal length' !!)
?~ b ~|('lists of unequal length' !!)
$(a t.a, b t.b, d [(c i.a i.b) d])
:: +map3: [(list T1) (list T2) (list T3) $-([T1 T2 T3] T4)] -> (list T4)
::
:: Builds a new list whose elements are the results of applying the given
:: function to the corresponding elements of the three lists
:: simultaneously.
:: Examples
:: > %: map3
:: (limo ~[1 2 3 4])
:: (limo ~[5 6 7 8])
:: (limo ~[9 10 11 12])
:: |=(a=[@ @ @] (add (add -.a +<.a) +>.a))
:: ==
:: ~[15 18 21 24]
:: Crash
:: 'lists of unequal length'
:: Source
++ map3
|* [a=(list) b=(list) c=(list) d=gate]
=/ e=(list) ~
|-
?~ a ?~ b ?~ c (flop e)
~|('lists of unequal length' !!)
~|('lists of unequal length' !!)
?~ b ~|('lists of unequal length' !!)
?~ c ~|('lists of unequal length' !!)
$(a t.a, b t.b, c t.c, e [(d i.a i.b i.c) e])
:: +map-fold: [(list T1) state:T2 $-([T1 T2] [T3 T2])] -> [(list T3) T2]
::
:: Combines map and fold. Builds a new list whose elements are the results of
:: applying the given function to each of the elements of the input list. The
:: function is also used to accumulate a final value.
:: Examples
:: > =input `(list [@t @])`~[['in' 1] ['out' 2] ['in' 3]]
:: > =foo |* [p=[@t @] state=@]
:: ^- [[@t @] @]
:: ?: =(-.p 'in') [['in' (mul +.p 2)] (add state +.p)]
:: [['out' (mul +.p 2)] (mul state +.p)]
:: > (map-fold input 0 foo)
:: [~[['in' 2] ['out' 4] ['in' 6]] 5]
:: Source
++ map-fold spin
:: +map-fold-back: [(list T1) state:T2 $-([T1 T2] [T3 T2])] -> [(list T3) T2]
::
:: Combines map and foldBack. Builds a new list whose elements are the results
:: of applying the given function to each of the elements of the input list,
:: starting from the end.
:: The function is also used to accumulate a final value.
:: Examples
:: > =input `(list [@t @])`~[['in' 1] ['out' 2] ['in' 3]]
:: > =foo |* [p=[@t @] state=@]
:: ^- [[@t @] @]
:: ?: =(-.p 'in') [['in' (mul +.p 2)] (add state +.p)]
:: [['out' (mul +.p 2)] (mul state +.p)]
:: > (map-fold-back input 0 foo)
:: [~[['in' 6] ['out' 4] ['in' 2]] 7]
:: Source
++ map-fold-back
|* [a=(list) state=* c=$-([* *] [* *])]
(spin (flop a) state c)
:: +mapi: [(list T1) mapping:$-([@ T1] T2)] -> (list T2)
::
:: Builds a new list whose elements are the results of applying the given
:: function to each of the elements of the list. The integer index passed
:: to the function indicates the index (from 0) of element being transformed.
:: Examples
:: > (mapi (limo ~[1 2 3]) |=([a=@ b=@] (add a b)))
:: ~[1 3 5]
:: Source
++ mapi
|* [a=(list) fun=$-([@ *] *)]
=/ i 0
=/ b=(list) ~
|-
?~ a (flop b)
$(a t.a, b [(fun i i.a) b], i +(i))
:: +mapi2: [(list T1) (list T2) mapping:$-([@ T1 T2] T3)] -> (list T3)
::
:: Like mapi, but mapping corresponding elements from two lists of equal length.
:: Examples
:: > %: mapi2
:: (limo ~[1 2 3])
:: (limo ~[4 5 6])
:: |=([a=@ b=@ c=@] (add (add a b) c))
:: ==
:: ~[5 8 11]
:: Crash
:: 'lists of unequal length'
:: Source
++ mapi2
|* [a=(list) b=(list) fun=$-([@ * *] *)]
=/ i 0
=/ c=(list) ~
|-
?~ a ?~ b (flop c) ~|('lists of unequal length' !!)
?~ b ~|('lists of unequal length' !!)
$(a t.a, b t.b, c [(fun i i.a i.b) c], i +(i))
:: +maxi: (list T) -> T
::
:: Return the greatest of all elements of the list, compared via Operators.max.
:: Examples
:: > (maxi (limo ~[10 12 11]))
:: 12
:: > (maxi (limo ~["max" "add" "busy"]))
:: Crash
:: 'empty list'
:: Source
++ maxi
|* a=(list)
~| 'empty list'