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)))
788If you check the definition of `+mul`, you'll see that it also utilizes this
791We can check explicitly what sequence of operations `+reel` performs like this:
793> =f |: [l='e_l' r='e_r']
795 :((cury cat 3) '(' l '*' r ')')
797'(a*(b*(c*(d*(e*e_r)))))'
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).
827The accumulator, which is a noun.
834 |* [a=(list) b=_=>(~ |=([* *] +<+))]
838 $(a t.a, b b(+<+ (b i.a +<+.b)))
844> (roll `(list @)`[1 2 3 4 5 ~] add)
847> (roll `(list @)`[6 3 1 ~] sub)
851> (roll `(list @)`[1 3 6 ~] sub)
855`+mul`'s default sample is 1, so calling `+roll` with `+mul` yields the expected behavior:
861> (roll `(list @)`~[1 2 3 4] mul)
865However, if you build a gate that uses `+mul` like so, the sample defaults to 0
866since that is the bunt of `@`:
869> (roll `(list @)`~[1 2 3 4] |=([a=@ b=@] (mul a b)))
873We can fix this with `|:`:
876> (roll `(list @)`~[1 2 3 4] |:([a=1 b=1] (mul a b)))
879If you check the definition of `+mul`, you'll see that it also utilizes this
882We can check explicitly what sequence of operations `+roll` performs like this:
884> =f |: [l='e_l' r='e_r']
886 :((cury cat 3) '(' l '*' r ')')
888'(e*(d*(c*(b*(a*e_r)))))
890This is in contrast to what one might expect:
893 |* [l=(list) f=$-([* *] *)]
901'(((((e_l*a)*b)*c)*d)*e)'
910Accepts an atom `a` and `list` `b`, producing the first `a` elements of
911the front of the list.
921A list of the same type as `b`.
930 ?: |(?=(~ b) =(0 a)) ~
931 [i.b $(b t.b, a (dec a))]
937> (scag 2 `(list @)`[1 2 3 4 ~])
942> (scag 10 `(list @)`[1 2 3 4 ~])
952Separates a `list` `a` into two lists - Those elements of `a` who produce
953true when slammed to gate `b` and those who produce `%.n`.
955(To "slam" means to call a gate and give it a sample/samples. In this instance,
956`a` is the list of samples that are given to the gate `b`.)
962`b` is a gate that accepts one argument and produces a flag.
973 |* [a=(list) b=$-(* ?)]
977 ?:((b i.a) [[i.a p.c] q.c] [p.c [i.a q.c]])
983> =a |=(a=@ (gth a 1))
984> (skid `(list @)`[0 1 2 3 ~] a)
985[p=[i=2 t=~[3]] q=[i=0 t=~[1]]]
994Cycles through the members of a list `a`, passing them to a gate `b` and
995producing a list of all of the members that produce `%.y`. Inverse of
1002`b` is a gate that accepts one argument and produces a boolean.
1013 |* [a=(list) b=$-(* ?)]
1017 ?:((b i.a) [i.a $(a t.a)] $(a t.a))
1023> =a |=(a=@ (gth a 1))
1024> (skim `(list @)`[0 1 2 3 ~] a)
1034Cycles through the members of `list` `a`, passing them to a gate `b`.
1035Produces a list of all of the members that produce `%.n`. Inverse of
1042`b` is a gate that accepts one argument and produces a flag.
1046A list of the same type as `a`.
1053 |* [a=(list) b=$-(* ?)]
1057 ?:((b i.a) $(a t.a) [i.a $(a t.a)])
1063> =a |=(a=@ (gth a 1))
1064> (skip `(l)`[0 1 2 3 ~]) a)
1074Accepts an atom `a` and list `b`, producing the remaining elements from
1085A list of the same type as `b`.
1102> (slag 2 (limo [1 2 3 4 ~]))
1104> (slag 1 (limo [1 2 3 4 ~]))
1105[i=2 t=[i=3 t=[i=4 t=~]]]
1114Accepts an atom `a` and a `++list` `b`, producing the element at the index
1115of `a`and failing if the list is null. Lists are 0-indexed.
1125Produces an element of `b`, or crashes if no element exists at that index.
1133 |- ^+ ?>(?=(^ b) i.b)
1149> (snag 0 `(list @ud)`~[1 2 3 4])
1157Replace item at index
1159Accepts a `list` `a`, an atom `b`, and a noun `c`, producing the list of `a` with the item at index `b` replaced with `c`.
1171the list of `a` with the item at index `b` replaced with `c`.
1178 |* [a=(list) b=@ c=*]
1180 (weld (scag b a) [c (slag +(b) a)])
1186> (snap (limo ~[2 3 4]) 1 11)
1196Removes the last element from list `a`.
1221> `tape`(snip "foobar")
1236Accepts a `++list` `a` and a noun `b`, producing the list of `b` appended to `a`.
1246Produces a list of `b` appended to `a`.
1259> `tape`(zing (snoc `(list tape)`~["a" "bc" "def"] "g"))
1261> (snoc `(list @ud)`~[1 2 3] 4)
1271Quicksort: accepts a `++list` `a` and a gate `b` which accepts two nouns and
1272produces a flag. `++sort` then produces a list of the elements of `a`,
1273sorted according to `b`.
1279`b` is a gate that accepts two nouns and produces a boolean.
1290 |* [a=(list) b=$-([* *] ?)]
1294 =+ s=(skid t.a |:(c=i.a (b c i.a)))
1304> (sort `(list @)`[0 1 2 3 ~] gth)
1312Gate to list, with state
1314Accepts a `++list` `a`, some state `b`, and a gate `c`. `c` is called with
1315a tuple -- the head is an element of `a` and the tail is the state `b`, and
1316should produce a tuple of the transformed element and the (potentially
1317modified) state `b`. Produces a pair where the first element is a list of the
1318transformed elements of `a`, and the second element is the final value of `b`.
1330A pair of a list and a noun.
1337 |* [a=(list) b=* c=_|=(^ [** +<+])]
1338 => .(c `$-([_?>(?=(^ a) i.a) _b] [_-:(c) _b])`c)
1339 =/ acc=(list _-:(c)) ~
1340 |- ^- (pair _acc _b)
1344 $(acc [res acc], a t.a)
1350> %^ spin (limo ~[4 5 6]) :: Trivial example -- does nothing with the state
1357> %^ spin (limo ~[4 5 6]) :: Form a pair with `p` as the index and `q` as the list element
1359 |=([n=@ a=@] [`(pair)`[a n] +(a)])
1360[p=~[[p=0 q=4] [p=1 q=5] [p=2 q=6]] q=3]
1364> %^ spin (reap 10 0) :: Create 10 random numbers less than `10`
1366 |=([n=@ rng=_og] (rads:rng 10))
1367[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`(~(rads og eny) 2)` creates a random number less than `2`, seeding the RNG
1373with entropy (`eny`). The head of the product is the random number, the tail
1374is the continuation of the RNG.
1380Gate to list, with state
1382Accepts a `list` `a` and a gate `b`. `c` is internal state, initially
1383derived by **bunting** the tail of the sample of gate `b`, instead of
1384being passed in explicitly as in `++spin`. Produces a list with the
1385gate applied to each element of the original list. `b` is called with a tuple --
1386the head is an element of `a` and the tail is the state `c`, and should
1387produce a tuple of the transformed element and the (potentially modified)
1405 |* [a=(list) b=_|=(^ [** +<+])]
1412> %+ spun (limo ~[4 5 6]) :: `p` as the index and `q` as the list element
1413 |=([n=@ a=@] [`(pair)`[a n] +(a)])
1414~[[p=0 q=4] [p=1 q=5] [p=2 q=6]]
1419> %+ spun (limo ~[4 5 6]) :: joins two lists into a list of pairs
1420 |=([n=@ a=@] [`(pair)`[(snag a l) n] +(a)])
1421~[[p=7 q=4] [p=8 q=5] [p=9 q=6]]
1430Similar to `substr` in Javascript: extracts a string infix, beginning at
1431inclusive index `a`, producing `b` number of characters.
1443A list of the same type as `c`.
1449 |* [[a=@ b=@] c=(list)]
1450 (scag +<-> (slag +<-< c))
1456> (swag [2 5] "roly poly")
1461> (swag [2 2] (limo [1 2 3 4 ~]))
1471Accepts a `++list` `a` and a gate `b`. Produces a list with the gate applied
1472to each element of the original list.
1489 |* [a=(list) b=gate]
1491 ^- (list _?>(?=(^ a) (b i.a)))
1494 [i=(b i.a) t=$(a t.a)]
1500> (turn (limo [104 111 111 110 ~]) @t)
1505> =a |=(a=@ (add a 4))
1506> (turn (limo [1 2 3 4 ~]) a)
1512`turn` is Hoon's version of 'map' in Haskell.
1520Concatenate two `++list`s `a` and `b`.
1524`a` and `b` are lists.
1531 |* [a=(list) b=(list)]
1532 => .(a ^.(homo a), b ^.(homo b))
1546> (weld (limo [1 2 ~]) (limo [3 4 ~]))
1556Concatenate two `++list`s `a` and `b` without losing their type information
1591> (welp ~[60 61 62] ~[%a %b %c])
1592[60 61 62 %a %b %c ~]
1594> ? (welp ~[60 61 62] ~[%a %b %c])
1595 {@ud @ud @ud $a $b $c $~}
1596[60 61 62 %a %b %c ~]
1600> (welp [sa+1 so+2 ~] si=3)
1601[[%sa 1] [%so 2] si=3]
1608Turns a `++list` of lists into a single list by promoting the elements of
1609each sublist into the higher.
1629 (welp +<- $(+< +<+))
1636> (zing (limo [(limo ['a' 'b' 'c' ~]) (limo ['e' 'f' 'g' ~]) (limo ['h' 'i' 'j' ~]) ~]))
1637~['a' 'b' 'c' 'e' 'f' 'g' 'h' 'i' 'j']
1641> (zing (limo [(limo [1 'a' 2 'b' ~]) (limo [3 'c' 4 'd' ~]) ~]))
1642~[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