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=~]]]
245Produces the length of any `list` `a` as an atom.
271 > (lent [1 2 3 4 ~]))
274 > (lent [1 'a' 2 'b' (some 10) ~])
284Computes the Boolean logical "and" on the results of gate `b` applied to each individual element in `list` `a`.
301 |* [a=(list) b=$-(* ?)]
311 > =a |=(a=@ (lte a 1))
312 > (levy `(list @)`[0 1 2 1 ~] a)
315 > =a |=(a=@ (lte a 3))
316 > (levy `(list @)`[0 1 2 1 ~] a)
327Computes the Boolean logical "or" on the results of applying `gate` `b` to
328every element of `++list` `a`.
341 |* [a=(list) b=$-(* ?)]
351 > =a |=(a=@ (gte a 1))
352 > (lien `(list @)`[0 1 2 1 ~] a)
355 > =a |=(a=@ (gte a 3))
356 > (lien `(list @)`[0 1 2 1 ~]) a)
366Turns a null-terminated tuple into a `list`.
370`a` is a null-terminated tuple.
382 |% +- $ ?~(a ~ ?:(_? i=-.a t=$ $(a +.a)))
391 [i=1 t=[i=2 t=[i=3 t=~]]]
400Passes each member of `list` `a` to gate `b`, which must produce a
401`unit`. Produces a new list with all the results that do not produce
408`b` is a gate that produces a unit.
417 ++ murn :: maybe transform
419 |* [a=(list) b=$-(* (unit))]
431 > =a |=(a=@ ?.((gte a 2) ~ (some (add a 10))))
432 > (murn `(list @)`[0 1 2 3 ~] a)
442Removes elements from list `c` beginning at inclusive index `a`, removing `b`
458 |* [[a=@ b=@] c=(list)]
459 (weld (scag a c) (slag (add a b) c))
465 > (oust [4 5] "good day, urbit!")
468 > (oust [2 2] `(list @)`[1 2 3 4 ~])
478Replicate: produces a `list` containing `a` copies of `b`.
505 ~[%a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a]
508 ~[~s1 ~s1 ~s1 ~s1 ~s1]
510 > `@dr`(roll (reap 5 ~s1) add)
520Right fold: moves right to left across a `list` `a`, recursively slamming
521a binary gate `b` with an element from `a` and an accumulator, producing
522the final value of the accumulator.
524(To "slam" means to call a gate and give it a sample/samples. In this instance,
525`a` is the list of samples that are given to the gate b.)
535The accumulator, which is a noun.
542 |* [a=(list) b=_|=([* *] +<+)]
552 > =sum =|([p=@ q=@] |.((add p q)))
553 > (reel (limo [1 2 3 4 5 ~]) sum)
556 > =a =|([p=@ q=@] |.((sub p q)))
557 > (reel (limo [6 3 1 ~]) a)
560 > (reel (limo [3 6 1 ~]) a)
571Left fold: moves left to right across a list `a`, recursively slamming a
572binary gate `b` with an element from the `list` and an accumulator,
573producing the final value of the accumulator.
575(To "slam" means to call a gate and give it a sample/samples. In this instance,
576`a` is the list of samples that are given to the gate b.)
586The accumulator, which is a noun.
593 |* [a=(list) b=_|=([* *] +<+)]
597 $(a t.a, b b(+<+ (b i.a +<+.b)))
603 > =sum =|([p=@ q=@] |.((add p q)))
604 > (roll (limo [1 2 3 4 5 ~]) sum)
607 > =a =|([p=@ q=@] |.((sub p q)))
608 > (roll (limo [6 3 1 ~]) a)
612 > (roll (limo [1 3 6 ~]) a)
622Accepts an atom `a` and `list` `b`, producing the first `a` elements of
623the front of the list.
633A list of the same type as `b`.
642 ?: |(?=(~ b) =(0 a)) ~
643 [i.b $(b t.b, a (dec a))]
649 > (scag 2 `(list @)`[1 2 3 4 ~])
652 > (scag 10 `(list @)`[1 2 3 4 ~])
662Separates a `list` `a` into two lists - Those elements of `a` who produce
663true when slammed to gate `b` and those who produce `%.n`.
665(To "slam" means to call a gate and give it a sample/samples. In this instance,
666`a` is the list of samples that are given to the gate `b`.)
672`b` is a gate that accepts one argument and produces a flag.
683 |* [a=(list) b=$-(* ?)]
687 ?:((b i.a) [[i.a p.c] q.c] [p.c [i.a q.c]])
694 > =a |=(a=@ (gth a 1))
695 > (skid `(list @)`[0 1 2 3 ~]) a)
696 (skid `(list @)`[0 1 2 3 ~]) a)
705Cycles through the members of a list `a`, passing them to a gate `b` and
706producing a list of all of the members that produce `%.y`. Inverse of
713`b` is a gate that accepts one argument and produces a boolean.
724 |* [a=(list) b=$-(* ?)]
728 ?:((b i.a) [i.a $(a t.a)] $(a t.a))
734 > =a |=(a=@ (gth a 1))
735 > (skim `(list @)`[0 1 2 3 ~] a)
744Cycles through the members of `list` `a`, passing them to a gate `b`.
745Produces a list of all of the members that produce `%.n`. Inverse of
752`b` is a gate that accepts one argument and produces a flag.
756A list of the same type as `a`.
763 |* [a=(list) b=$-(* ?)]
767 ?:((b i.a) $(a t.a) [i.a $(a t.a)])
773 > =a |=(a=@ (gth a 1))
774 > (skip `(l)`[0 1 2 3 ~]) a)
784Accepts an atom `a` and list `b`, producing the remaining elements from
795A list of the same type as `b`.
812 > (slag 2 (limo [1 2 3 4 ~]))
814 > (slag 1 (limo [1 2 3 4 ~]))
815 [i=2 t=[i=3 t=[i=4 t=~]]]
824Accepts an atom `a` and a `++list` `b`, producing the element at the index
825of `a`and failing if the list is null. Lists are 0-indexed.
835Produces an element of `b`, or crashes if no element exists at that index.
855 > (snag 0 `(list @ud)`~[1 2 3 4])
865Accepts a `++list` `a` and a noun `b`, producing the list of `b` appended to `a`.
875Produces a list of `b` appended to `a`.
888 > `tape`(zing (snoc `(list tape)`~["a" "bc" "def"] "g"))
890 > (snoc `(list @ud)`~[1 2 3] 4)
900Quicksort: accepts a `++list` `a` and a gate `b` which accepts two nouns and
901produces a flag. `++sort` then produces a list of the elements of `a`,
902sorted according to `b`.
908`b` is a gate that accepts two nouns and produces a boolean.
917 ++ sort !. :: quicksort
919 |* [a=(list) b=$-([* *] ?)]
924 $(a (skim t.a |=(c/_i.a (b c i.a))))
926 [i.a $(a (skim t.a |=(c/_i.a !(b c i.a))))]
932 > =a =|([p/@ q/@] |.((gth p q)))
933 > (sort (limo [0 1 2 3 ~]) a)
941Gate to list, with state
943Accepts a `++list` `a`, a gate `b`, and some state `c`. Produces a list with
944the gate applied to each element of the original list. `b` is called with
945a tuple -- the head is an element of `a` and the tail is the state `c`, and
946should produce a tuple of the transformed element and the (potentially
966 |* [a=(list) b=* c=_|=(^ [** +<+])]
967 => .(c `$-([_?>(?=(^ a) i.a) _b] [_-:(c) _b])`c)
968 =/ acc=(list _-:(c)) ~
973 $(acc [res acc], a t.a)
979 > %^ spin (limo ~[4 5 6]) :: trivial example -- does nothing with the state
984 > %^ spin (limo ~[4 5 6]) :: `p` as the index and `q` as the list element
985 |=([n=@ a=@] [`(pair)`[a n] +(a)])
987 ~[[p=0 q=4] [p=1 q=5] [p=2 q=6]]
989 > %^ spin (reap 10 0) :: Create 10 random numbers less than `10`
990 |=([n=@ rng=_og] (rads:rng 10))
992 ~[5 5 9 4 1 7 9 9 9 6]
997`(~(rads og eny) 2)` creates a random number less than `2`, seeding the RNG
998with entropy (`eny`). The head of the product is the random number, the tail
999is the continuation of the RNG.
1006Gate to list, with state
1008Accepts a `list` `a` and a gate `b`. `c` is internal state, initially
1009derived by **bunting** the tail of the sample of gate `b`, instead of
1010being passed in explicitly as in `++spin`. Produces a list with the
1011gate applied to each element of the original list. `b` is called with a tuple --
1012the head is an element of `a` and the tail is the state `c`, and should
1013produce a tuple of the transformed element and the (potentially modified)
1031 |* [a=(list) b=_|=(^ [** +<+])]
1038 > %+ spun (limo ~[4 5 6]) :: `p` as the index and `q` as the list element
1039 |=([n=@ a=@] [`(pair)`[a n] +(a)])
1040 ~[[p=0 q=4] [p=1 q=5] [p=2 q=6]]
1042 > =l (limo ~[7 8 9])
1043 > %+ spun (limo ~[4 5 6]) :: joins two lists into a list of pairs
1044 |=([n=@ a=@] [`(pair)`[(snag a l) n] +(a)])
1045 ~[[p=7 q=4] [p=8 q=5] [p=9 q=6]]
1054Similar to `substr` in Javascript: extracts a string infix, beginning at
1055inclusive index `a`, producing `b` number of characters.
1067A list of the same type as `c`.
1073 |* [[a=@ b=@] c=(list)]
1074 (scag +<-> (slag +<-< c))
1080 > (swag [2 5] "roly poly")
1083 > (swag [2 2] (limo [1 2 3 4 ~]))
1093Accepts a `++list` `a` and a gate `b`. Produces a list with the gate applied
1094to each element of the original list.
1111 |* [a=(list) b=gate]
1114 [i=(b i.a) t=$(a t.a)]
1120 > (turn (limo [104 111 111 110 ~]) @t)
1123 > =a |=(a=@ (add a 4))
1124 > (turn (limo [1 2 3 4 ~]) a)
1130`turn` is Hoon's version of 'map' in Haskell.
1137Concatenate two `++list`s `a` and `b`.
1141`a` and `b` are lists.
1148 |* [a=(list) b=(list)]
1149 => .(a ^.(homo a), b ^.(homo b))
1159 ~[~~u ~~r ~~b ~~i ~~t]
1161 > (weld (limo [1 2 ~]) (limo [3 4 ~]))
1171Concatenate two `++list`s `a` and `b` without losing their type information
1200 > (welp "foo" "bar")
1203 > (welp ~[60 61 62] ~[%a %b %c])
1204 [60 61 62 %a %b %c ~]
1206 > ? (welp ~[60 61 62] ~[%a %b %c])
1207 {@ud @ud @ud $a $b $c $~}
1208 [60 61 62 %a %b %c ~]
1210 > (welp [sa+1 so+2 ~] si=3)
1211 [[%sa 1] [%so 2] si=3]
1220Turns a `++list` of lists into a single list by promoting the elements of
1221each sublist into the higher.
1240 (welp +<- $(+< +<+))
1248 > (zing (limo [(limo ['a' 'b' 'c' ~]) (limo ['e' 'f' 'g' ~]) (limo ['h' 'i' 'j' ~]) ~]))
1249 ~['a' 'b' 'c' 'e' 'f' 'g' 'h' 'i' 'j']
1251 > (zing (limo [(limo [1 'a' 2 'b' ~]) (limo [3 'c' 4 'd' ~]) ~]))
1252 ~[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