2title = "2b: List Logic"
11Produces the indices of all occurrences of `nedl` in `hstk` as a `list` of
29 |= [nedl=(list) hstk=(list)]
35 ?: |(?=($~ n) ?=($~ h))
39 ^$(i +(i), hstk +.hstk, fnd [i fnd])
41 ^$(i +(i), hstk +.hstk)
47 > (fand ~[3] ~[1 2 3])
50 > (fand ~[4] ~[1 2 3])
53 > (fand ~['a'] "cbabab")
56 > (fand "ba" "cbabab")
66Produces the index of the first occurrence of `nedl` in `hstk` as the `unit`
84 |= [nedl=(list) hstk=(list)]
89 ?: |(?=($~ n) ?=($~ h))
95 ^$(i +(i), hstk +.hstk)
101 > (find [3]~ ~[1 2 3])
104 > (find [4]~ ~[1 2 3])
107 > (find ['c']~ "cbabab")
110 > (find "ab" "cbabab")
113 > (find "bab" "cbabab")
122Produces the `list` `a` in reverse order.
163Produces a `list` composed of each consecutive integer starting from `a` and
164ending with `b`. `a` and `b` are themselves included.
182 ?:(=(a +(b)) ~ [a $(a +(a))])
191 > `(list @t)`(gulf 99 106)
200Produces a `list` whose type is a fork of all the contained types in the
201list `a`. Used when you want to make all the types of the elements of a list the
218 |% +- $ ?:(*? ~ [i=(snag 0 a) t=$])
227 [i=1 t=[i=97 t=[i=2 t=[i=98 t=[i=[~ u=10] t=~]]]]]
230 ~[1 97 2 98 [~ u=10]]
232 > =a (limo [1 2 3 ~])
234 [i=1 t=[i=2 t=[i=3 t=~]]]
244Constructs a new `list`, placing `sep` between every element of `lit`.
260 |* [sep=* lit=(list)]
261 =. sep `_?>(?=(^ lit) i.lit)`sep
263 =| out=(list _?>(?=(^ lit) i.lit))
267 $(out [sep i.lit out], lit t.lit)
276 > (join 0 `(list @)`~[1 2 3])
285Produces the length of any `list` `a` as an atom.
311 > (lent [1 2 3 4 ~]))
314 > (lent [1 'a' 2 'b' (some 10) ~])
324Computes the Boolean logical "and" on the results of gate `b` applied to each individual element in `list` `a`.
341 |* [a=(list) b=$-(* ?)]
351 > =a |=(a=@ (lte a 1))
352 > (levy `(list @)`[0 1 2 1 ~] a)
355 > =a |=(a=@ (lte a 3))
356 > (levy `(list @)`[0 1 2 1 ~] a)
367Computes the Boolean logical "or" on the results of applying `gate` `b` to
368every element of `++list` `a`.
381 |* [a=(list) b=$-(* ?)]
391 > =a |=(a=@ (gte a 1))
392 > (lien `(list @)`[0 1 2 1 ~] a)
395 > =a |=(a=@ (gte a 3))
396 > (lien `(list @)`[0 1 2 1 ~]) a)
406Turns a null-terminated tuple into a `list`.
410`a` is a null-terminated tuple.
422 |% +- $ ?~(a ~ ?:(_? i=-.a t=$ $(a +.a)))
431 [i=1 t=[i=2 t=[i=3 t=~]]]
440Passes each member of `list` `a` to gate `b`, which must produce a
441`unit`. Produces a new list with all the results that do not produce
448`b` is a gate that produces a unit.
457 ++ murn :: maybe transform
459 |* [a=(list) b=$-(* (unit))]
471 > =a |=(a=@ ?.((gte a 2) ~ (some (add a 10))))
472 > (murn `(list @)`[0 1 2 3 ~] a)
482Removes elements from list `c` beginning at inclusive index `a`, removing `b`
498 |* [[a=@ b=@] c=(list)]
499 (weld (scag a c) (slag (add a b) c))
505 > (oust [4 5] "good day, urbit!")
508 > (oust [2 2] `(list @)`[1 2 3 4 ~])
518Replicate: produces a `list` containing `a` copies of `b`.
545 ~[%a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a]
548 ~[~s1 ~s1 ~s1 ~s1 ~s1]
550 > `@dr`(roll (reap 5 ~s1) add)
560Right fold: moves right to left across a `list` `a`, recursively slamming
561a binary gate `b` with an element from `a` and an accumulator, producing
562the final value of the accumulator.
564(To "slam" means to call a gate and give it a sample/samples. In this instance,
565`a` is the list of samples that are given to the gate b.)
575The accumulator, which is a noun.
582 |* [a=(list) b=_|=([* *] +<+)]
592 > (reel `(list @)`[1 2 3 4 5 ~] add)
595 > (reel `(list @)`[6 3 1 ~] sub)
598 > (reel `(list @)`[3 6 1 ~] sub)
609Left fold: moves left to right across a list `a`, recursively slamming a
610binary gate `b` with an element from the `list` and an accumulator,
611producing the final value of the accumulator.
613(To "slam" means to call a gate and give it a sample/samples. In this instance,
614`a` is the list of samples that are given to the gate b.)
624The accumulator, which is a noun.
631 |* [a=(list) b=_|=([* *] +<+)]
635 $(a t.a, b b(+<+ (b i.a +<+.b)))
641 > (roll `(list @)`[1 2 3 4 5 ~] add)
644 > (roll `(list @)`[6 3 1 ~] sub)
648 > (roll `(list @)`[1 3 6 ~] sub)
658Accepts an atom `a` and `list` `b`, producing the first `a` elements of
659the front of the list.
669A list of the same type as `b`.
678 ?: |(?=(~ b) =(0 a)) ~
679 [i.b $(b t.b, a (dec a))]
685 > (scag 2 `(list @)`[1 2 3 4 ~])
688 > (scag 10 `(list @)`[1 2 3 4 ~])
698Separates a `list` `a` into two lists - Those elements of `a` who produce
699true when slammed to gate `b` and those who produce `%.n`.
701(To "slam" means to call a gate and give it a sample/samples. In this instance,
702`a` is the list of samples that are given to the gate `b`.)
708`b` is a gate that accepts one argument and produces a flag.
719 |* [a=(list) b=$-(* ?)]
723 ?:((b i.a) [[i.a p.c] q.c] [p.c [i.a q.c]])
730 > =a |=(a=@ (gth a 1))
731 > (skid `(list @)`[0 1 2 3 ~] a)
732 [p=[i=2 t=~[3]] q=[i=0 t=~[1]]]
741Cycles through the members of a list `a`, passing them to a gate `b` and
742producing a list of all of the members that produce `%.y`. Inverse of
749`b` is a gate that accepts one argument and produces a boolean.
760 |* [a=(list) b=$-(* ?)]
764 ?:((b i.a) [i.a $(a t.a)] $(a t.a))
770 > =a |=(a=@ (gth a 1))
771 > (skim `(list @)`[0 1 2 3 ~] a)
780Cycles through the members of `list` `a`, passing them to a gate `b`.
781Produces a list of all of the members that produce `%.n`. Inverse of
788`b` is a gate that accepts one argument and produces a flag.
792A list of the same type as `a`.
799 |* [a=(list) b=$-(* ?)]
803 ?:((b i.a) $(a t.a) [i.a $(a t.a)])
809 > =a |=(a=@ (gth a 1))
810 > (skip `(l)`[0 1 2 3 ~]) a)
820Accepts an atom `a` and list `b`, producing the remaining elements from
831A list of the same type as `b`.
848 > (slag 2 (limo [1 2 3 4 ~]))
850 > (slag 1 (limo [1 2 3 4 ~]))
851 [i=2 t=[i=3 t=[i=4 t=~]]]
860Accepts an atom `a` and a `++list` `b`, producing the element at the index
861of `a`and failing if the list is null. Lists are 0-indexed.
871Produces an element of `b`, or crashes if no element exists at that index.
891 > (snag 0 `(list @ud)`~[1 2 3 4])
901Accepts a `++list` `a` and a noun `b`, producing the list of `b` appended to `a`.
911Produces a list of `b` appended to `a`.
924 > `tape`(zing (snoc `(list tape)`~["a" "bc" "def"] "g"))
926 > (snoc `(list @ud)`~[1 2 3] 4)
935Accepts a `list` `a`, an atom `b`, and a noun `c`, producing the list of `a` with the item at index `b` replaced with `c`.
948the list of `a` with the item at index `b` replaced with `c`.
954 |* [a/(list) b/@ c/*]
956 (weld (scag b a) [c (slag +(b) a)])
962 > (snap (limo ~[2 3 4]) 1 11)
971Accepts a `list` `a`, an atom `b`, and a noun `c`, producing the list of `a` with the item `c` inserted at index `b`.
984the list of `a` with the item `c` inserted at index `b`.
990 |* [a/(list) b/@ c/*]
992 (weld (scag b a) [c (slag b a)])
998 > (into (limo ~[2 3 4]) 1 11)
1007Quicksort: accepts a `++list` `a` and a gate `b` which accepts two nouns and
1008produces a flag. `++sort` then produces a list of the elements of `a`,
1009sorted according to `b`.
1015`b` is a gate that accepts two nouns and produces a boolean.
1024 ++ sort !. :: quicksort
1026 |* [a=(list) b=$-([* *] ?)]
1031 $(a (skim t.a |=(c/_i.a (b c i.a))))
1033 [i.a $(a (skim t.a |=(c/_i.a !(b c i.a))))]
1039 > (sort `(list @)`[0 1 2 3 ~] gth)
1047Gate to list, with state
1049Accepts a `++list` `a`, some state `b`, and a gate `c`. `c` is called with
1050a tuple -- the head is an element of `a` and the tail is the state `b`, and
1051should produce a tuple of the transformed element and the (potentially
1052modified) state `b`. Produces a pair where the first element is a list of the
1053transformed elements of `a`, and the second element is the final value of `b`.
1065A pair of a list and a noun.
1072 |* [a=(list) b=* c=_|=(^ [** +<+])]
1073 => .(c `$-([_?>(?=(^ a) i.a) _b] [_-:(c) _b])`c)
1074 =/ acc=(list _-:(c)) ~
1075 |- ^- (pair _acc _b)
1079 $(acc [res acc], a t.a)
1085 > %^ spin (limo ~[4 5 6]) :: Trivial example -- does nothing with the state
1090 > %^ spin (limo ~[4 5 6]) :: Form a pair with `p` as the index and `q` as the list element
1092 |=([n=@ a=@] [`(pair)`[a n] +(a)])
1093 [p=~[[p=0 q=4] [p=1 q=5] [p=2 q=6]] q=3]
1095 > %^ spin (reap 10 0) :: Create 10 random numbers less than `10`
1097 |=([n=@ rng=_og] (rads:rng 10))
1098 [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>}>]
1103`(~(rads og eny) 2)` creates a random number less than `2`, seeding the RNG
1104with entropy (`eny`). The head of the product is the random number, the tail
1105is the continuation of the RNG.
1112Gate to list, with state
1114Accepts a `list` `a` and a gate `b`. `c` is internal state, initially
1115derived by **bunting** the tail of the sample of gate `b`, instead of
1116being passed in explicitly as in `++spin`. Produces a list with the
1117gate applied to each element of the original list. `b` is called with a tuple --
1118the head is an element of `a` and the tail is the state `c`, and should
1119produce a tuple of the transformed element and the (potentially modified)
1137 |* [a=(list) b=_|=(^ [** +<+])]
1144 > %+ spun (limo ~[4 5 6]) :: `p` as the index and `q` as the list element
1145 |=([n=@ a=@] [`(pair)`[a n] +(a)])
1146 ~[[p=0 q=4] [p=1 q=5] [p=2 q=6]]
1148 > =l (limo ~[7 8 9])
1149 > %+ spun (limo ~[4 5 6]) :: joins two lists into a list of pairs
1150 |=([n=@ a=@] [`(pair)`[(snag a l) n] +(a)])
1151 ~[[p=7 q=4] [p=8 q=5] [p=9 q=6]]
1160Similar to `substr` in Javascript: extracts a string infix, beginning at
1161inclusive index `a`, producing `b` number of characters.
1173A list of the same type as `c`.
1179 |* [[a=@ b=@] c=(list)]
1180 (scag +<-> (slag +<-< c))
1186 > (swag [2 5] "roly poly")
1189 > (swag [2 2] (limo [1 2 3 4 ~]))
1199Accepts a `++list` `a` and a gate `b`. Produces a list with the gate applied
1200to each element of the original list.
1217 |* [a=(list) b=gate]
1220 [i=(b i.a) t=$(a t.a)]
1226 > (turn (limo [104 111 111 110 ~]) @t)
1229 > =a |=(a=@ (add a 4))
1230 > (turn (limo [1 2 3 4 ~]) a)
1236`turn` is Hoon's version of 'map' in Haskell.
1243Concatenate two `++list`s `a` and `b`.
1247`a` and `b` are lists.
1254 |* [a=(list) b=(list)]
1255 => .(a ^.(homo a), b ^.(homo b))
1267 > (weld (limo [1 2 ~]) (limo [3 4 ~]))
1277Concatenate two `++list`s `a` and `b` without losing their type information
1306 > (welp "foo" "bar")
1309 > (welp ~[60 61 62] ~[%a %b %c])
1310 [60 61 62 %a %b %c ~]
1312 > ? (welp ~[60 61 62] ~[%a %b %c])
1313 {@ud @ud @ud $a $b $c $~}
1314 [60 61 62 %a %b %c ~]
1316 > (welp [sa+1 so+2 ~] si=3)
1317 [[%sa 1] [%so 2] si=3]
1324Turns a `++list` of lists into a single list by promoting the elements of
1325each sublist into the higher.
1344 (welp +<- $(+< +<+))
1352 > (zing (limo [(limo ['a' 'b' 'c' ~]) (limo ['e' 'f' 'g' ~]) (limo ['h' 'i' 'j' ~]) ~]))
1353 ~['a' 'b' 'c' 'e' 'f' 'g' 'h' 'i' 'j']
1355 > (zing (limo [(limo [1 'a' 2 'b' ~]) (limo [3 'c' 4 'd' ~]) ~]))
1356 ~[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