on e2c63b7 help sign in
1608 lines 21 kB history raw
1+++
2title = "2b: List Logic"
3weight = 5
4template = "doc.html"
5+++
7## `++bake`
9**Note:** This function isn't specifically a list function but is included in section 2b of the standard library so is documented here for completeness.
11Convert wet gate `f` to a dry gate by specifying argument mold `a`.
13`+bake` is a wet gate that takes a wet gate and produces a dry gate.
15#### Accepts
17`f` is a `gate`.
19`a` is a `mold`.
21#### Produces
23A dry `gate` whose sample type is `a`.
25#### Source
27```hoon
28++ bake
29 |* [f=gate a=mold]
30 |= arg=a
31 (f arg)
32```
34#### Examples
36```
37> =wet-gate |*(a=* [a a])
38> (wet-gate 42)
39[42 42]
40> (wet-gate ['foo' 'bar'])
41[['foo' 'bar'] 'foo' 'bar']
42> =dry-gate (bake wet-gate @ud)
43> (dry-gate 42)
44[42 42]
45> (dry-gate ['foo' 'bar'])
46-need.@ud
47-have.[@t @t]
48nest-fail
49```
51---
53## `++fand`
55All indices in `list`
57Produces the indices of all occurrences of `nedl` in `hstk` as a `list` of
58atoms.
60#### Accepts
62`nedl` is a list.
64`hstk` is a list.
66#### Produces
68A `list`.
70#### Source
72```hoon
73++ fand
74 ~/ %fand
75 |= [nedl=(list) hstk=(list)]
76 =| i=@ud
77 =| fnd=(list @ud)
78 |- ^+ fnd
79 =+ [n=nedl h=hstk]
80 |-
81 ?: |(?=(~ n) ?=(~ h))
82 (flop fnd)
83 ?: =(i.n i.h)
84 ?~ t.n
85 ^$(i +(i), hstk +.hstk, fnd [i fnd])
86 $(n t.n, h t.h)
87 ^$(i +(i), hstk +.hstk)
88```
90#### Examples
92```
93> (fand ~[3] ~[1 2 3])
94~[2]
95```
97```
98> (fand ~[4] ~[1 2 3])
100```
102```
103> (fand ~['a'] "cbabab")
104~[2 4]
105```
107```
108> (fand "ba" "cbabab")
109~[1 3]
110```
112---
114## `++find`
116First index in `list`
118Produces the index of the first occurrence of `nedl` in `hstk` as the `unit`
119of an atom.
121#### Accepts
123`nedl` is a list.
125`hstk` is a list.
127#### Produces
129The `unit` of an atom.
131#### Source
133```hoon
134++ find
135 ~/ %find
136 |= [nedl=(list) hstk=(list)]
137 =| i=@ud
138 |- ^- (unit @ud)
139 =+ [n=nedl h=hstk]
140 |-
141 ?: |(?=(~ n) ?=(~ h))
142 ~
143 ?: =(i.n i.h)
144 ?~ t.n
145 `i
146 $(n t.n, h t.h)
147 ^$(i +(i), hstk +.hstk)
148```
150#### Examples
152```
153> (find [3]~ ~[1 2 3])
154[~ u=2]
155```
157```
158> (find [4]~ ~[1 2 3])
160```
162```
163> (find ['c']~ "cbabab")
164[~ u=0]
165```
167```
168> (find "ab" "cbabab")
169[~ u=1]
170```
172```
173> (find "bab" "cbabab")
174[~ u=2]
175```
177---
179## `++flop`
181Reverse
183Produces the `list` `a` in reverse order.
185#### Accepts
187`a` is a `list`.
189#### Produces
191A `list`.
193#### Source
195```hoon
196++ flop
197 ~/ %flop
198 |* a=(list)
199 => .(a (homo a))
200 ^+ a
201 =+ b=`_a`~
202 |-
203 ?~ a b
204 $(a t.a, b [i.a b])
205```
207#### Examples
209```
210> =a [1 2 3 ~]
211> (flop a)
212~[3 2 1]
213```
215```
216> (flop (flop a))
217~[1 2 3]
218```
220---
222## `++gulf`
224List from range
226Produces a `list` composed of each consecutive integer starting from `a` and
227ending with `b`. `a` and `b` are themselves included.
229#### Accepts
231`a` is an atom.
233`b` is an atom.
235#### Produces
237a `list`.
239#### Source
241```hoon
242++ gulf
243 |= [a=@ b=@]
244 ?> (lte a b)
245 |- ^- (list @)
246 ?:(=(a +(b)) ~ [a $(a +(a))])
247```
249#### Examples
251```
252> (gulf 1 6)
253~[1 2 3 4 5 6]
254```
256```
257> `(list @t)`(gulf 99 106)
258<|c d e f g h i j|>
259```
261---
263## `++homo`
265Homogenize
267Produces a `list` whose type is a fork of all the contained types in the
268list `a`. Used when you want to make all the types of the elements of a list the
269same.
271#### Accepts
273`a` is a `list`.
275#### Produces
277a `list`.
279#### Source
281```hoon
282++ homo
283 |* a=(list)
284 ^+ =< $
285 |@ ++ $ ?:(*? ~ [i=(snag 0 a) t=$])
286 --
287 a
288```
290#### Examples
292```
293> lyst
294[i=1 t=[i=97 t=[i=2 t=[i=98 t=[i=[~ u=10] t=~]]]]]
295> (homo lyst)
296~[1 97 2 98 [~ u=10]]
297```
299```
300> =a (limo [1 2 3 ~])
301> a
302[i=1 t=[i=2 t=[i=3 t=~]]]
303> (homo a)
304~[1 2 3]
305```
307---
309## `++into`
311Insert item at index
313Accepts a `list` `a`, an atom `b`, and a noun `c`, producing the list of `a` with the item `c` inserted at index `b`.
315#### Accepts
317`a` is a list.
319`b` is a atom.
321`c` is a noun.
323#### Produces
325the list of `a` with the item `c` inserted at index `b`.
327#### Source
329```hoon
330++ into
331 ~/ %into
332 |* [a=(list) b=@ c=*]
333 ^+ a
334 (weld (scag b a) [c (slag b a)])
335```
337#### Examples
339```
340> (into (limo ~[2 3 4]) 1 11)
341~[2 11 3 4]
342```
344---
346## `++join`
348Constructs a new `list`, placing `sep` between every element of `lit`.
350#### Accepts
352`sep` is a `noun`.
354`lit` is a `list`.
356#### Produces
358a `list`.
360#### Source
362```hoon
363++ join
364 |* [sep=* lit=(list)]
365 =. sep `_?>(?=(^ lit) i.lit)`sep
366 ?~ lit ~
367 =| out=(list _?>(?=(^ lit) i.lit))
368 |- ^+ out
369 ?~ t.lit
370 (flop [i.lit out])
371 $(out [sep i.lit out], lit t.lit)
372```
374#### Examples
376```
377> (join ' ' "hoon")
378"h o o n"
379```
381```
382> (join 0 `(list @)`~[1 2 3])
383~[1 0 2 0 3]
384```
386---
388## `++lent`
390List length
392Produces the length of any `list` `a` as an atom.
394#### Accepts
396`a` is a `list`.
398#### Produces
400an atom.
402#### Source
404```hoon
405++ lent
406 ~/ %lent
407 |= a=(list)
408 ^- @
409 =+ b=0
410 |-
411 ?~ a b
412 $(a t.a, b +(b))
413```
415#### Examples
417```
418> (lent [1 2 3 4 ~]))
420```
422```
423> (lent [1 'a' 2 'b' (some 10) ~])
425```
427---
429## `++levy`
431Logical "and" on list
433Computes the Boolean logical "and" on the results of gate `b` applied to each individual element in `list` `a`.
435#### Accepts
437`a` is a list.
439`b` is a gate.
441#### Produces
443A boolean.
445#### Source
447```hoon
448++ levy
449 ~/ %levy
450 |* [a=(list) b=$-(* ?)]
451 |- ^- ?
452 ?~ a &
453 ?. (b i.a) |
454 $(a t.a)
455```
457#### Examples
459```
460> =a |=(a=@ (lte a 1))
461> (levy `(list @)`[0 1 2 1 ~] a)
462%.n
463```
465```
466> =a |=(a=@ (lte a 3))
467> (levy `(list @)`[0 1 2 1 ~] a)
468%.y
469```
471---
473## `++lien`
475Logical "or" on list
477Computes the Boolean logical "or" on the results of applying `gate` `b` to
478every element of `++list` `a`.
480#### Accepts
482`a` is a list.
484`b` is a gate.
486#### Source
488```hoon
489++ lien
490 ~/ %lien
491 |* [a=(list) b=$-(* ?)]
492 |- ^- ?
493 ?~ a |
494 ?: (b i.a) &
495 $(a t.a)
496```
498#### Examples
500```
501> =a |=(a=@ (gte a 1))
502> (lien `(list @)`[0 1 2 1 ~] a)
503%.y
504```
506```
507> =a |=(a=@ (gte a 3))
508> (lien `(list @)`[0 1 2 1 ~]) a)
509%.n
510```
512---
514## `++limo`
516List Constructor
518Turns a null-terminated tuple into a `list`.
520#### Accepts
522`a` is a null-terminated tuple.
524#### Produces
526A `++list`.
528#### Source
530```hoon
531++ limo
532 |* a=*
533 ^+ =< $
534 |@ ++ $ ?~(a ~ ?:(*? [i=-.a t=$] $(a +.a)))
535 --
536 a
537```
539#### Examples
541```
542> (limo [1 2 3 ~])
543[i=1 t=[i=2 t=[i=3 t=~]]]
544```
546---
548## `++murn`
550Maybe transform
552Passes each member of `list` `a` to gate `b`, which must produce a
553`unit`. Produces a new list with all the results that do not produce
554`~`.
556#### Accepts
558`a` is a list.
560`b` is a gate that produces a unit.
562#### Produces
564A unit.
566#### Source
568```hoon
569++ murn
570 ~/ %murn
571 |* [a=(list) b=$-(* (unit))]
572 => .(a (homo a))
573 |- ^- (list _?>(?=(^ a) (need (b i.a))))
574 ?~ a ~
575 =/ c (b i.a)
576 ?~ c $(a t.a)
577 [+.c $(a t.a)]
578```
580#### Examples
582```
583> =a |=(a=@ ?.((gte a 2) ~ (some (add a 10))))
584> (murn `(list @)`[0 1 2 3 ~] a)
585[i=12 t=[i=13 t=~]]
586```
588---
590## `++oust`
592Remove
594Removes elements from list `c` beginning at inclusive index `a`, removing `b`
595number of elements.
597#### Accepts
599`c` is a list.
601#### Produces
603A `++list`.
605#### Source
607```hoon
608++ oust
609 ~/ %oust
610 |* [[a=@ b=@] c=(list)]
611 (weld (scag +<-< c) (slag (add +<-< +<->) c))
612```
614#### Examples
616```
617> (oust [4 5] "good day, urbit!")
618"good urbit!"
619```
621```
622> (oust [2 2] `(list @)`[1 2 3 4 ~])
623~[1 2]
624```
626---
628## `++reap`
630Replicate
632Replicate: produces a `list` containing `a` copies of `b`.
634#### Accepts
636`a` is an atom.
638`b` is a noun.
640#### Produces
642A list.
644#### Source
646```hoon
647++ reap
648 ~/ %reap
649 |* [a=@ b=*]
650 |- ^- (list _b)
651 ?~ a ~
652 [b $(a (dec a))]
653```
655#### Examples
657```
658> (reap 20 %a)
659~[%a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a]
660```
662```
663> (reap 5 ~s1)
664~[~s1 ~s1 ~s1 ~s1 ~s1]
665> `@dr`(roll (reap 5 ~s1) add)
666~s5
667```
669---
671## `++rear`
673Last item of list
675Produces the last item in list `a`, crashing if `a` is null.
677#### Accepts
679`a` is a `list`.
681#### Produces
683The type of the last element in `a`.
685#### Source
687```hoon
688++ rear
689 ~/ %rear
690 |* a=(list)
691 ^- _?>(?=(^ a) i.a)
692 ?> ?=(^ a)
693 ?: =(~ t.a) i.a
694 $(a t.a)
695```
697#### Examples
699```
700> (rear ~[1 2 3])
702```
704```
705> (rear ~)
706dojo: hoon expression failed
707```
709---
711## `++reel`
713Right fold
715Right fold: moves right to left across a `list` `a`, recursively slamming
716a binary gate `b` with an element from `a` and an accumulator, producing
717the final value of the accumulator.
719(To "slam" means to call a gate and give it a sample/samples. In this instance,
720`a` is the list of samples that are given to the gate b.)
722The initial value of the accumulator is the bunt of `b`'s second argument (`+<+`). This can occasionally produce
723undesired behavior (see examples). If you need more control over the initial
724value, try making use of `$_` and `|:`, or perhaps [`+spin`](#spin) or
725[`+spun`](#spun).
727#### Accepts
729`a` is a list.
731`b` is a binary gate.
733#### Produces
735The accumulator, which is a noun.
737#### Source
739```hoon
740++ reel
741 ~/ %reel
742 |* [a=(list) b=_=>(~ |=([* *] +<+))]
743 |- ^+ ,.+<+.b
744 ?~ a
745 +<+.b
746 (b i.a $(a t.a))
747```
749#### Examples
751```
752> (reel `(list @)`[1 2 3 4 5 ~] add)
75315
755> (reel `(list @)`[6 3 1 ~] sub)
758> (reel `(list @)`[3 6 1 ~] sub)
759! subtract-underflow
760! exit
761```
763`+mul`'s default sample is 1, so calling `+reel` with `+mul` yields the expected behavior:
765```
766> *mul
769> (reel `(list @)`~[1 2 3 4] mul)
77024
771```
773However, if you build a gate that uses `+mul` like so, the sample defaults to 0
774since that is the bunt of `@`:
776```
777> (reel `(list @)`~[1 2 3 4] |=([a=@ b=@] (mul a b)))
779```
781We can fix this with `|:`:
783```
784> (reel `(list @)`~[1 2 3 4] |:([a=1 b=1] (mul a b)))
78524
786```
788---
790## `++roll`
792Left fold
794Left fold: moves left to right across a list `a`, recursively slamming a
795binary gate `b` with an element from the `list` and an accumulator,
796producing the final value of the accumulator.
798(To "slam" means to call a gate and give it a sample/samples. In this instance,
799`a` is the list of samples that are given to the gate b.)
801The initial value of the accumulator is `b`'s second argument (`+<+`). This can
802occasionally produce undesired behavior (see examples). If you need more control
803over the initial value, try making use of `$_` and `|:`, or perhaps
804[`+spin`](#spin) or [`+spun`](#spun).
806#### Accepts
808`a` is a list.
810`b` is a binary gate.
812#### Produces
814The accumulator, which is a noun.
816#### Source
818```hoon
819++ roll
820 ~/ %roll
821 |* [a=(list) b=_=>(~ |=([* *] +<+))]
822 |- ^+ ,.+<+.b
823 ?~ a
824 +<+.b
825 $(a t.a, b b(+<+ (b i.a +<+.b)))
826```
828#### Examples
830```
831> (roll `(list @)`[1 2 3 4 5 ~] add)
832q=15
834> (roll `(list @)`[6 3 1 ~] sub)
835! subtract-underflow
836! exit
838> (roll `(list @)`[1 3 6 ~] sub)
839q=4
840```
842`+mul`'s default sample is 1, so calling `+roll` with `+mul` yields the expected behavior:
844```
845> *mul
848> (roll `(list @)`~[1 2 3 4] mul)
84924
850```
852However, if you build a gate that uses `+mul` like so, the sample defaults to 0
853since that is the bunt of `@`:
855```
856> (roll `(list @)`~[1 2 3 4] |=([a=@ b=@] (mul a b)))
858```
860We can fix this with `|:`:
862```
863> (roll `(list @)`~[1 2 3 4] |:([a=1 b=1] (mul a b)))
86424
865```
867---
869## `++scag`
871Prefix
873Accepts an atom `a` and `list` `b`, producing the first `a` elements of
874the front of the list.
876#### Accepts
878`a` is an atom.
880`b` is a list.
882#### Produces
884A list of the same type as `b`.
886#### Source
888```hoon
889++ scag
890 ~/ %scag
891 |* [a=@ b=(list)]
892 |- ^+ b
893 ?: |(?=(~ b) =(0 a)) ~
894 [i.b $(b t.b, a (dec a))]
895```
897#### Examples
899```
900> (scag 2 `(list @)`[1 2 3 4 ~])
901[i=1 t=~[2]]
902```
904```
905> (scag 10 `(list @)`[1 2 3 4 ~])
906[i=1 t=~[2 3 4]]
907```
909---
911## `++skid`
913Separate
915Separates a `list` `a` into two lists - Those elements of `a` who produce
916true when slammed to gate `b` and those who produce `%.n`.
918(To "slam" means to call a gate and give it a sample/samples. In this instance,
919`a` is the list of samples that are given to the gate `b`.)
921#### Accepts
923`a` is a list.
925`b` is a gate that accepts one argument and produces a flag.
927#### Produces
929A cell of two lists.
931#### Source
933```hoon
934++ skid
935 ~/ %skid
936 |* [a=(list) b=$-(* ?)]
937 |- ^+ [p=a q=a]
938 ?~ a [~ ~]
939 =+ c=$(a t.a)
940 ?:((b i.a) [[i.a p.c] q.c] [p.c [i.a q.c]])
941```
943#### Examples
945```
946> =a |=(a=@ (gth a 1))
947> (skid `(list @)`[0 1 2 3 ~] a)
948[p=[i=2 t=~[3]] q=[i=0 t=~[1]]]
949```
951---
953## `++skim`
955Suffix
957Cycles through the members of a list `a`, passing them to a gate `b` and
958producing a list of all of the members that produce `%.y`. Inverse of
959`skip`.
961#### Accepts
963`a` is a list.
965`b` is a gate that accepts one argument and produces a boolean.
967#### Produces
969A list.
971#### Source
973```hoon
974++ skim
975 ~/ %skim
976 |* [a=(list) b=$-(* ?)]
977 |-
978 ^+ a
979 ?~ a ~
980 ?:((b i.a) [i.a $(a t.a)] $(a t.a))
981```
983#### Examples
985```
986> =a |=(a=@ (gth a 1))
987> (skim `(list @)`[0 1 2 3 ~] a)
988[i=2 t=~[3]]
989```
991---
993## `++skip`
995Except
997Cycles through the members of `list` `a`, passing them to a gate `b`.
998Produces a list of all of the members that produce `%.n`. Inverse of
999`skim`.
1001#### Accepts
1003`a` is a list.
1005`b` is a gate that accepts one argument and produces a flag.
1007#### Produces
1009A list of the same type as `a`.
1011#### Source
1013```hoon
1014++ skip
1015 ~/ %skip
1016 |* [a=(list) b=$-(* ?)]
1017 |-
1018 ^+ a
1019 ?~ a ~
1020 ?:((b i.a) $(a t.a) [i.a $(a t.a)])
1021```
1023#### Examples
1025```
1026> =a |=(a=@ (gth a 1))
1027> (skip `(l)`[0 1 2 3 ~]) a)
1028[i=0 t=[i=1 t=~]]
1029```
1031---
1033## `++slag`
1035Suffix
1037Accepts an atom `a` and list `b`, producing the remaining elements from
1038`b` starting at `a`.
1040#### Accepts
1042`a` is an atom.
1044`b` is a list.
1046#### Produces
1048A list of the same type as `b`.
1050#### Source
1052```hoon
1053++ slag
1054 ~/ %slag
1055 |* [a=@ b=(list)]
1056 |- ^+ b
1057 ?: =(0 a) b
1058 ?~ b ~
1059 $(b t.b, a (dec a))
1060```
1062#### Examples
1064```
1065> (slag 2 (limo [1 2 3 4 ~]))
1066[i=3 t=[i=4 t=~]]
1067> (slag 1 (limo [1 2 3 4 ~]))
1068[i=2 t=[i=3 t=[i=4 t=~]]]
1069```
1071---
1073## `++snag`
1075Index
1077Accepts an atom `a` and a `++list` `b`, producing the element at the index
1078of `a`and failing if the list is null. Lists are 0-indexed.
1080#### Accepts
1082`a` is an atom.
1084`b` is a list.
1086#### Produces
1088Produces an element of `b`, or crashes if no element exists at that index.
1090#### Source
1092```hoon
1093++ snag
1094 ~/ %snag
1095 |* [a=@ b=(list)]
1096 |- ^+ ?>(?=(^ b) i.b)
1097 ?~ b
1098 ~_ leaf+"snag-fail"
1099 !!
1100 ?: =(0 a) i.b
1101 $(b t.b, a (dec a))
1102```
1104#### Examples
1106```
1107> (snag 2 "asdf")
1108'd'
1109```
1111```
1112> (snag 0 `(list @ud)`~[1 2 3 4])
1114```
1116---
1118## `++snap`
1120Replace item at index
1122Accepts a `list` `a`, an atom `b`, and a noun `c`, producing the list of `a` with the item at index `b` replaced with `c`.
1124#### Accepts
1126`a` is a list.
1128`b` is a atom.
1130`c` is a noun.
1132#### Produces
1134the list of `a` with the item at index `b` replaced with `c`.
1136#### Source
1138```hoon
1139++ snap
1140 ~/ %snap
1141 |* [a=(list) b=@ c=*]
1142 ^+ a
1143 (weld (scag b a) [c (slag +(b) a)])
1144```
1146#### Examples
1148```
1149> (snap (limo ~[2 3 4]) 1 11)
1150~[2 11 4]
1151```
1153---
1155## `++snip`
1157Drop tail off list
1159Removes the last element from list `a`.
1161#### Accepts
1163`a` is a `list`.
1165#### Produces
1167A `list`.
1169#### Source
1171```hoon
1172++ snip
1173 ~/ %snip
1174 |* a=(list)
1175 ^+ a
1176 ?~ a ~
1177 ?: =(~ t.a) ~
1178 [i.a $(a t.a)]
1179```
1181#### Examples
1183```
1184> `tape`(snip "foobar")
1185"fooba"
1186```
1188```
1189> (snip ~)
1191```
1193---
1195## `++snoc`
1197Append
1199Accepts a `++list` `a` and a noun `b`, producing the list of `b` appended to `a`.
1201#### Accepts
1203`a` is a list.
1205`b` is a noun.
1207#### Produces
1209Produces a list of `b` appended to `a`.
1211#### Source
1213```hoon
1214++ snoc
1215 |* [a=(list) b=*]
1216 (weld a ^+(a [b]~))
1217```
1219#### Examples
1221```
1222> `tape`(zing (snoc `(list tape)`~["a" "bc" "def"] "g"))
1223"abcdefg"
1224> (snoc `(list @ud)`~[1 2 3] 4)
1225~[1 2 3 4]
1226```
1228---
1230## `++sort`
1232Quicksort
1234Quicksort: accepts a `++list` `a` and a gate `b` which accepts two nouns and
1235produces a flag. `++sort` then produces a list of the elements of `a`,
1236sorted according to `b`.
1238#### Accepts
1240`a` is a list.
1242`b` is a gate that accepts two nouns and produces a boolean.
1244#### Produces
1246A list
1248#### Source
1250```hoon
1251++ sort !.
1252 ~/ %sort
1253 |* [a=(list) b=$-([* *] ?)]
1254 => .(a ^.(homo a))
1255 |- ^+ a
1256 ?~ a ~
1257 =+ s=(skid t.a |:(c=i.a (b c i.a)))
1258 %+ weld
1259 $(a p.s)
1260 ^+ t.a
1261 [i.a $(a q.s)]
1262```
1264#### Examples
1266```
1267> (sort `(list @)`[0 1 2 3 ~] gth)
1268~[3 2 1 0]
1269```
1271---
1273## `++spin` {#spin}
1275Gate to list, with state
1277Accepts a `++list` `a`, some state `b`, and a gate `c`. `c` is called with
1278a tuple -- the head is an element of `a` and the tail is the state `b`, and
1279should produce a tuple of the transformed element and the (potentially
1280modified) state `b`. Produces a pair where the first element is a list of the
1281transformed elements of `a`, and the second element is the final value of `b`.
1283#### Accepts
1285`a` is a `++list`.
1287`b` is a noun.
1289`c` is a gate.
1291#### Produces
1293A pair of a list and a noun.
1295#### Source
1297```hoon
1298++ spin
1299 ~/ %spin
1300 |* [a=(list) b=* c=_|=(^ [** +<+])]
1301 => .(c `$-([_?>(?=(^ a) i.a) _b] [_-:(c) _b])`c)
1302 =/ acc=(list _-:(c)) ~
1303 |- ^- (pair _acc _b)
1304 ?~ a
1305 [(flop acc) b]
1306 =^ res b (c i.a b)
1307 $(acc [res acc], a t.a)
1308```
1310#### Examples
1312```
1313> %^ spin (limo ~[4 5 6]) :: Trivial example -- does nothing with the state
1315 |=([n=@ a=@] [n a])
1316[p=~[4 5 6] q=0]
1317```
1319```
1320> %^ spin (limo ~[4 5 6]) :: Form a pair with `p` as the index and `q` as the list element
1322 |=([n=@ a=@] [`(pair)`[a n] +(a)])
1323[p=~[[p=0 q=4] [p=1 q=5] [p=2 q=6]] q=3]
1324```
1326```
1327> %^ spin (reap 10 0) :: Create 10 random numbers less than `10`
1328 ~(. og eny)
1329 |=([n=@ rng=_og] (rads:rng 10))
1330[p=~[7 8 6 0 1 5 4 7 9 3] q=<4.rvi {a/@uvJ <51.qyl 129.pdd 41.mac 1.ane $141>}>]
1331```
1333#### Discussion
1335`(~(rads og eny) 2)` creates a random number less than `2`, seeding the RNG
1336with entropy (`eny`). The head of the product is the random number, the tail
1337is the continuation of the RNG.
1339---
1341## `++spun` {#spun}
1343Gate to list, with state
1345Accepts a `list` `a` and a gate `b`. `c` is internal state, initially
1346derived by **bunting** the tail of the sample of gate `b`, instead of
1347being passed in explicitly as in `++spin`. Produces a list with the
1348gate applied to each element of the original list. `b` is called with a tuple --
1349the head is an element of `a` and the tail is the state `c`, and should
1350produce a tuple of the transformed element and the (potentially modified)
1351state `c`.
1353#### Accepts
1355`a` is a `++list`.
1357`b` is a gate.
1359#### Produces
1361A list.
1363#### Source
1365```hoon
1366++ spun
1367 ~/ %spun
1368 |* [a=(list) b=_|=(^ [** +<+])]
1369 p:(spin a +<+.b b)
1370```
1372#### Examples
1374```
1375> %+ spun (limo ~[4 5 6]) :: `p` as the index and `q` as the list element
1376 |=([n=@ a=@] [`(pair)`[a n] +(a)])
1377~[[p=0 q=4] [p=1 q=5] [p=2 q=6]]
1378```
1380```
1381> =l (limo ~[7 8 9])
1382> %+ spun (limo ~[4 5 6]) :: joins two lists into a list of pairs
1383 |=([n=@ a=@] [`(pair)`[(snag a l) n] +(a)])
1384~[[p=7 q=4] [p=8 q=5] [p=9 q=6]]
1385```
1387---
1389## `++swag`
1391Infix
1393Similar to `substr` in Javascript: extracts a string infix, beginning at
1394inclusive index `a`, producing `b` number of characters.
1396#### Accepts
1398`a` is an atom.
1400`b` is an atom.
1402`c` is a list.
1404#### Produces
1406A list of the same type as `c`.
1408#### Source
1410```hoon
1411++ swag
1412 |* [[a=@ b=@] c=(list)]
1413 (scag +<-> (slag +<-< c))
1414```
1416#### Examples
1418```
1419> (swag [2 5] "roly poly")
1420"ly po"
1421```
1423```
1424> (swag [2 2] (limo [1 2 3 4 ~]))
1425[i=3 t=[i=4 t=~]]
1426```
1428---
1430## `++turn`
1432Gate to list
1434Accepts a `++list` `a` and a gate `b`. Produces a list with the gate applied
1435to each element of the original list.
1437#### Accepts
1439`a` is a list.
1441`b` is a gate.
1443#### Produces
1445A list.
1447#### Source
1449```hoon
1450++ turn
1451 ~/ %turn
1452 |* [a=(list) b=gate]
1453 => .(a (homo a))
1454 ^- (list _?>(?=(^ a) (b i.a)))
1455 |-
1456 ?~ a ~
1457 [i=(b i.a) t=$(a t.a)]
1458```
1460#### Examples
1462```
1463> (turn (limo [104 111 111 110 ~]) @t)
1464<|h o o n|>
1465```
1467```
1468> =a |=(a=@ (add a 4))
1469> (turn (limo [1 2 3 4 ~]) a)
1470~[5 6 7 8]
1471```
1473#### Discussion
1475`turn` is Hoon's version of 'map' in Haskell.
1477---
1479## `++weld`
1481Concatenate
1483Concatenate two `++list`s `a` and `b`.
1485#### Accepts
1487`a` and `b` are lists.
1489#### Source
1491```hoon
1492++ weld
1493 ~/ %weld
1494 |* [a=(list) b=(list)]
1495 => .(a ^.(homo a), b ^.(homo b))
1496 |- ^+ b
1497 ?~ a b
1498 [i.a $(a t.a)]
1499```
1501#### Examples
1503```
1504> (weld "urb" "it")
1505"urbit"
1506```
1508```
1509> (weld (limo [1 2 ~]) (limo [3 4 ~]))
1510~[1 2 3 4]
1511```
1513---
1515## `++welp`
1517Perfect weld
1519Concatenate two `++list`s `a` and `b` without losing their type information
1520to homogenization.
1522#### Accepts
1524`a` is a list.
1526`b` is a list.
1528#### Produces
1530A list.
1532#### Source
1534```hoon
1535++ welp
1536 ~/ %welp
1537 =| [* *]
1538 |@
1539 ++ $
1540 ?~ +<-
1541 +<-(. +<+)
1542 +<-(+ $(+<- +<->))
1543 --
1544```
1546#### Examples
1548```
1549> (welp "foo" "bar")
1550"foobar"
1551```
1553```
1554> (welp ~[60 61 62] ~[%a %b %c])
1555[60 61 62 %a %b %c ~]
1557> ? (welp ~[60 61 62] ~[%a %b %c])
1558 {@ud @ud @ud $a $b $c $~}
1559[60 61 62 %a %b %c ~]
1560```
1562```
1563> (welp [sa+1 so+2 ~] si=3)
1564[[%sa 1] [%so 2] si=3]
1565```
1567---
1569## `++zing`
1571Turns a `++list` of lists into a single list by promoting the elements of
1572each sublist into the higher.
1574#### Accepts
1576A list of lists.
1578#### Produces
1580A list.
1582#### Source
1584```hoon
1585++ zing
1586 ~/ %zing
1587 =| *
1588 |@
1589 ++ $
1590 ?~ +<
1591 +<
1592 (welp +<- $(+< +<+))
1593 --
1594```
1596#### Examples
1598```
1599> (zing (limo [(limo ['a' 'b' 'c' ~]) (limo ['e' 'f' 'g' ~]) (limo ['h' 'i' 'j' ~]) ~]))
1600~['a' 'b' 'c' 'e' 'f' 'g' 'h' 'i' 'j']
1601```
1603```
1604> (zing (limo [(limo [1 'a' 2 'b' ~]) (limo [3 'c' 4 'd' ~]) ~]))
1605~[1 97 2 98 3 99 4 100]
1606```
1608---