on dffd855 help sign in
1649 lines 22 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=2]
170```
172```
173> (find "bab" "cbabab")
174[~ u=1]
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 list.
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```
788If you check the definition of `+mul`, you'll see that it also utilizes this
789pattern.
791We can check explicitly what sequence of operations `+reel` performs like this:
793```
794> =f |: [l='e_l' r='e_r']
795 ^- @t
796 :((cury cat 3) '(' l '*' r ')')
797> (reel "abcde" f)
798'(a*(b*(c*(d*(e*e_r)))))'
799```
801---
803## `++roll`
805Left fold
807Left fold: moves left to right across a list `a`, recursively slamming a
808binary gate `b` with an element from the `list` and an accumulator,
809producing the final value of the accumulator.
811(To "slam" means to call a gate and give it a sample/samples. In this instance,
812`a` is the list of samples that are given to the gate b.)
814The initial value of the accumulator is `b`'s second argument (`+<+`). This can
815occasionally produce undesired behavior (see examples). If you need more control
816over the initial value, try making use of `$_` and `|:`, or perhaps
817[`+spin`](#spin) or [`+spun`](#spun).
819#### Accepts
821`a` is a list.
823`b` is a binary gate.
825#### Produces
827The accumulator, which is a noun.
829#### Source
831```hoon
832++ roll
833 ~/ %roll
834 |* [a=(list) b=_=>(~ |=([* *] +<+))]
835 |- ^+ ,.+<+.b
836 ?~ a
837 +<+.b
838 $(a t.a, b b(+<+ (b i.a +<+.b)))
839```
841#### Examples
843```
844> (roll `(list @)`[1 2 3 4 5 ~] add)
845q=15
847> (roll `(list @)`[6 3 1 ~] sub)
848! subtract-underflow
849! exit
851> (roll `(list @)`[1 3 6 ~] sub)
852q=4
853```
855`+mul`'s default sample is 1, so calling `+roll` with `+mul` yields the expected behavior:
857```
858> *mul
861> (roll `(list @)`~[1 2 3 4] mul)
86224
863```
865However, if you build a gate that uses `+mul` like so, the sample defaults to 0
866since that is the bunt of `@`:
868```
869> (roll `(list @)`~[1 2 3 4] |=([a=@ b=@] (mul a b)))
871```
873We can fix this with `|:`:
875```
876> (roll `(list @)`~[1 2 3 4] |:([a=1 b=1] (mul a b)))
87724
878```
880If you check the definition of `+mul`, you'll see that it also utilizes this
881pattern.
883We can check explicitly what sequence of operations `+roll` performs like this:
885```
886> =f |: [l='e_l' r='e_r']
887 ^- @t
888 :((cury cat 3) '(' l '*' r ')')
889> (roll "abcde" f)
890'(e*(d*(c*(b*(a*e_r)))))
891```
893This is in contrast to what one might expect:
895```
896> =foldl
897 |* [l=(list) f=$-([* *] *)]
898 ^- f
899 ?~ l +<-.f
900 %= $
901 +<-.f (f +<-.f i.l)
902 l t.l
903 ==
904> (foldl "abcde" f)
905'(((((e_l*a)*b)*c)*d)*e)'
906```
908---
910## `++scag`
912Prefix
914Accepts an atom `a` and `list` `b`, producing the first `a` elements of
915the front of the list.
917#### Accepts
919`a` is an atom.
921`b` is a list.
923#### Produces
925A list of the same type as `b`.
927#### Source
929```hoon
930++ scag
931 ~/ %scag
932 |* [a=@ b=(list)]
933 |- ^+ b
934 ?: |(?=(~ b) =(0 a)) ~
935 [i.b $(b t.b, a (dec a))]
936```
938#### Examples
940```
941> (scag 2 `(list @)`[1 2 3 4 ~])
942[i=1 t=~[2]]
943```
945```
946> (scag 10 `(list @)`[1 2 3 4 ~])
947[i=1 t=~[2 3 4]]
948```
950---
952## `++skid`
954Separate
956Separates a `list` `a` into two lists - Those elements of `a` who produce
957true when slammed to gate `b` and those who produce `%.n`.
959(To "slam" means to call a gate and give it a sample/samples. In this instance,
960`a` is the list of samples that are given to the gate `b`.)
962#### Accepts
964`a` is a list.
966`b` is a gate that accepts one argument and produces a flag.
968#### Produces
970A cell of two lists.
972#### Source
974```hoon
975++ skid
976 ~/ %skid
977 |* [a=(list) b=$-(* ?)]
978 |- ^+ [p=a q=a]
979 ?~ a [~ ~]
980 =+ c=$(a t.a)
981 ?:((b i.a) [[i.a p.c] q.c] [p.c [i.a q.c]])
982```
984#### Examples
986```
987> =a |=(a=@ (gth a 1))
988> (skid `(list @)`[0 1 2 3 ~] a)
989[p=[i=2 t=~[3]] q=[i=0 t=~[1]]]
990```
992---
994## `++skim`
996Suffix
998Cycles through the members of a list `a`, passing them to a gate `b` and
999producing a list of all of the members that produce `%.y`. Inverse of
1000`skip`.
1002#### Accepts
1004`a` is a list.
1006`b` is a gate that accepts one argument and produces a boolean.
1008#### Produces
1010A list.
1012#### Source
1014```hoon
1015++ skim
1016 ~/ %skim
1017 |* [a=(list) b=$-(* ?)]
1018 |-
1019 ^+ a
1020 ?~ a ~
1021 ?:((b i.a) [i.a $(a t.a)] $(a t.a))
1022```
1024#### Examples
1026```
1027> =a |=(a=@ (gth a 1))
1028> (skim `(list @)`[0 1 2 3 ~] a)
1029[i=2 t=~[3]]
1030```
1032---
1034## `++skip`
1036Except
1038Cycles through the members of `list` `a`, passing them to a gate `b`.
1039Produces a list of all of the members that produce `%.n`. Inverse of
1040`skim`.
1042#### Accepts
1044`a` is a list.
1046`b` is a gate that accepts one argument and produces a flag.
1048#### Produces
1050A list of the same type as `a`.
1052#### Source
1054```hoon
1055++ skip
1056 ~/ %skip
1057 |* [a=(list) b=$-(* ?)]
1058 |-
1059 ^+ a
1060 ?~ a ~
1061 ?:((b i.a) $(a t.a) [i.a $(a t.a)])
1062```
1064#### Examples
1066```
1067> =a |=(a=@ (gth a 1))
1068> (skip `(l)`[0 1 2 3 ~]) a)
1069[i=0 t=[i=1 t=~]]
1070```
1072---
1074## `++slag`
1076Suffix
1078Accepts an atom `a` and list `b`, producing the remaining elements from
1079`b` starting at `a`.
1081#### Accepts
1083`a` is an atom.
1085`b` is a list.
1087#### Produces
1089A list of the same type as `b`.
1091#### Source
1093```hoon
1094++ slag
1095 ~/ %slag
1096 |* [a=@ b=(list)]
1097 |- ^+ b
1098 ?: =(0 a) b
1099 ?~ b ~
1100 $(b t.b, a (dec a))
1101```
1103#### Examples
1105```
1106> (slag 2 (limo [1 2 3 4 ~]))
1107[i=3 t=[i=4 t=~]]
1108> (slag 1 (limo [1 2 3 4 ~]))
1109[i=2 t=[i=3 t=[i=4 t=~]]]
1110```
1112---
1114## `++snag`
1116Index
1118Accepts an atom `a` and a `++list` `b`, producing the element at the index
1119of `a`and failing if the list is null. Lists are 0-indexed.
1121#### Accepts
1123`a` is an atom.
1125`b` is a list.
1127#### Produces
1129Produces an element of `b`, or crashes if no element exists at that index.
1131#### Source
1133```hoon
1134++ snag
1135 ~/ %snag
1136 |* [a=@ b=(list)]
1137 |- ^+ ?>(?=(^ b) i.b)
1138 ?~ b
1139 ~_ leaf+"snag-fail"
1140 !!
1141 ?: =(0 a) i.b
1142 $(b t.b, a (dec a))
1143```
1145#### Examples
1147```
1148> (snag 2 "asdf")
1149'd'
1150```
1152```
1153> (snag 0 `(list @ud)`~[1 2 3 4])
1155```
1157---
1159## `++snap`
1161Replace item at index
1163Accepts a `list` `a`, an atom `b`, and a noun `c`, producing the list of `a` with the item at index `b` replaced with `c`.
1165#### Accepts
1167`a` is a list.
1169`b` is a atom.
1171`c` is a noun.
1173#### Produces
1175the list of `a` with the item at index `b` replaced with `c`.
1177#### Source
1179```hoon
1180++ snap
1181 ~/ %snap
1182 |* [a=(list) b=@ c=*]
1183 ^+ a
1184 (weld (scag b a) [c (slag +(b) a)])
1185```
1187#### Examples
1189```
1190> (snap (limo ~[2 3 4]) 1 11)
1191~[2 11 4]
1192```
1194---
1196## `++snip`
1198Drop tail off list
1200Removes the last element from list `a`.
1202#### Accepts
1204`a` is a `list`.
1206#### Produces
1208A `list`.
1210#### Source
1212```hoon
1213++ snip
1214 ~/ %snip
1215 |* a=(list)
1216 ^+ a
1217 ?~ a ~
1218 ?: =(~ t.a) ~
1219 [i.a $(a t.a)]
1220```
1222#### Examples
1224```
1225> `tape`(snip "foobar")
1226"fooba"
1227```
1229```
1230> (snip ~)
1232```
1234---
1236## `++snoc`
1238Append
1240Accepts a `++list` `a` and a noun `b`, producing the list of `b` appended to `a`.
1242#### Accepts
1244`a` is a list.
1246`b` is a noun.
1248#### Produces
1250Produces a list of `b` appended to `a`.
1252#### Source
1254```hoon
1255++ snoc
1256 |* [a=(list) b=*]
1257 (weld a ^+(a [b]~))
1258```
1260#### Examples
1262```
1263> `tape`(zing (snoc `(list tape)`~["a" "bc" "def"] "g"))
1264"abcdefg"
1265> (snoc `(list @ud)`~[1 2 3] 4)
1266~[1 2 3 4]
1267```
1269---
1271## `++sort`
1273Quicksort
1275Quicksort: accepts a `++list` `a` and a gate `b` which accepts two nouns and
1276produces a flag. `++sort` then produces a list of the elements of `a`,
1277sorted according to `b`.
1279#### Accepts
1281`a` is a list.
1283`b` is a gate that accepts two nouns and produces a boolean.
1285#### Produces
1287A list
1289#### Source
1291```hoon
1292++ sort !.
1293 ~/ %sort
1294 |* [a=(list) b=$-([* *] ?)]
1295 => .(a ^.(homo a))
1296 |- ^+ a
1297 ?~ a ~
1298 =+ s=(skid t.a |:(c=i.a (b c i.a)))
1299 %+ weld
1300 $(a p.s)
1301 ^+ t.a
1302 [i.a $(a q.s)]
1303```
1305#### Examples
1307```
1308> (sort `(list @)`[0 1 2 3 ~] gth)
1309~[3 2 1 0]
1310```
1312---
1314## `++spin` {% #spin %}
1316Gate to list, with state
1318Accepts a `++list` `a`, some state `b`, and a gate `c`. `c` is called with
1319a tuple -- the head is an element of `a` and the tail is the state `b`, and
1320should produce a tuple of the transformed element and the (potentially
1321modified) state `b`. Produces a pair where the first element is a list of the
1322transformed elements of `a`, and the second element is the final value of `b`.
1324#### Accepts
1326`a` is a `++list`.
1328`b` is a noun.
1330`c` is a gate.
1332#### Produces
1334A pair of a list and a noun.
1336#### Source
1338```hoon
1339++ spin
1340 ~/ %spin
1341 |* [a=(list) b=* c=_|=(^ [** +<+])]
1342 => .(c `$-([_?>(?=(^ a) i.a) _b] [_-:(c) _b])`c)
1343 =/ acc=(list _-:(c)) ~
1344 |- ^- (pair _acc _b)
1345 ?~ a
1346 [(flop acc) b]
1347 =^ res b (c i.a b)
1348 $(acc [res acc], a t.a)
1349```
1351#### Examples
1353```
1354> %^ spin (limo ~[4 5 6]) :: Trivial example -- does nothing with the state
1356 |=([n=@ a=@] [n a])
1357[p=~[4 5 6] q=0]
1358```
1360```
1361> %^ spin (limo ~[4 5 6]) :: Form a pair with `p` as the index and `q` as the list element
1363 |=([n=@ a=@] [`(pair)`[a n] +(a)])
1364[p=~[[p=0 q=4] [p=1 q=5] [p=2 q=6]] q=3]
1365```
1367```
1368> %^ spin (reap 10 0) :: Create 10 random numbers less than `10`
1369 ~(. og eny)
1370 |=([n=@ rng=_og] (rads:rng 10))
1371[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>}>]
1372```
1374#### Discussion
1376`(~(rads og eny) 2)` creates a random number less than `2`, seeding the RNG
1377with entropy (`eny`). The head of the product is the random number, the tail
1378is the continuation of the RNG.
1380---
1382## `++spun` {% #spun %}
1384Gate to list, with state
1386Accepts a `list` `a` and a gate `b`. `c` is internal state, initially
1387derived by **bunting** the tail of the sample of gate `b`, instead of
1388being passed in explicitly as in `++spin`. Produces a list with the
1389gate applied to each element of the original list. `b` is called with a tuple --
1390the head is an element of `a` and the tail is the state `c`, and should
1391produce a tuple of the transformed element and the (potentially modified)
1392state `c`.
1394#### Accepts
1396`a` is a `++list`.
1398`b` is a gate.
1400#### Produces
1402A list.
1404#### Source
1406```hoon
1407++ spun
1408 ~/ %spun
1409 |* [a=(list) b=_|=(^ [** +<+])]
1410 p:(spin a +<+.b b)
1411```
1413#### Examples
1415```
1416> %+ spun (limo ~[4 5 6]) :: `p` as the index and `q` as the list element
1417 |=([n=@ a=@] [`(pair)`[a n] +(a)])
1418~[[p=0 q=4] [p=1 q=5] [p=2 q=6]]
1419```
1421```
1422> =l (limo ~[7 8 9])
1423> %+ spun (limo ~[4 5 6]) :: joins two lists into a list of pairs
1424 |=([n=@ a=@] [`(pair)`[(snag a l) n] +(a)])
1425~[[p=7 q=4] [p=8 q=5] [p=9 q=6]]
1426```
1428---
1430## `++swag`
1432Infix
1434Similar to `substr` in Javascript: extracts a string infix, beginning at
1435inclusive index `a`, producing `b` number of characters.
1437#### Accepts
1439`a` is an atom.
1441`b` is an atom.
1443`c` is a list.
1445#### Produces
1447A list of the same type as `c`.
1449#### Source
1451```hoon
1452++ swag
1453 |* [[a=@ b=@] c=(list)]
1454 (scag +<-> (slag +<-< c))
1455```
1457#### Examples
1459```
1460> (swag [2 5] "roly poly")
1461"ly po"
1462```
1464```
1465> (swag [2 2] (limo [1 2 3 4 ~]))
1466[i=3 t=[i=4 t=~]]
1467```
1469---
1471## `++turn`
1473Gate to list
1475Accepts a `++list` `a` and a gate `b`. Produces a list with the gate applied
1476to each element of the original list.
1478#### Accepts
1480`a` is a list.
1482`b` is a gate.
1484#### Produces
1486A list.
1488#### Source
1490```hoon
1491++ turn
1492 ~/ %turn
1493 |* [a=(list) b=gate]
1494 => .(a (homo a))
1495 ^- (list _?>(?=(^ a) (b i.a)))
1496 |-
1497 ?~ a ~
1498 [i=(b i.a) t=$(a t.a)]
1499```
1501#### Examples
1503```
1504> (turn (limo [104 111 111 110 ~]) @t)
1505<|h o o n|>
1506```
1508```
1509> =a |=(a=@ (add a 4))
1510> (turn (limo [1 2 3 4 ~]) a)
1511~[5 6 7 8]
1512```
1514#### Discussion
1516`turn` is Hoon's version of 'map' in Haskell.
1518---
1520## `++weld`
1522Concatenate
1524Concatenate two `++list`s `a` and `b`.
1526#### Accepts
1528`a` and `b` are lists.
1530#### Source
1532```hoon
1533++ weld
1534 ~/ %weld
1535 |* [a=(list) b=(list)]
1536 => .(a ^.(homo a), b ^.(homo b))
1537 |- ^+ b
1538 ?~ a b
1539 [i.a $(a t.a)]
1540```
1542#### Examples
1544```
1545> (weld "urb" "it")
1546"urbit"
1547```
1549```
1550> (weld (limo [1 2 ~]) (limo [3 4 ~]))
1551~[1 2 3 4]
1552```
1554---
1556## `++welp`
1558Perfect weld
1560Concatenate two `++list`s `a` and `b` without losing their type information
1561to homogenization.
1563#### Accepts
1565`a` is a list.
1567`b` is a list.
1569#### Produces
1571A list.
1573#### Source
1575```hoon
1576++ welp
1577 ~/ %welp
1578 =| [* *]
1579 |@
1580 ++ $
1581 ?~ +<-
1582 +<-(. +<+)
1583 +<-(+ $(+<- +<->))
1584 --
1585```
1587#### Examples
1589```
1590> (welp "foo" "bar")
1591"foobar"
1592```
1594```
1595> (welp ~[60 61 62] ~[%a %b %c])
1596[60 61 62 %a %b %c ~]
1598> ? (welp ~[60 61 62] ~[%a %b %c])
1599 {@ud @ud @ud $a $b $c $~}
1600[60 61 62 %a %b %c ~]
1601```
1603```
1604> (welp [sa+1 so+2 ~] si=3)
1605[[%sa 1] [%so 2] si=3]
1606```
1608---
1610## `++zing`
1612Turns a `++list` of lists into a single list by promoting the elements of
1613each sublist into the higher.
1615#### Accepts
1617A list of lists.
1619#### Produces
1621A list.
1623#### Source
1625```hoon
1626++ zing
1627 ~/ %zing
1628 =| *
1629 |@
1630 ++ $
1631 ?~ +<
1632 +<
1633 (welp +<- $(+< +<+))
1634 --
1635```
1637#### Examples
1639```
1640> (zing (limo [(limo ['a' 'b' 'c' ~]) (limo ['e' 'f' 'g' ~]) (limo ['h' 'i' 'j' ~]) ~]))
1641~['a' 'b' 'c' 'e' 'f' 'g' 'h' 'i' 'j']
1642```
1644```
1645> (zing (limo [(limo [1 'a' 2 'b' ~]) (limo [3 'c' 4 'd' ~]) ~]))
1646~[1 97 2 98 3 99 4 100]
1647```
1649---