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.)
567The initial value of the accumulator is the bunt of `b`'s second argument (`+<+`). This can occasionally produce
568undesired behavior (see examples). If you need more control over the initial
569value, try making use of `$_` and `|:`, or perhaps [`+spin`](#spin) or
580The accumulator, which is a noun.
587 |* [a=(list) b=_|=([* *] +<+)]
597 > (reel `(list @)`[1 2 3 4 5 ~] add)
600 > (reel `(list @)`[6 3 1 ~] sub)
603 > (reel `(list @)`[3 6 1 ~] sub)
608`+mul`'s default sample is 1, so calling `+reel` with `+mul` yields the expected behavior:
613> (reel `(list @)`~[1 2 3 4] mul)
616However, if you build a gate that uses `+mul` like so, the sample defaults to 0
617since that is the bunt of `@`:
619> (reel `(list @)`~[1 2 3 4] |=([a=@ b=@] (mul a b)))
622We can fix this with `|:`:
624> (reel `(list @)`~[1 2 3 4] |:([a=1 b=1] (mul a b)))
635Left fold: moves left to right across a list `a`, recursively slamming a
636binary gate `b` with an element from the `list` and an accumulator,
637producing the final value of the accumulator.
639(To "slam" means to call a gate and give it a sample/samples. In this instance,
640`a` is the list of samples that are given to the gate b.)
642The initial value of the accumulator is `b`'s second argument (`+<+`). This can
643occasionally produce undesired behavior (see examples). If you need more control
644over the initial value, try making use of `$_` and `|:`, or perhaps
645[`+spin`](#spin) or [`+spun`](#spun).
655The accumulator, which is a noun.
662 |* [a=(list) b=_|=([* *] +<+)]
666 $(a t.a, b b(+<+ (b i.a +<+.b)))
672> (roll `(list @)`[1 2 3 4 5 ~] add)
675> (roll `(list @)`[6 3 1 ~] sub)
679> (roll `(list @)`[1 3 6 ~] sub)
683`+mul`'s default sample is 1, so calling `+roll` with `+mul` yields the expected behavior:
688> (roll `(list @)`~[1 2 3 4] mul)
691However, if you build a gate that uses `+mul` like so, the sample defaults to 0
692since that is the bunt of `@`:
694> (roll `(list @)`~[1 2 3 4] |=([a=@ b=@] (mul a b)))
697We can fix this with `|:`:
699> (roll `(list @)`~[1 2 3 4] |:([a=1 b=1] (mul a b)))
709Accepts an atom `a` and `list` `b`, producing the first `a` elements of
710the front of the list.
720A list of the same type as `b`.
729 ?: |(?=(~ b) =(0 a)) ~
730 [i.b $(b t.b, a (dec a))]
736 > (scag 2 `(list @)`[1 2 3 4 ~])
739 > (scag 10 `(list @)`[1 2 3 4 ~])
749Separates a `list` `a` into two lists - Those elements of `a` who produce
750true when slammed to gate `b` and those who produce `%.n`.
752(To "slam" means to call a gate and give it a sample/samples. In this instance,
753`a` is the list of samples that are given to the gate `b`.)
759`b` is a gate that accepts one argument and produces a flag.
770 |* [a=(list) b=$-(* ?)]
774 ?:((b i.a) [[i.a p.c] q.c] [p.c [i.a q.c]])
781 > =a |=(a=@ (gth a 1))
782 > (skid `(list @)`[0 1 2 3 ~] a)
783 [p=[i=2 t=~[3]] q=[i=0 t=~[1]]]
792Cycles through the members of a list `a`, passing them to a gate `b` and
793producing a list of all of the members that produce `%.y`. Inverse of
800`b` is a gate that accepts one argument and produces a boolean.
811 |* [a=(list) b=$-(* ?)]
815 ?:((b i.a) [i.a $(a t.a)] $(a t.a))
821 > =a |=(a=@ (gth a 1))
822 > (skim `(list @)`[0 1 2 3 ~] a)
831Cycles through the members of `list` `a`, passing them to a gate `b`.
832Produces a list of all of the members that produce `%.n`. Inverse of
839`b` is a gate that accepts one argument and produces a flag.
843A list of the same type as `a`.
850 |* [a=(list) b=$-(* ?)]
854 ?:((b i.a) $(a t.a) [i.a $(a t.a)])
860 > =a |=(a=@ (gth a 1))
861 > (skip `(l)`[0 1 2 3 ~]) a)
871Accepts an atom `a` and list `b`, producing the remaining elements from
882A list of the same type as `b`.
899 > (slag 2 (limo [1 2 3 4 ~]))
901 > (slag 1 (limo [1 2 3 4 ~]))
902 [i=2 t=[i=3 t=[i=4 t=~]]]
911Accepts an atom `a` and a `++list` `b`, producing the element at the index
912of `a`and failing if the list is null. Lists are 0-indexed.
922Produces an element of `b`, or crashes if no element exists at that index.
942 > (snag 0 `(list @ud)`~[1 2 3 4])
952Accepts a `++list` `a` and a noun `b`, producing the list of `b` appended to `a`.
962Produces a list of `b` appended to `a`.
975 > `tape`(zing (snoc `(list tape)`~["a" "bc" "def"] "g"))
977 > (snoc `(list @ud)`~[1 2 3] 4)
986Accepts a `list` `a`, an atom `b`, and a noun `c`, producing the list of `a` with the item at index `b` replaced with `c`.
999the list of `a` with the item at index `b` replaced with `c`.
1005 |* [a/(list) b/@ c/*]
1007 (weld (scag b a) [c (slag +(b) a)])
1013 > (snap (limo ~[2 3 4]) 1 11)
1022Accepts a `list` `a`, an atom `b`, and a noun `c`, producing the list of `a` with the item `c` inserted at index `b`.
1035the list of `a` with the item `c` inserted at index `b`.
1041 |* [a/(list) b/@ c/*]
1043 (weld (scag b a) [c (slag b a)])
1049 > (into (limo ~[2 3 4]) 1 11)
1058Quicksort: accepts a `++list` `a` and a gate `b` which accepts two nouns and
1059produces a flag. `++sort` then produces a list of the elements of `a`,
1060sorted according to `b`.
1066`b` is a gate that accepts two nouns and produces a boolean.
1075 ++ sort !. :: quicksort
1077 |* [a=(list) b=$-([* *] ?)]
1082 $(a (skim t.a |=(c/_i.a (b c i.a))))
1084 [i.a $(a (skim t.a |=(c/_i.a !(b c i.a))))]
1090 > (sort `(list @)`[0 1 2 3 ~] gth)
1098Gate to list, with state
1100Accepts a `++list` `a`, some state `b`, and a gate `c`. `c` is called with
1101a tuple -- the head is an element of `a` and the tail is the state `b`, and
1102should produce a tuple of the transformed element and the (potentially
1103modified) state `b`. Produces a pair where the first element is a list of the
1104transformed elements of `a`, and the second element is the final value of `b`.
1116A pair of a list and a noun.
1123 |* [a=(list) b=* c=_|=(^ [** +<+])]
1124 => .(c `$-([_?>(?=(^ a) i.a) _b] [_-:(c) _b])`c)
1125 =/ acc=(list _-:(c)) ~
1126 |- ^- (pair _acc _b)
1130 $(acc [res acc], a t.a)
1136 > %^ spin (limo ~[4 5 6]) :: Trivial example -- does nothing with the state
1141 > %^ spin (limo ~[4 5 6]) :: Form a pair with `p` as the index and `q` as the list element
1143 |=([n=@ a=@] [`(pair)`[a n] +(a)])
1144 [p=~[[p=0 q=4] [p=1 q=5] [p=2 q=6]] q=3]
1146 > %^ spin (reap 10 0) :: Create 10 random numbers less than `10`
1148 |=([n=@ rng=_og] (rads:rng 10))
1149 [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>}>]
1154`(~(rads og eny) 2)` creates a random number less than `2`, seeding the RNG
1155with entropy (`eny`). The head of the product is the random number, the tail
1156is the continuation of the RNG.
1163Gate to list, with state
1165Accepts a `list` `a` and a gate `b`. `c` is internal state, initially
1166derived by **bunting** the tail of the sample of gate `b`, instead of
1167being passed in explicitly as in `++spin`. Produces a list with the
1168gate applied to each element of the original list. `b` is called with a tuple --
1169the head is an element of `a` and the tail is the state `c`, and should
1170produce a tuple of the transformed element and the (potentially modified)
1188 |* [a=(list) b=_|=(^ [** +<+])]
1195 > %+ spun (limo ~[4 5 6]) :: `p` as the index and `q` as the list element
1196 |=([n=@ a=@] [`(pair)`[a n] +(a)])
1197 ~[[p=0 q=4] [p=1 q=5] [p=2 q=6]]
1199 > =l (limo ~[7 8 9])
1200 > %+ spun (limo ~[4 5 6]) :: joins two lists into a list of pairs
1201 |=([n=@ a=@] [`(pair)`[(snag a l) n] +(a)])
1202 ~[[p=7 q=4] [p=8 q=5] [p=9 q=6]]
1211Similar to `substr` in Javascript: extracts a string infix, beginning at
1212inclusive index `a`, producing `b` number of characters.
1224A list of the same type as `c`.
1230 |* [[a=@ b=@] c=(list)]
1231 (scag +<-> (slag +<-< c))
1237 > (swag [2 5] "roly poly")
1240 > (swag [2 2] (limo [1 2 3 4 ~]))
1250Accepts a `++list` `a` and a gate `b`. Produces a list with the gate applied
1251to each element of the original list.
1268 |* [a=(list) b=gate]
1271 [i=(b i.a) t=$(a t.a)]
1277 > (turn (limo [104 111 111 110 ~]) @t)
1280 > =a |=(a=@ (add a 4))
1281 > (turn (limo [1 2 3 4 ~]) a)
1287`turn` is Hoon's version of 'map' in Haskell.
1294Concatenate two `++list`s `a` and `b`.
1298`a` and `b` are lists.
1305 |* [a=(list) b=(list)]
1306 => .(a ^.(homo a), b ^.(homo b))
1318 > (weld (limo [1 2 ~]) (limo [3 4 ~]))
1328Concatenate two `++list`s `a` and `b` without losing their type information
1344++ welp :: faceless weld
1358 > (welp "foo" "bar")
1361 > (welp ~[60 61 62] ~[%a %b %c])
1362 [60 61 62 %a %b %c ~]
1364 > ? (welp ~[60 61 62] ~[%a %b %c])
1365 {@ud @ud @ud $a $b $c $~}
1366 [60 61 62 %a %b %c ~]
1368 > (welp [sa+1 so+2 ~] si=3)
1369 [[%sa 1] [%so 2] si=3]
1376Turns a `++list` of lists into a single list by promoting the elements of
1377each sublist into the higher.
1397 (welp +<- $(+< +<+))
1405 > (zing (limo [(limo ['a' 'b' 'c' ~]) (limo ['e' 'f' 'g' ~]) (limo ['h' 'i' 'j' ~]) ~]))
1406 ~['a' 'b' 'c' 'e' 'f' 'g' 'h' 'i' 'j']
1408 > (zing (limo [(limo [1 'a' 2 'b' ~]) (limo [3 'c' 4 'd' ~]) ~]))
1409 ~[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