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
793A list of the same type as `b`.
810 > (slag 2 (limo [1 2 3 4 ~]))
812 > (slag 1 (limo [1 2 3 4 ~]))
813 [i=2 t=[i=3 t=[i=4 t=~]]]
822Accepts an atom `a` and a `++list` `b`, producing the element at the index
823of `a`and failing if the list is null. Lists are 0-indexed.
831Produces an element of `b`, or crashes if no element exists at that index.
851 > (snag 0 `(list @ud)`~[1 2 3 4])
861Quicksort: accepts a `++list` `a` and a gate `b` which accepts two nouns and
862produces a flag. `++sort` then produces a list of the elements of `a`,
863sorted according to `b`.
867`b` is a gate that accepts two nouns and produces a boolean.
876 ++ sort !. :: quicksort
878 |* [a=(list) b=$-([* *] ?)]
883 $(a (skim t.a |=(c/_i.a (b c i.a))))
885 [i.a $(a (skim t.a |=(c/_i.a !(b c i.a))))]
891 > =a =|([p/@ q/@] |.((gth p q)))
892 > (sort (limo [0 1 2 3 ~]) a)
900Gate to list, with state
902Accepts a `++list` `a`, a gate `b`, and some state `c`. Produces a list with
903the gate applied to each element of the original list. `b` is called with
904a tuple -- the head is an element of `a` and the tail is the state `c`, and
905should produce a tuple of the transformed element and the (potentially
925 |* [a=(list) b=* c=_|=(^ [** +<+])]
926 => .(c `$-([_?>(?=(^ a) i.a) _b] [_-:(c) _b])`c)
927 =/ acc=(list _-:(c)) ~
932 $(acc [res acc], a t.a)
938 > %^ spin (limo ~[4 5 6]) :: trivial example -- does nothing with the state
943 > %^ spin (limo ~[4 5 6]) :: `p` as the index and `q` as the list element
944 |=([n=@ a=@] [`(pair)`[a n] +(a)])
946 ~[[p=0 q=4] [p=1 q=5] [p=2 q=6]]
948 > %^ spin (reap 10 0) :: Create 10 random numbers less than `10`
949 |=([n=@ rng=_og] (rads:rng 10))
951 ~[5 5 9 4 1 7 9 9 9 6]
956`(~(rads og eny) 2)` creates a random number less than `2`, seeding the RNG
957with entropy (`eny`). The head of the product is the random number, the tail
958is the continuation of the RNG.
965Gate to list, with state
967Accepts a `list` `a` and a gate `b`. `c` is internal state, initially
968derived by **bunting** the tail of the sample of gate `b`, instead of
969being passed in explicitly as in `++spin`. Produces a list with the
970gate applied to each element of the original list. `b` is called with a tuple --
971the head is an element of `a` and the tail is the state `c`, and should
972produce a tuple of the transformed element and the (potentially modified)
990 |* [a=(list) b=_|=(^ [** +<+])]
997 > %+ spun (limo ~[4 5 6]) :: `p` as the index and `q` as the list element
998 |=([n=@ a=@] [`(pair)`[a n] +(a)])
999 ~[[p=0 q=4] [p=1 q=5] [p=2 q=6]]
1001 > =l (limo ~[7 8 9])
1002 > %+ spun (limo ~[4 5 6]) :: joins two lists into a list of pairs
1003 |=([n=@ a=@] [`(pair)`[(snag a l) n] +(a)])
1004 ~[[p=7 q=4] [p=8 q=5] [p=9 q=6]]
1013Similar to `substr` in Javascript: extracts a string infix, beginning at
1014inclusive index `a`, producing `b` number of characters.
1022A list of the same type as `c`.
1028 |* [[a=@ a=@] c=(list)]
1029 (scag +<-> (slag +<-< c))
1035 > (swag [2 5] "roly poly")
1038 > (swag [2 2] (limo [1 2 3 4 ~]))
1048Accepts a `++list` `a` and a gate `b`. Produces a list with the gate applied
1049to each element of the original list.
1064 |* [a=(list) b=gate]
1067 [i=(b i.a) t=$(a t.a)]
1073 > (turn (limo [104 111 111 110 ~]) @t)
1076 > =a |=(a=@ (add a 4))
1077 > (turn (limo [1 2 3 4 ~]) a)
1083`turn` is Hoon's version of 'map' in Haskell.
1090Concatenate two `++list`s `a` and `b`.
1094`a` and `b` are lists.
1101 |* [a=(list) b=(list)]
1102 => .(a ^.(homo a), b ^.(homo b))
1112 ~[~~u ~~r ~~b ~~i ~~t]
1114 > (weld (limo [1 2 ~]) (limo [3 4 ~]))
1124Concatenate two `++list`s `a` and `b` without losing their type information
1153 > (welp "foo" "bar")
1156 > (welp ~[60 61 62] ~[%a %b %c])
1157 [60 61 62 %a %b %c ~]
1159 > ? (welp ~[60 61 62] ~[%a %b %c])
1160 {@ud @ud @ud $a $b $c $~}
1161 [60 61 62 %a %b %c ~]
1163 > (welp [sa+1 so+2 ~] si=3)
1164 [[%sa 1] [%so 2] si=3]
1173Turns a `++list` of lists into a single list by promoting the elements of
1174each sublist into the higher.
1193 (welp +<- $(+< +<+))
1201 > (zing (limo [(limo ['a' 'b' 'c' ~]) (limo ['e' 'f' 'g' ~]) (limo ['h' 'i' 'j' ~]) ~]))
1202 ~['a' 'b' 'c' 'e' 'f' 'g' 'h' 'i' 'j']
1204 > (zing (limo [(limo [1 'a' 2 'b' ~]) (limo [3 'c' 4 'd' ~]) ~]))
1205 ~[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