2title = "2b: List Logic"
10Produces the indices of all occurrences of `nedl` in `hstk` as a `list` of
28 |= [nedl=(list) hstk=(list)]
34 ?: |(?=($~ n) ?=($~ h))
38 ^$(i +(i), hstk +.hstk, fnd [i fnd])
40 ^$(i +(i), hstk +.hstk)
46 > (fand ~[3] ~[1 2 3])
49 > (fand ~[4] ~[1 2 3])
52 > (fand ~['a'] "cbabab")
55 > (fand "ba" "cbabab")
65Produces the index of the first occurrence of `nedl` in `hstk` as the `unit`
83 |= [nedl=(list) hstk=(list)]
88 ?: |(?=($~ n) ?=($~ h))
94 ^$(i +(i), hstk +.hstk)
100 > (find [3]~ ~[1 2 3])
103 > (find [4]~ ~[1 2 3])
106 > (find ['c']~ "cbabab")
109 > (find "ab" "cbabab")
112 > (find "bab" "cbabab")
121Produces the `list` `a` in reverse order.
162Produces a `list` composed of each consecutive integer starting from `a` and
163ending with `b`. `a` and `b` are themselves included.
181 ?:(=(a +(b)) ~ [a $(a +(a))])
190 > `(list @t)`(gulf 99 106)
199Produces a `list` whose type is a fork of all the contained types in the
200list `a`. Used when you want to make all the types of the elements of a list the
217 |% +- $ ?:(*? ~ [i=(snag 0 a) t=$])
226 [i=1 t=[i=97 t=[i=2 t=[i=98 t=[i=[~ u=10] t=~]]]]]
229 ~[1 97 2 98 [~ u=10]]
231 > =a (limo [1 2 3 ~])
233 [i=1 t=[i=2 t=[i=3 t=~]]]
243Constructs a new `list`, placing `sep` between every element of `lit`.
259 |* [sep=* lit=(list)]
260 =. sep `_?>(?=(^ lit) i.lit)`sep
262 =| out=(list _?>(?=(^ lit) i.lit))
266 $(out [sep i.lit out], lit t.lit)
275 > (join 0 `(list @)`~[1 2 3])
284Produces the length of any `list` `a` as an atom.
310 > (lent [1 2 3 4 ~]))
313 > (lent [1 'a' 2 'b' (some 10) ~])
323Computes the Boolean logical "and" on the results of gate `b` applied to each individual element in `list` `a`.
340 |* [a=(list) b=$-(* ?)]
350 > =a |=(a=@ (lte a 1))
351 > (levy `(list @)`[0 1 2 1 ~] a)
354 > =a |=(a=@ (lte a 3))
355 > (levy `(list @)`[0 1 2 1 ~] a)
366Computes the Boolean logical "or" on the results of applying `gate` `b` to
367every element of `++list` `a`.
380 |* [a=(list) b=$-(* ?)]
390 > =a |=(a=@ (gte a 1))
391 > (lien `(list @)`[0 1 2 1 ~] a)
394 > =a |=(a=@ (gte a 3))
395 > (lien `(list @)`[0 1 2 1 ~]) a)
405Turns a null-terminated tuple into a `list`.
409`a` is a null-terminated tuple.
421 |% +- $ ?~(a ~ ?:(_? i=-.a t=$ $(a +.a)))
430 [i=1 t=[i=2 t=[i=3 t=~]]]
439Passes each member of `list` `a` to gate `b`, which must produce a
440`unit`. Produces a new list with all the results that do not produce
447`b` is a gate that produces a unit.
456 ++ murn :: maybe transform
458 |* [a=(list) b=$-(* (unit))]
470 > =a |=(a=@ ?.((gte a 2) ~ (some (add a 10))))
471 > (murn `(list @)`[0 1 2 3 ~] a)
481Removes elements from list `c` beginning at inclusive index `a`, removing `b`
497 |* [[a=@ b=@] c=(list)]
498 (weld (scag a c) (slag (add a b) c))
504 > (oust [4 5] "good day, urbit!")
507 > (oust [2 2] `(list @)`[1 2 3 4 ~])
517Replicate: produces a `list` containing `a` copies of `b`.
544 ~[%a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a]
547 ~[~s1 ~s1 ~s1 ~s1 ~s1]
549 > `@dr`(roll (reap 5 ~s1) add)
559Right fold: moves right to left across a `list` `a`, recursively slamming
560a binary gate `b` with an element from `a` and an accumulator, producing
561the final value of the accumulator.
563(To "slam" means to call a gate and give it a sample/samples. In this instance,
564`a` is the list of samples that are given to the gate b.)
574The accumulator, which is a noun.
581 |* [a=(list) b=_|=([* *] +<+)]
591 > =sum =|([p=@ q=@] |.((add p q)))
592 > (reel (limo [1 2 3 4 5 ~]) sum)
595 > =a =|([p=@ q=@] |.((sub p q)))
596 > (reel (limo [6 3 1 ~]) a)
599 > (reel (limo [3 6 1 ~]) a)
610Left fold: moves left to right across a list `a`, recursively slamming a
611binary gate `b` with an element from the `list` and an accumulator,
612producing the final value of the accumulator.
614(To "slam" means to call a gate and give it a sample/samples. In this instance,
615`a` is the list of samples that are given to the gate b.)
625The accumulator, which is a noun.
632 |* [a=(list) b=_|=([* *] +<+)]
636 $(a t.a, b b(+<+ (b i.a +<+.b)))
642 > =sum =|([p=@ q=@] |.((add p q)))
643 > (roll (limo [1 2 3 4 5 ~]) sum)
646 > =a =|([p=@ q=@] |.((sub p q)))
647 > (roll (limo [6 3 1 ~]) a)
651 > (roll (limo [1 3 6 ~]) a)
661Accepts an atom `a` and `list` `b`, producing the first `a` elements of
662the front of the list.
672A list of the same type as `b`.
681 ?: |(?=(~ b) =(0 a)) ~
682 [i.b $(b t.b, a (dec a))]
688 > (scag 2 `(list @)`[1 2 3 4 ~])
691 > (scag 10 `(list @)`[1 2 3 4 ~])
701Separates a `list` `a` into two lists - Those elements of `a` who produce
702true when slammed to gate `b` and those who produce `%.n`.
704(To "slam" means to call a gate and give it a sample/samples. In this instance,
705`a` is the list of samples that are given to the gate `b`.)
711`b` is a gate that accepts one argument and produces a flag.
722 |* [a=(list) b=$-(* ?)]
726 ?:((b i.a) [[i.a p.c] q.c] [p.c [i.a q.c]])
733 > =a |=(a=@ (gth a 1))
734 > (skid `(list @)`[0 1 2 3 ~]) a)
735 (skid `(list @)`[0 1 2 3 ~]) a)
744Cycles through the members of a list `a`, passing them to a gate `b` and
745producing a list of all of the members that produce `%.y`. Inverse of
752`b` is a gate that accepts one argument and produces a boolean.
763 |* [a=(list) b=$-(* ?)]
767 ?:((b i.a) [i.a $(a t.a)] $(a t.a))
773 > =a |=(a=@ (gth a 1))
774 > (skim `(list @)`[0 1 2 3 ~] a)
783Cycles through the members of `list` `a`, passing them to a gate `b`.
784Produces a list of all of the members that produce `%.n`. Inverse of
791`b` is a gate that accepts one argument and produces a flag.
795A list of the same type as `a`.
802 |* [a=(list) b=$-(* ?)]
806 ?:((b i.a) $(a t.a) [i.a $(a t.a)])
812 > =a |=(a=@ (gth a 1))
813 > (skip `(l)`[0 1 2 3 ~]) a)
823Accepts an atom `a` and list `b`, producing the remaining elements from
834A list of the same type as `b`.
851 > (slag 2 (limo [1 2 3 4 ~]))
853 > (slag 1 (limo [1 2 3 4 ~]))
854 [i=2 t=[i=3 t=[i=4 t=~]]]
863Accepts an atom `a` and a `++list` `b`, producing the element at the index
864of `a`and failing if the list is null. Lists are 0-indexed.
874Produces an element of `b`, or crashes if no element exists at that index.
894 > (snag 0 `(list @ud)`~[1 2 3 4])
904Accepts a `++list` `a` and a noun `b`, producing the list of `b` appended to `a`.
914Produces a list of `b` appended to `a`.
927 > `tape`(zing (snoc `(list tape)`~["a" "bc" "def"] "g"))
929 > (snoc `(list @ud)`~[1 2 3] 4)
939Quicksort: accepts a `++list` `a` and a gate `b` which accepts two nouns and
940produces a flag. `++sort` then produces a list of the elements of `a`,
941sorted according to `b`.
947`b` is a gate that accepts two nouns and produces a boolean.
956 ++ sort !. :: quicksort
958 |* [a=(list) b=$-([* *] ?)]
963 $(a (skim t.a |=(c/_i.a (b c i.a))))
965 [i.a $(a (skim t.a |=(c/_i.a !(b c i.a))))]
971 > =a =|([p/@ q/@] |.((gth p q)))
972 > (sort (limo [0 1 2 3 ~]) a)
980Gate to list, with state
982Accepts a `++list` `a`, a gate `b`, and some state `c`. Produces a list with
983the gate applied to each element of the original list. `b` is called with
984a tuple -- the head is an element of `a` and the tail is the state `c`, and
985should produce a tuple of the transformed element and the (potentially
1005 |* [a=(list) b=* c=_|=(^ [** +<+])]
1006 => .(c `$-([_?>(?=(^ a) i.a) _b] [_-:(c) _b])`c)
1007 =/ acc=(list _-:(c)) ~
1008 |- ^- (pair _acc _b)
1012 $(acc [res acc], a t.a)
1018 > %^ spin (limo ~[4 5 6]) :: trivial example -- does nothing with the state
1023 > %^ spin (limo ~[4 5 6]) :: `p` as the index and `q` as the list element
1024 |=([n=@ a=@] [`(pair)`[a n] +(a)])
1026 ~[[p=0 q=4] [p=1 q=5] [p=2 q=6]]
1028 > %^ spin (reap 10 0) :: Create 10 random numbers less than `10`
1029 |=([n=@ rng=_og] (rads:rng 10))
1031 ~[5 5 9 4 1 7 9 9 9 6]
1036`(~(rads og eny) 2)` creates a random number less than `2`, seeding the RNG
1037with entropy (`eny`). The head of the product is the random number, the tail
1038is the continuation of the RNG.
1045Gate to list, with state
1047Accepts a `list` `a` and a gate `b`. `c` is internal state, initially
1048derived by **bunting** the tail of the sample of gate `b`, instead of
1049being passed in explicitly as in `++spin`. Produces a list with the
1050gate applied to each element of the original list. `b` is called with a tuple --
1051the head is an element of `a` and the tail is the state `c`, and should
1052produce a tuple of the transformed element and the (potentially modified)
1070 |* [a=(list) b=_|=(^ [** +<+])]
1077 > %+ spun (limo ~[4 5 6]) :: `p` as the index and `q` as the list element
1078 |=([n=@ a=@] [`(pair)`[a n] +(a)])
1079 ~[[p=0 q=4] [p=1 q=5] [p=2 q=6]]
1081 > =l (limo ~[7 8 9])
1082 > %+ spun (limo ~[4 5 6]) :: joins two lists into a list of pairs
1083 |=([n=@ a=@] [`(pair)`[(snag a l) n] +(a)])
1084 ~[[p=7 q=4] [p=8 q=5] [p=9 q=6]]
1093Similar to `substr` in Javascript: extracts a string infix, beginning at
1094inclusive index `a`, producing `b` number of characters.
1106A list of the same type as `c`.
1112 |* [[a=@ b=@] c=(list)]
1113 (scag +<-> (slag +<-< c))
1119 > (swag [2 5] "roly poly")
1122 > (swag [2 2] (limo [1 2 3 4 ~]))
1132Accepts a `++list` `a` and a gate `b`. Produces a list with the gate applied
1133to each element of the original list.
1150 |* [a=(list) b=gate]
1153 [i=(b i.a) t=$(a t.a)]
1159 > (turn (limo [104 111 111 110 ~]) @t)
1162 > =a |=(a=@ (add a 4))
1163 > (turn (limo [1 2 3 4 ~]) a)
1169`turn` is Hoon's version of 'map' in Haskell.
1176Concatenate two `++list`s `a` and `b`.
1180`a` and `b` are lists.
1187 |* [a=(list) b=(list)]
1188 => .(a ^.(homo a), b ^.(homo b))
1198 ~[~~u ~~r ~~b ~~i ~~t]
1200 > (weld (limo [1 2 ~]) (limo [3 4 ~]))
1210Concatenate two `++list`s `a` and `b` without losing their type information
1239 > (welp "foo" "bar")
1242 > (welp ~[60 61 62] ~[%a %b %c])
1243 [60 61 62 %a %b %c ~]
1245 > ? (welp ~[60 61 62] ~[%a %b %c])
1246 {@ud @ud @ud $a $b $c $~}
1247 [60 61 62 %a %b %c ~]
1249 > (welp [sa+1 so+2 ~] si=3)
1250 [[%sa 1] [%so 2] si=3]
1257Turns a `++list` of lists into a single list by promoting the elements of
1258each sublist into the higher.
1277 (welp +<- $(+< +<+))
1285 > (zing (limo [(limo ['a' 'b' 'c' ~]) (limo ['e' 'f' 'g' ~]) (limo ['h' 'i' 'j' ~]) ~]))
1286 ~['a' 'b' 'c' 'e' 'f' 'g' 'h' 'i' 'j']
1288 > (zing (limo [(limo [1 'a' 2 'b' ~]) (limo [3 'c' 4 'd' ~]) ~]))
1289 ~[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