2title = "2b: List Logic"
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.
23A dry `gate` whose sample type is `a`.
37> =wet-gate |*(a=* [a a])
40> (wet-gate ['foo' 'bar'])
41[['foo' 'bar'] 'foo' 'bar']
42> =dry-gate (bake wet-gate @ud)
45> (dry-gate ['foo' 'bar'])
57Produces the indices of all occurrences of `nedl` in `hstk` as a `list` of
75 |= [nedl=(list) hstk=(list)]
85 ^$(i +(i), hstk +.hstk, fnd [i fnd])
87 ^$(i +(i), hstk +.hstk)
103> (fand ~['a'] "cbabab")
108> (fand "ba" "cbabab")
118Produces the index of the first occurrence of `nedl` in `hstk` as the `unit`
129The `unit` of an atom.
136 |= [nedl=(list) hstk=(list)]
141 ?: |(?=(~ n) ?=(~ h))
147 ^$(i +(i), hstk +.hstk)
153> (find [3]~ ~[1 2 3])
158> (find [4]~ ~[1 2 3])
163> (find ['c']~ "cbabab")
168> (find "ab" "cbabab")
173> (find "bab" "cbabab")
183Produces the `list` `a` in reverse order.
226Produces a `list` composed of each consecutive integer starting from `a` and
227ending with `b`. `a` and `b` are themselves included.
246 ?:(=(a +(b)) ~ [a $(a +(a))])
257> `(list @t)`(gulf 99 106)
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
285 |@ ++ $ ?:(*? ~ [i=(snag 0 a) t=$])
294[i=1 t=[i=97 t=[i=2 t=[i=98 t=[i=[~ u=10] t=~]]]]]
302[i=1 t=[i=2 t=[i=3 t=~]]]
313Accepts a `list` `a`, an atom `b`, and a noun `c`, producing the list of `a` with the item `c` inserted at index `b`.
325the list of `a` with the item `c` inserted at index `b`.
332 |* [a=(list) b=@ c=*]
334 (weld (scag b a) [c (slag b a)])
340> (into (limo ~[2 3 4]) 1 11)
348Constructs a new `list`, placing `sep` between every element of `lit`.
364 |* [sep=* lit=(list)]
365 =. sep `_?>(?=(^ lit) i.lit)`sep
367 =| out=(list _?>(?=(^ lit) i.lit))
371 $(out [sep i.lit out], lit t.lit)
382> (join 0 `(list @)`~[1 2 3])
392Produces the length of any `list` `a` as an atom.
423> (lent [1 'a' 2 'b' (some 10) ~])
433Computes the Boolean logical "and" on the results of gate `b` applied to each individual element in `list` `a`.
450 |* [a=(list) b=$-(* ?)]
460> =a |=(a=@ (lte a 1))
461> (levy `(list @)`[0 1 2 1 ~] a)
466> =a |=(a=@ (lte a 3))
467> (levy `(list @)`[0 1 2 1 ~] a)
477Computes the Boolean logical "or" on the results of applying `gate` `b` to
478every element of `++list` `a`.
491 |* [a=(list) b=$-(* ?)]
501> =a |=(a=@ (gte a 1))
502> (lien `(list @)`[0 1 2 1 ~] a)
507> =a |=(a=@ (gte a 3))
508> (lien `(list @)`[0 1 2 1 ~]) a)
518Turns a null-terminated tuple into a `list`.
522`a` is a null-terminated tuple.
534 |@ ++ $ ?~(a ~ ?:(*? [i=-.a t=$] $(a +.a)))
543[i=1 t=[i=2 t=[i=3 t=~]]]
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
560`b` is a gate that produces a unit.
571 |* [a=(list) b=$-(* (unit))]
573 |- ^- (list _?>(?=(^ a) (need (b i.a))))
583> =a |=(a=@ ?.((gte a 2) ~ (some (add a 10))))
584> (murn `(list @)`[0 1 2 3 ~] a)
594Removes elements from list `c` beginning at inclusive index `a`, removing `b`
610 |* [[a=@ b=@] c=(list)]
611 (weld (scag +<-< c) (slag (add +<-< +<->) c))
617> (oust [4 5] "good day, urbit!")
622> (oust [2 2] `(list @)`[1 2 3 4 ~])
632Replicate: produces a `list` containing `a` copies of `b`.
659~[%a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a]
664~[~s1 ~s1 ~s1 ~s1 ~s1]
665> `@dr`(roll (reap 5 ~s1) add)
675Produces the last item in list `a`, crashing if `a` is null.
683The type of the last element in `a`.
706dojo: hoon expression failed
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
735The accumulator, which is a noun.
742 |* [a=(list) b=_=>(~ |=([* *] +<+))]
752> (reel `(list @)`[1 2 3 4 5 ~] add)
755> (reel `(list @)`[6 3 1 ~] sub)
758> (reel `(list @)`[3 6 1 ~] sub)
763`+mul`'s default sample is 1, so calling `+reel` with `+mul` yields the expected behavior:
769> (reel `(list @)`~[1 2 3 4] mul)
773However, if you build a gate that uses `+mul` like so, the sample defaults to 0
774since that is the bunt of `@`:
777> (reel `(list @)`~[1 2 3 4] |=([a=@ b=@] (mul a b)))
781We can fix this with `|:`:
784> (reel `(list @)`~[1 2 3 4] |:([a=1 b=1] (mul a b)))
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).
814The accumulator, which is a noun.
821 |* [a=(list) b=_=>(~ |=([* *] +<+))]
825 $(a t.a, b b(+<+ (b i.a +<+.b)))
831> (roll `(list @)`[1 2 3 4 5 ~] add)
834> (roll `(list @)`[6 3 1 ~] sub)
838> (roll `(list @)`[1 3 6 ~] sub)
842`+mul`'s default sample is 1, so calling `+roll` with `+mul` yields the expected behavior:
848> (roll `(list @)`~[1 2 3 4] mul)
852However, if you build a gate that uses `+mul` like so, the sample defaults to 0
853since that is the bunt of `@`:
856> (roll `(list @)`~[1 2 3 4] |=([a=@ b=@] (mul a b)))
860We can fix this with `|:`:
863> (roll `(list @)`~[1 2 3 4] |:([a=1 b=1] (mul a b)))
873Accepts an atom `a` and `list` `b`, producing the first `a` elements of
874the front of the list.
884A list of the same type as `b`.
893 ?: |(?=(~ b) =(0 a)) ~
894 [i.b $(b t.b, a (dec a))]
900> (scag 2 `(list @)`[1 2 3 4 ~])
905> (scag 10 `(list @)`[1 2 3 4 ~])
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`.)
925`b` is a gate that accepts one argument and produces a flag.
936 |* [a=(list) b=$-(* ?)]
940 ?:((b i.a) [[i.a p.c] q.c] [p.c [i.a q.c]])
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]]]
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
965`b` is a gate that accepts one argument and produces a boolean.
976 |* [a=(list) b=$-(* ?)]
980 ?:((b i.a) [i.a $(a t.a)] $(a t.a))
986> =a |=(a=@ (gth a 1))
987> (skim `(list @)`[0 1 2 3 ~] a)
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
1005`b` is a gate that accepts one argument and produces a flag.
1009A list of the same type as `a`.
1016 |* [a=(list) b=$-(* ?)]
1020 ?:((b i.a) $(a t.a) [i.a $(a t.a)])
1026> =a |=(a=@ (gth a 1))
1027> (skip `(l)`[0 1 2 3 ~]) a)
1037Accepts an atom `a` and list `b`, producing the remaining elements from
1048A list of the same type as `b`.
1065> (slag 2 (limo [1 2 3 4 ~]))
1067> (slag 1 (limo [1 2 3 4 ~]))
1068[i=2 t=[i=3 t=[i=4 t=~]]]
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.
1088Produces an element of `b`, or crashes if no element exists at that index.
1096 |- ^+ ?>(?=(^ b) i.b)
1112> (snag 0 `(list @ud)`~[1 2 3 4])
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`.
1134the list of `a` with the item at index `b` replaced with `c`.
1141 |* [a=(list) b=@ c=*]
1143 (weld (scag b a) [c (slag +(b) a)])
1149> (snap (limo ~[2 3 4]) 1 11)
1159Removes the last element from list `a`.
1184> `tape`(snip "foobar")
1199Accepts a `++list` `a` and a noun `b`, producing the list of `b` appended to `a`.
1209Produces a list of `b` appended to `a`.
1222> `tape`(zing (snoc `(list tape)`~["a" "bc" "def"] "g"))
1224> (snoc `(list @ud)`~[1 2 3] 4)
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`.
1242`b` is a gate that accepts two nouns and produces a boolean.
1253 |* [a=(list) b=$-([* *] ?)]
1257 =+ s=(skid t.a |:(c=i.a (b c i.a)))
1267> (sort `(list @)`[0 1 2 3 ~] gth)
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`.
1293A pair of a list and a noun.
1300 |* [a=(list) b=* c=_|=(^ [** +<+])]
1301 => .(c `$-([_?>(?=(^ a) i.a) _b] [_-:(c) _b])`c)
1302 =/ acc=(list _-:(c)) ~
1303 |- ^- (pair _acc _b)
1307 $(acc [res acc], a t.a)
1313> %^ spin (limo ~[4 5 6]) :: Trivial example -- does nothing with the state
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]
1327> %^ spin (reap 10 0) :: Create 10 random numbers less than `10`
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>}>]
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.
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)
1368 |* [a=(list) b=_|=(^ [** +<+])]
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]]
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]]
1393Similar to `substr` in Javascript: extracts a string infix, beginning at
1394inclusive index `a`, producing `b` number of characters.
1406A list of the same type as `c`.
1412 |* [[a=@ b=@] c=(list)]
1413 (scag +<-> (slag +<-< c))
1419> (swag [2 5] "roly poly")
1424> (swag [2 2] (limo [1 2 3 4 ~]))
1434Accepts a `++list` `a` and a gate `b`. Produces a list with the gate applied
1435to each element of the original list.
1452 |* [a=(list) b=gate]
1454 ^- (list _?>(?=(^ a) (b i.a)))
1457 [i=(b i.a) t=$(a t.a)]
1463> (turn (limo [104 111 111 110 ~]) @t)
1468> =a |=(a=@ (add a 4))
1469> (turn (limo [1 2 3 4 ~]) a)
1475`turn` is Hoon's version of 'map' in Haskell.
1483Concatenate two `++list`s `a` and `b`.
1487`a` and `b` are lists.
1494 |* [a=(list) b=(list)]
1495 => .(a ^.(homo a), b ^.(homo b))
1509> (weld (limo [1 2 ~]) (limo [3 4 ~]))
1519Concatenate two `++list`s `a` and `b` without losing their type information
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 ~]
1563> (welp [sa+1 so+2 ~] si=3)
1564[[%sa 1] [%so 2] si=3]
1571Turns a `++list` of lists into a single list by promoting the elements of
1572each sublist into the higher.
1592 (welp +<- $(+< +<+))
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']
1604> (zing (limo [(limo [1 'a' 2 'b' ~]) (limo [3 'c' 4 'd' ~]) ~]))
1605~[1 97 2 98 3 99 4 100]
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list