on 6d45ede help sign in
1168 lines 18 kB history raw
1---
2navhome: /docs
3---
6# 2b: list logic
7Reverse
9### `++flop`
11Produces the list `a` in reverse order.
13Accepts
14-------
16`a` is a list.
18Produces
19--------
21A list.
23Source
24------
26 ++ flop :: reverse
27 ~/ %flop
28 |* a/(list)
29 => .(a (homo a))
30 ^+ a
31 =+ b=`_a`~
32 |-
33 ?~ a b
34 $(a t.a, b [i.a b])
38Examples
39--------
41 > =a (limo [1 2 3 ~])
42 > (flop a)
43 ~[3 2 1]
47***
48### `++homo`
50Homogenize
52Produces a `++list` whose type is a fork of all the contained types in the
53list `a`. Used when you want to make all the types of the elements of a list the same.
55Accepts
56-------
58`a` is a list.
60Produces
61--------
63a list.
65Source
66------
68 ++ homo :: homogenize
69 |* a/(list)
70 ^+ =< $
71 |% +- $ ?:(*? ~ [i=(snag 0 a) t=$])
72 --
73 a
76Examples
77--------
79 > lyst
80 [i=1 t=[i=97 t=[i=2 t=[i=98 t=[i=[~ u=10] t=~]]]]]
81 > (homo lyst)
82 ~[1 97 2 98 [~ u=10]]
83 > =a (limo [1 2 3 ~])
84 > a
85 [i=1 t=[i=2 t=[i=3 t=~]]]
86 > (homo a)
87 ~[1 2 3]
91***
92### `++lent`
94List length
96Produces the length of any `++list` `a` as an atom.
98Accepts
99-------
101`a` is a list.
103Produces
104--------
106an atom.
108Source
109------
111 ++ lent :: length
112 ~/ %lent
113 |= a/(list)
114 ^- @
115 =+ b=0
116 |-
117 ?~ a b
118 $(a t.a, b +(b))
121Examples
122--------
124 > (lent (limo [1 2 3 4 ~]))
125 4
126 > (lent (limo [1 'a' 2 'b' (some 10) ~]))
127 5
131***
132### `++find`
134First index in list
136Produces the index of the first occurrence of `++list` `nedl` in
137`++list` `hstk` as the `++unit` of an atom.
139Accepts
140-------
142`nedl` is a list.
144`hstk` is a list.
146Produces
147--------
149The `++unit` of an atom.
151Source
152------
154 ++ find :: first index
155 ~/ %find
156 |= {nedl/(list) hstk/(list)}
157 =| i/@ud
158 |- ^- (unit @ud)
159 =+ [n=nedl h=hstk]
160 |-
161 ?: |(?=($~ n) ?=($~ h))
162 ~
163 ?: =(i.n i.h)
164 ?~ t.n
165 `i
166 $(n t.n, h t.h)
167 ^$(i +(i), hstk +.hstk)
170Examples
171--------
173 > (find [3]~ ~[1 2 3])
174 [~ 2]
175 > (find [4]~ ~[1 2 3])
176 ~
177 > (find ['a']~ "cbabab")
178 [~ 2]
179 > (find "ab" "cbabab")
180 [~ 2]
184***
185### `++fand`
187All indices in list
189Produces the indices of all occurrences of `++list` `nedl` in
190`++list` `hstk` as a `++list` of atoms.
192Accepts
193-------
195`nedl` is a list.
197`hstk` is a list.
199Produces
200--------
202A `++list` of atoms.
204Source
205------
207 ++ fand :: all indices
208 ~/ %fand
209 |= {nedl/(list) hstk/(list)}
210 =| i/@ud
211 =| fnd/(list @ud)
212 |- ^+ fnd
213 =+ [n=nedl h=hstk]
214 |-
215 ?: |(?=($~ n) ?=($~ h))
216 (flop fnd)
217 ?: =(i.n i.h)
218 ?~ t.n
219 ^$(i +(i), hstk +.hstk, fnd [i fnd])
220 $(n t.n, h t.h)
221 ^$(i +(i), hstk +.hstk)
224Examples
225--------
227 > (fand [3]~ ~[1 2 3])
228 ~[2]
229 > (fand [4]~ ~[1 2 3])
230 ~
231 > (fand ['a']~ "cbabab")
232 ~[2 4]
233 > (fand "ab" "cbabab")
234 ~[2 4]
238***
239### `++levy`
241Logical "and" on list
243Computes the Boolean logical "and" on the results of gate `b` applied to each individual element in `++list` `a`.
245Accepts
246-------
248`a` is a list.
250`b` is a gate.
252Produces
253--------
255A boolean.
257Source
258------
260 ++ levy
261 ~/ %levy :: all of
262 |* {a/(list) b/$-(* ?)}
263 |- ^- ?
264 ?~ a &
265 ?. (b i.a) |
266 $(a t.a)
269Examples
270--------
272 > =a |=(a/@ (lte a 1))
273 > (levy (limo [0 1 2 1 ~]) a)
274 %.n
275 > =a |=(a/@ (lte a 3))
276 > (levy (limo [0 1 2 1 ~]) a)
277 %.y
281***
282### `++lien`
284Logical "or" on list
286Computes the Boolean logical "or" on the results of applying [gate]() `b` to every element of [`++list`]() `a`.
288Accepts
289-------
291`a` is a list.
293`b` is a gate.
295Source
296------
298 ++ lien :: some of
299 ~/ %lien
300 |* {a/(list) b/$-(* ?)}
301 |- ^- ?
302 ?~ a |
303 ?: (b i.a) &
304 $(a t.a)
306Examples
307--------
309 > =a |=(a=@ (gte a 1))
310 > (lien (limo [0 1 2 1 ~]) a)
311 %.y
312 > =a |=(a=@ (gte a 3))
313 > (lien (limo [0 1 2 1 ~]) a)
314 %.n
318***
319### `++limo`
321List Constructor
323Turns a null-terminated tuple into a [`++list`]().
325Accepts
326-------
328`a` is a null-terminated tuple.
330Produces
331--------
333A `++list`.
335Source
336------
338 ++ limo :: listify
339 |* a=*
340 ^+ =< $
341 |% +- $ ?~(a ~ ?:(_? i=-.a t=$ $(a +.a)))
342 --
343 a
345Examples
346--------
348 > (limo [1 2 3 ~])
349 [i=1 t=[i=2 t=[i=3 t=~]]]
353***
354### `++murn`
356Maybe transform
358Passes each member of `++list` `a` to gate `b`, which must produce a
359`++unit`. Produces a new list with all the results that do not produce
360`~`.
362Accepts
363-------
365`a` is a list.
367`b` is a gate that produces a unit.
369Produces
370--------
372A unit.
374Source
375------
377 ++ murn :: maybe transform
378 ~/ %murn
379 |* {a/(list) b/$-(* (unit))}
380 |-
381 ?~ a ~
382 =+ c=(b i.a)
383 ?~ c
384 $(a t.a)
385 [i=u.c t=$(a t.a)]
387Examples
388--------
390 > =a |=(a=@ ?.((gte a 2) ~ (some (add a 10))))
391 > (murn (limo [0 1 2 3 ~]) a)
392 [i=12 t=[i=13 t=~]]
396***
397### `++oust`
399Remove
401Removes elements from list `c` beginning at inclusive index `a`, removing `b` number of elements.
403Accepts
404-------
406`c` is a list.
408Produces
409--------
411A `++list`.
413Source
414------
416 ++ oust :: remove
417 ~/ %oust
418 |* {{a/@ b/@} c/(list)}
419 (weld (scag a c) (slag (add a b) c))
421Examples
422--------
424 > (oust [4 5] "good day, urbit!")
425 "good urbit!"
426 > (oust [2 2] (limo [1 2 3 4 ~]))
427 [i=1 t=[i=2 t=~]]
431***
432### `++reap`
434Replicate
436Replicate: produces a `++list` containing `a` copies of `b`.
438Accepts
439-------
441`b` is a noun.
443Produces
444--------
446A list.
448Source
449------
451 ++ reap :: replicate
452 ~/ %reap
453 |* {a/@ b/*}
454 |- ^- (list _b)
455 ?~ a ~
456 [b $(a (dec a))]
458Examples
459--------
461 > (reap 20 %a)
462 ~[%a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a]
463 > (reap 5 ~s1)
464 ~[~s1 ~s1 ~s1 ~s1 ~s1]
465 > `@dr`(roll (reap 5 ~s1) add)
466 ~s5
470***
471### `++reel`
473Right fold
475Right fold: moves right to left across a `++list` `a`, recursively slamming
476a binary gate `b` with an element from `a` and an accumulator, producing
477the final value of the accumulator.
479Accepts
480-------
482`b` is a binary gate.
484Produces
485--------
487The accumulator, which is a noun.
489Source
490------
492 ++ reel :: right fold
493 ~/ %reel
494 |* {a/(list) b/_|=({* *} +<+)}
495 |- ^+ +<+.b
496 ?~ a
497 +<+.b
498 (b i.a $(a t.a))
501Examples
502--------
504 > =sum =|([p=@ q=@] |.((add p q)))
505 > (reel (limo [1 2 3 4 5 ~]) sum)
506 15
507 > =a =|([p=@ q=@] |.((sub p q)))
508 > (reel (limo [6 3 1 ~]) a)
509 4
510 > (reel (limo [3 6 1 ~]) a)
511 ! subtract-underflow
512 ! exit
516***
517### `++roll`
519Left fold
521Left fold: moves left to right across a list `a`, recursively slamming a
522binary [gate]() `b` with an element from the [`++list`]() and an accumulator,
523producing the final value of the accumulator.
525Accepts
526-------
528`b` is a binary [gate]().
530Produces
531--------
533The accumulator, which is a [noun]().
535Source
536------
538 ++ roll :: left fold
539 ~/ %roll
540 |* [a=(list) b=_|=([* *] +<+)]
541 |- ^+ +<+.b
542 ?~ a
543 +<+.b
544 $(a t.a, b b(+<+ (b i.a +<+.b)))
546Examples
547--------
549 > =sum =|([p=@ q=@] |.((add p q)))
550 > (roll (limo [1 2 3 4 5 ~]) sum)
551 q=15
552 > =a =|([p=@ q=@] |.((sub p q)))
553 > (roll (limo [6 3 1 ~]) a)
554 ! subtract-underflow
555 ! exit
556 > (roll (limo [1 3 6 ~]) a)
557 q=4
561***
562### `++scag`
564Prefix
566Accepts an atom `a` and [list]() `b`, producing the first `a` elements of
567the front of the list.
569Accepts
570-------
572`b` is a list.
574Produces
575--------
577A list of the same type as `b`.
579Source
580------
582 ++ scag :: prefix
583 ~/ %scag
584 |* [a=@ b=(list)]
585 |- ^+ b
586 ?: |(?=(~ b) =(0 a)) ~
587 [i.b $(b t.b, a (dec a))]
589Examples
590--------
592 > (scag 2 (limo [0 1 2 3 ~]))
593 [i=0 t=[i=1 t=~]]
594 > (scag 10 (limo [1 2 3 4 ~]))
595 [i=1 t=[i=2 t=[i=3 t=[i=4 t=~]]]]
599***
600### `++skid`
602Separate
604Separates a `++list` `a` into two lists - Those elements of `a` who produce
605true when slammed to gate `b` and those who produce `%.n`.
607Accepts
608-------
610`b` is a gate that accepts one argument and produces a loobean.
612Produces
613--------
615A cell of two lists.
617Source
618------
620 ++ skid :: separate
621 ~/ %skid
622 |* {a/(list) b/$-(* ?)}
623 |- ^+ [p=a q=a]
624 ?~ a [~ ~]
625 =+ c=$(a t.a)
626 ?:((b i.a) [[i.a p.c] q.c] [p.c [i.a q.c]])
630Examples
631--------
633 > =a |=(a/@ (gth a 1))
634 > (skid (limo [0 1 2 3 ~]) a)
635 [p=[i=2 t=[i=3 t=~]] q=[i=0 t=[i=1 t=~]]]
639***
640### `++skim`
642Suffix
644Cycles through the members of a list `a`, passing them to a gate `b` and
645producing a list of all of the members that produce `%.y`. Inverse of
646`++skip`.
648Accepts
649-------
651`b` is a gate that accepts one argument and produces a boolean.
653Produces
654--------
656A boolean
658Source
659------
661 ++ skim :: only
662 ~/ %skim
663 |* {a/(list) b/$-(* ?)}
664 |-
665 ^+ a
666 ?~ a ~
667 ?:((b i.a) [i.a $(a t.a)] $(a t.a))
670Examples
671--------
673 > =a |=(a/@ (gth a 1))
674 > (skim (limo [0 1 2 3 ~]) a)
675 [i=2 t=[i=3 t=~]]
679***
680### `++skip`
682Except
684Cycles through the members of `++list` `a`, passing them to a gate `b`.
685Produces a list of all of the members that produce `%.n`. Inverse of
686`++skim`.
688Accepts
689-------
691`b` is a gate that accepts one argument and produces a loobean.
693Produces
694--------
696A list of the same type as `a`.
698Source
699------
701 ++ skip :: except
702 ~/ %skip
703 |* {a/(list) b/$-(* ?)}
704 |-
705 ^+ a
706 ?~ a ~
707 ?:((b i.a) $(a t.a) [i.a $(a t.a)])
710Examples
711--------
713 > =a |=(a/@ (gth a 1))
714 > (skip (limo [0 1 2 3 ~]) a)
715 [i=0 t=[i=1 t=~]]
719***
720### `++slag`
722Suffix
724Accepts an atom `a` and list `b`, producing the remaining elements from
725`b` starting at `a`.
727Accepts
728-------
730`b` is a list.
732Produces
733--------
735A list of the same type as `b`.
737Source
738------
740 ++ slag :: suffix
741 ~/ %slag
742 |* {a/@ b/(list)}
743 |- ^+ b
744 ?: =(0 a) b
745 ?~ b ~
746 $(b t.b, a (dec a))
749Examples
750--------
752 > (slag 2 (limo [1 2 3 4 ~]))
753 [i=3 t=[i=4 t=~]]
754 > (slag 1 (limo [1 2 3 4 ~]))
755 [i=2 t=[i=3 t=[i=4 t=~]]]
759***
760### `++snag`
762Index
764Accepts an atom `a` and a `++list` `b`, producing the element at the index
765of `a`and failing if the list is null. Lists are 0-indexed.
767Accepts
768-------
770`b` is a list.
772Produces
773--------
775Produces an element of `b`, or crashes if no element exists at that index.
777Source
778------
780 ++ snag :: index
781 ~/ %snag
782 |* {a/@ b/(list)}
783 |-
784 ?~ b
785 ~|('snag-fail' !!)
786 ?: =(0 a) i.b
787 $(b t.b, a (dec a))
790Examples
791--------
793 > (snag 2 "asdf")
794 ~~d
795 > (snag 0 `(list @ud)`~[1 2 3 4])
796 1
800***
801### `++sort`
803Quicksort
805Quicksort: accepts a `++list` `a` and a gate `b` which accepts two nouns and
806produces a loobean. `++sort` then produces a list of the elements of `a`,
807sorted according to `b`.
809Accepts
810-------
812`b` is a gate that accepts two nouns and produces a boolean.
814Produces
815--------
817A list
819Source
820------
822 ++ sort !. :: quicksort
823 ~/ %sort
824 |* {a/(list) b/$-([* *] ?)}
825 => .(a ^.(homo a))
826 |- ^+ a
827 ?~ a ~
828 %+ weld
829 $(a (skim t.a |=(c/_i.a (b c i.a))))
830 ^+ t.a
831 [i.a $(a (skim t.a |=(c/_i.a !(b c i.a))))]
834Examples
835--------
837 > =a =|({p=@ q=@} |.((gth p q)))
838 > (sort (limo [0 1 2 3 ~]) a)
839 ~[3 2 1 0]
843***
844### `++swag`
846Infix
848Similar to `substr` in Javascript: extracts a string infix, beginning at
849inclusive index `a`, producing `b` number of characters.
851Accepts
852-------
854`c` is a list.
856Produces
857--------
859A list of the same type as `c`.
861Source
862------
864 ++ swag :: infix
865 |* {{a/@ b/@} c/(list)}
866 (scag +<-> (slag +<-< c))
869Examples
870--------
872 > (swag [2 5] "roly poly")
873 "ly po"
874 > (swag [2 2] (limo [1 2 3 4 ~]))
875 [i=3 t=[i=4 t=~]]
879***
880### `++turn`
882Gate to list
884Accepts a `++list` `a` and a gate `b`. Produces a list with the gate applied
885to each element of the original list.
887Accepts
888-------
890`b` is a gate.
892Produces
893--------
895A list.
897Source
898------
900 ++ turn :: transform
901 ~/ %turn
902 |* {a/(list) b/$-(* *)}
903 |-
904 ?~ a ~
905 [i=(b i.a) t=$(a t.a)]
908Examples
909--------
911 > (turn (limo [104 111 111 110 ~]) @t)
912 <|h o o n|>
913 > =a |=(a/@ (add a 4))
914 > (turn (limo [1 2 3 4 ~]) a)
915 ~[5 6 7 8]
919***
920### `++spin`
922Gate to list, with state
924Accepts a `++list` `a`, a gate `b`, and some state `c`. Produces a list with
925the gate applied to each element of the original list. `b` is called with
926a tuple -- the head is an element of `a` and the tail is the state `c`, and
927should produce a tuple of the transformed element and the (potentially
928modified) state `c`.
930Accepts
931-------
933`a` is a `++list`.
935`b` is a gate.
937`c` is a noun.
939Produces
940--------
942A list.
944Source
945------
947 ++ spin
948 |* {a/(list) b/_|=({* *} [** +<+]) c/*}
949 :: ?< ?=($-([_?<(?=($~ a) i.a) _c] [* _c]) b)
950 |-
951 ?~ a
952 ~
953 =+ v=(b i.a c)
954 [i=-.v t=$(a t.a, c +.v)]
957Examples
958--------
960A trivial example -- does nothing with the state:
962 > %^ spin (limo ~[4 5 6])
963 |=({n/@ a/@} [n a])
964 0
965 ~[4 5 6]
967Create a list of pairs, `p` as the index and `q` as the list element:
969 > %^ spin (limo ~[4 5 6])
970 |=({n/@ a/@} [`(pair)`[a n] +(a)])
971 0
972 ~[[p=0 q=4] [p=1 q=5] [p=2 q=6]]
974Create 10 random numbers less than `10`:
976 > %^ spin (reap 10 0)
977 |=({n/@ rng/_og} (rads:rng 10))
978 ~(. og eny)
979 ~[5 5 9 4 1 7 9 9 9 6]
981*Notes*:
983`(~(rads og eny) 2)` creates a random number less than `2`, seeding the RNG
984with entropy (`eny`). The head of the product is the random number, the tail
985is the continuation of the RNG.
989***
990### `++spun`
992Gate to list, with state
994Accepts a `++list` `a` and a gate `b`. `c` is internal state, initially
995derived by *bunting* the tail of the sample of gate `b`, instead of
996being passed in explicitly as in `++spin`. Produces a list with the
997gate applied to each element of the original list. `b` is called with a tuple
998-- the head is an element of `a` and the tail is the state `c`, and should
999produce a tuple of the transformed element and the (potentially modified)
1000state `c`.
1002Accepts
1003-------
1005`a` is a `++list`.
1007`b` is a gate.
1009Produces
1010--------
1012A list.
1014Source
1015------
1017 ++ spun
1018 |* {a/(list) b/_|=({* *} [** +<+])}
1019 =| c/_+<+.b
1020 |-
1021 ?~ a
1023 =+ v=(b i.a c)
1024 [i=-.v t=$(a t.a, c +.v)]
1027Examples
1028--------
1030Create a list of pairs, `p` as the index and `q` as the list element:
1032 > %+ spun (limo ~[4 5 6])
1033 |=({n/@ a/@} [`(pair)`[a n] +(a)])
1034 ~[[p=0 q=4] [p=1 q=5] [p=2 q=6]]
1036Join two lists into a list of pairs, `p` from `l` and `q` from the sample:
1038 > =l (limo ~[7 8 9])
1039 > %+ spun (limo ~[4 5 6])
1040 |=({n/@ a/@} [`(pair)`[(snag a l) n] +(a)])
1041 ~[[p=7 q=4] [p=8 q=5] [p=9 q=6]]
1045***
1046### `++weld`
1048Concatenate
1050Concatenate two `++list`s `a` and `b`.
1052Accepts
1053-------
1055`a` and `b` are lists.
1057Source
1058------
1060 ++ weld :: concatenate
1061 ~/ %weld
1062 |* {a/(list) b/(list)}
1063 => .(a ^.(homo a), b ^.(homo b))
1064 |- ^+ b
1065 ?~ a b
1066 [i.a $(a t.a)]
1069Examples
1070--------
1072 > (weld "urb" "it")
1073 ~[~~u ~~r ~~b ~~i ~~t]
1074 > (weld (limo [1 2 ~]) (limo [3 4 ~]))
1075 ~[1 2 3 4]
1079***
1080### `++welp`
1082Perfect weld
1084Concatenate two `++list`s `a` and `b` without losing their type information
1085to homogenization.
1087Accepts
1088-------
1090`a` is a list.
1092`b` is a list.
1094Produces
1095--------
1097A list.
1099Source
1100------
1102 ++ welp :: perfect weld
1103 =| {* *}
1104 |%
1105 +- $
1106 ?~ +<-
1107 +<-(. +<+)
1108 +<-(+ $(+<- +<->))
1109 --
1112Examples
1113--------
1115 > (welp "foo" "bar")
1116 "foobar"
1117 ~zod/arvo=/hoon/hoon> (welp ~[60 61 62] ~[%a %b %c])
1118 [60 61 62 %a %b %c ~]
1119 ~zod/arvo=/hoon/hoon>? (welp ~[60 61 62] ~[%a %b %c])
1120 [60 61 62 %a %b %c ~]
1121 [@ud @ud @ud %a %b %c %~]
1122 ~zod/arvo=/hoon/hoon> (welp [sa+1 so+2 ~] si/3)
1123 [[%sa 1] [%so 2] %si 3]
1127***
1128### `++zing`
1130Cons
1132Turns a `++list` of lists into a single list by promoting the elements of
1133each sublist into the higher.
1135Accepts
1136-------
1138A list of lists.
1140Produces
1141--------
1143A list.
1145Source
1146------
1148 ++ zing :: promote
1149 =| *
1150 |%
1151 +- $
1152 ?~ +<
1153 +<
1154 (welp +<- $(+< +<+))
1155 --
1159Examples
1160--------
1162 > (zing (limo [(limo ['a' 'b' 'c' ~]) (limo ['e' 'f' 'g' ~]) (limo ['h' 'i' 'j' ~]) ~]))
1163 ~['a' 'b' 'c' 'e' 'f' 'g' 'h' 'i' 'j']
1164 > (zing (limo [(limo [1 'a' 2 'b' ~]) (limo [3 'c' 4 'd' ~]) ~]))
1165 ~[1 97 2 98 3 99 4 100]
1168***