on 8564483 help sign in
900 lines 13 kB history raw
1---
2navhome: /docs
3---
6# 2b: list logic
7Reverse
9### `++flop`
11Produces the list `a` in reverse order.
13Accepts
14-------
16`a` is a list.
18Produces
19--------
21A list.
23Source
24------
26 ++ flop :: reverse
27 ~/ %flop
28 |* a/(list)
29 => .(a (homo a))
30 ^+ a
31 =+ b=`_a`~
32 |-
33 ?~ a b
34 $(a t.a, b [i.a b])
38Examples
39--------
41 > =a (limo [1 2 3 ~])
42 > (flop a)
43 ~[3 2 1]
47***
48### `++homo`
50Homogenize
52Produces a `++list` whose type is a fork of all the contained types in the
53list `a`. Used when you want to make all the types of the elements of a list the same.
55Accepts
56-------
58`a` is a list.
60Produces
61--------
63a list.
65Source
66------
68 ++ homo :: homogenize
69 |* a/(list)
70 ^+ =< $
71 |% +- $ ?:(*? ~ [i=(snag 0 a) t=$])
72 --
73 a
76Examples
77--------
79 > lyst
80 [i=1 t=[i=97 t=[i=2 t=[i=98 t=[i=[~ u=10] t=~]]]]]
81 > (homo lyst)
82 ~[1 97 2 98 [~ u=10]]
83 > =a (limo [1 2 3 ~])
84 > a
85 [i=1 t=[i=2 t=[i=3 t=~]]]
86 > (homo a)
87 ~[1 2 3]
91***
92### `++lent`
94List length
96Produces the length of any `++list` `a` as an atom.
98Accepts
99-------
101`a` is a list.
103Produces
104--------
106an atom.
108Source
109------
111 ++ lent :: length
112 ~/ %lent
113 |= a/(list)
114 ^- @
115 =+ b=0
116 |-
117 ?~ a b
118 $(a t.a, b +(b))
121Examples
122--------
124 > (lent (limo [1 2 3 4 ~]))
125 4
126 > (lent (limo [1 'a' 2 'b' (some 10) ~]))
127 5
131***
132### `++levy`
134Logical "and" on list
136Computes the Boolean logical "and" on the results of gate `b` applied to each individual element in `++list` `a`.
138Accepts
139-------
141`a` is a list.
143`b` is a gate.
145Produces
146--------
148A boolean.
150Source
151------
153 ++ levy
154 ~/ %levy :: all of
155 |* {a/(list) b/$-(* ?)}
156 |- ^- ?
157 ?~ a &
158 ?. (b i.a) |
159 $(a t.a)
162Examples
163--------
165 > =a |=(a/@ (lte a 1))
166 > (levy (limo [0 1 2 1 ~]) a)
167 %.n
168 > =a |=(a/@ (lte a 3))
169 > (levy (limo [0 1 2 1 ~]) a)
170 %.y
174***
175### `++lien`
177Logical "or" on list
179Computes the Boolean logical "or" on the results of applying [gate]() `b` to every element of [`++list`]() `a`.
181Accepts
182-------
184`a` is a list.
186`b` is a gate.
188Source
189------
191 ++ lien :: some of
192 ~/ %lien
193 |* {a/(list) b/$-(* ?)}
194 |- ^- ?
195 ?~ a |
196 ?: (b i.a) &
197 $(a t.a)
199Examples
200--------
202 > =a |=(a=@ (gte a 1))
203 > (lien (limo [0 1 2 1 ~]) a)
204 %.y
205 > =a |=(a=@ (gte a 3))
206 > (lien (limo [0 1 2 1 ~]) a)
207 %.n
211***
212### `++limo`
214List Constructor
216Turns a null-terminated tuple into a [`++list`]().
218Accepts
219-------
221`a` is a null-terminated tuple.
223Produces
224--------
226A `++list`.
228Source
229------
231 ++ limo :: listify
232 |* a=*
233 ^+ =< $
234 |% +- $ ?~(a ~ ?:(_? i=-.a t=$ $(a +.a)))
235 --
236 a
238Examples
239--------
241 > (limo [1 2 3 ~])
242 [i=1 t=[i=2 t=[i=3 t=~]]]
246***
247### `++murn`
249Maybe transform
251Passes each member of `++list` `a` to gate `b`, which must produce a
252`++unit`. Produces a new list with all the results that do not produce
253`~`.
255Accepts
256-------
258`a` is a list.
260`b` is a gate that produces a unit.
262Produces
263--------
265A unit.
267Source
268------
270 ++ murn :: maybe transform
271 ~/ %murn
272 |* {a/(list) b/$-(* (unit))}
273 |-
274 ?~ a ~
275 =+ c=(b i.a)
276 ?~ c
277 $(a t.a)
278 [i=u.c t=$(a t.a)]
280Examples
281--------
283 > =a |=(a=@ ?.((gte a 2) ~ (some (add a 10))))
284 > (murn (limo [0 1 2 3 ~]) a)
285 [i=12 t=[i=13 t=~]]
289***
290### `++reap`
292Replicate
294Replicate: produces a `++list` containing `a` copies of `b`.
296Accepts
297-------
299`b` is a noun.
301Produces
302--------
304A list.
306Source
307------
309 ++ reap :: replicate
310 ~/ %reap
311 |* {a/@ b/*}
312 |- ^- (list _b)
313 ?~ a ~
314 [b $(a (dec a))]
316Examples
317--------
319 > (reap 20 %a)
320 ~[%a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a]
321 > (reap 5 ~s1)
322 ~[~s1 ~s1 ~s1 ~s1 ~s1]
323 > `@dr`(roll (reap 5 ~s1) add)
324 ~s5
328***
329### `++reel`
331Right fold
333Right fold: moves right to left across a `++list` `a`, recursively slamming
334a binary gate `b` with an element from `a` and an accumulator, producing
335the final value of the accumulator.
337Accepts
338-------
340`b` is a binary gate.
342Produces
343--------
345The accumulator, which is a noun.
347Source
348------
350 ++ reel :: right fold
351 ~/ %reel
352 |* {a/(list) b/_|=({* *} +<+)}
353 |- ^+ +<+.b
354 ?~ a
355 +<+.b
356 (b i.a $(a t.a))
359Examples
360--------
362 > =sum =|([p=@ q=@] |.((add p q)))
363 > (reel (limo [1 2 3 4 5 ~]) sum)
364 15
365 > =a =|([p=@ q=@] |.((sub p q)))
366 > (reel (limo [6 3 1 ~]) a)
367 4
368 > (reel (limo [3 6 1 ~]) a)
369 ! subtract-underflow
370 ! exit
374***
375### `++roll`
377Left fold
379Left fold: moves left to right across a list `a`, recursively slamming a
380binary [gate]() `b` with an element from the [`++list`]() and an accumulator,
381producing the final value of the accumulator.
383Accepts
384-------
386`b` is a binary [gate]().
388Produces
389--------
391The accumulator, which is a [noun]().
393Source
394------
396 ++ roll :: left fold
397 ~/ %roll
398 |* [a=(list) b=_|=([* *] +<+)]
399 |- ^+ +<+.b
400 ?~ a
401 +<+.b
402 $(a t.a, b b(+<+ (b i.a +<+.b)))
404Examples
405--------
407 > =sum =|([p=@ q=@] |.((add p q)))
408 > (roll (limo [1 2 3 4 5 ~]) sum)
409 q=15
410 > =a =|([p=@ q=@] |.((sub p q)))
411 > (roll (limo [6 3 1 ~]) a)
412 ! subtract-underflow
413 ! exit
414 > (roll (limo [1 3 6 ~]) a)
415 q=4
419***
420### `++scag`
422Prefix
424Accepts an atom `a` and [list]() `b`, producing the first `a` elements of
425the front of the list.
427Accepts
428-------
430`b` is a list.
432Produces
433--------
435A list of the same type as `b`.
437Source
438------
440 ++ scag :: prefix
441 ~/ %scag
442 |* [a=@ b=(list)]
443 |- ^+ b
444 ?: |(?=(~ b) =(0 a)) ~
445 [i.b $(b t.b, a (dec a))]
447Examples
448--------
450 > (scag 2 (limo [0 1 2 3 ~]))
451 [i=0 t=[i=1 t=~]]
452 > (scag 10 (limo [1 2 3 4 ~]))
453 [i=1 t=[i=2 t=[i=3 t=[i=4 t=~]]]]
457***
458### `++skid`
460Separate
462Separates a `++list` `a` into two lists - Those elements of `a` who produce
463true when slammed to gate `b` and those who produce `%.n`.
465Accepts
466-------
468`b` is a gate that accepts one argument and produces a loobean.
470Produces
471--------
473A cell of two lists.
475Source
476------
478 ++ skid :: separate
479 ~/ %skid
480 |* {a/(list) b/$-(* ?)}
481 |- ^+ [p=a q=a]
482 ?~ a [~ ~]
483 =+ c=$(a t.a)
484 ?:((b i.a) [[i.a p.c] q.c] [p.c [i.a q.c]])
488Examples
489--------
491 > =a |=(a/@ (gth a 1))
492 > (skid (limo [0 1 2 3 ~]) a)
493 [p=[i=2 t=[i=3 t=~]] q=[i=0 t=[i=1 t=~]]]
497***
498### `++skim`
500Suffix
502Cycles through the members of a list `a`, passing them to a gate `b` and
503producing a list of all of the members that produce `%.y`. Inverse of
504`++skip`.
506Accepts
507-------
509`b` is a gate that accepts one argument and produces a boolean.
511Produces
512--------
514A boolean
516Source
517------
519 ++ skim :: only
520 ~/ %skim
521 |* {a/(list) b/$-(* ?)}
522 |-
523 ^+ a
524 ?~ a ~
525 ?:((b i.a) [i.a $(a t.a)] $(a t.a))
528Examples
529--------
531 > =a |=(a/@ (gth a 1))
532 > (skim (limo [0 1 2 3 ~]) a)
533 [i=2 t=[i=3 t=~]]
537***
538### `++skip`
540Except
542Cycles through the members of `++list` `a`, passing them to a gate `b`.
543Produces a list of all of the members that produce `%.n`. Inverse of
544`++skim`.
546Accepts
547-------
549`b` is a gate that accepts one argument and produces a loobean.
551Produces
552--------
554A list of the same type as `a`.
556Source
557------
559 ++ skip :: except
560 ~/ %skip
561 |* {a/(list) b/$-(* ?)}
562 |-
563 ^+ a
564 ?~ a ~
565 ?:((b i.a) $(a t.a) [i.a $(a t.a)])
568Examples
569--------
571 > =a |=(a/@ (gth a 1))
572 > (skip (limo [0 1 2 3 ~]) a)
573 [i=0 t=[i=1 t=~]]
577***
578### `++slag`
580Suffix
582Accepts an atom `a` and list `b`, producing the remaining elements from
583`b` starting at `a`.
585Accepts
586-------
588`b` is a list.
590Produces
591--------
593A list of the same type as `b`.
595Source
596------
598 ++ slag :: suffix
599 ~/ %slag
600 |* {a/@ b/(list)}
601 |- ^+ b
602 ?: =(0 a) b
603 ?~ b ~
604 $(b t.b, a (dec a))
607Examples
608--------
610 > (slag 2 (limo [1 2 3 4 ~]))
611 [i=3 t=[i=4 t=~]]
612 > (slag 1 (limo [1 2 3 4 ~]))
613 [i=2 t=[i=3 t=[i=4 t=~]]]
617***
618### `++snag`
620Index
622Accepts an atom `a` and a `++list` `b`, producing the element at the index
623of `a`and failing if the list is null. Lists are 0-indexed.
625Accepts
626-------
628`b` is a list.
630Produces
631--------
633Produces an element of `b`, or crashes if no element exists at that index.
635Source
636------
638 ++ snag :: index
639 ~/ %snag
640 |* {a/@ b/(list)}
641 |-
642 ?~ b
643 ~|('snag-fail' !!)
644 ?: =(0 a) i.b
645 $(b t.b, a (dec a))
648Examples
649--------
651 > (snag 2 "asdf")
652 ~~d
653 > (snag 0 `(list ,@ud)`~[1 2 3 4])
654 1
658***
659### `++sort`
661Quicksort
663Quicksort: accepts a `++list` `a` and a gate `b` which accepts two nouns and
664produces a loobean. `++sort` then produces a list of the elements of `a`,
665sorted according to `b`.
667Accepts
668-------
670`b` is a gate that ccepts two nouns and produces a boolean.
672Produces
673--------
675A list
677Source
678------
680 ++ sort !. :: quicksort
681 ~/ %sort
682 |* {a/(list) b/$-([* *] ?)}
683 => .(a ^.(homo a))
684 |- ^+ a
685 ?~ a ~
686 %+ weld
687 $(a (skim t.a |=(c/_i.a (b c i.a))))
688 ^+ t.a
689 [i.a $(a (skim t.a |=(c/_i.a !(b c i.a))))]
692Examples
693--------
695 > =a =|({p=@ q=@} |.((gth p q)))
696 > (sort (limo [0 1 2 3 ~]) a)
697 ~[3 2 1 0]
701***
702### `++swag`
704Infix
706Similar to `substr` in Javascript: extracts a string infix, beginning at
707inclusive index `a`, producing `b` number of characters.
709Accepts
710-------
712`c` is a list.
714Produces
715--------
717A list of the same type as `c`.
719Source
720------
722 ++ swag :: infix
723 |* {{a/@ b/@} c/(list)}
724 (scag +<-> (slag +<-< c))
727Examples
728--------
730 > (swag [2 5] "roly poly")
731 "ly po"
732 > (swag [2 2] (limo [1 2 3 4 ~]))
733 [i=3 t=[i=4 t=~]]
737***
738### `++turn`
740Gate to list
742Accepts a `++list` `a` and a gate `b`. Produces a list with the gate applied
743to each element of the original list.
745Accepts
746-------
748`b` is a gate.
750Produces
751--------
753A list.
755Source
756------
758 ++ turn :: transform
759 ~/ %turn
760 |* {a/(list) b/$-(* *)}
761 |-
762 ?~ a ~
763 [i=(b i.a) t=$(a t.a)]
766Examples
767--------
769 > (turn (limo [104 111 111 110 ~]) ,@t)
770 <|h o o n|>
771 > =a |=(a/@ (add a 4))
772 > (turn (limo [1 2 3 4 ~]) a)
773 ~[5 6 7 8]
777***
778### `++weld`
780Concatenate
782Concatenate two `++list`s `a` and `b`.
784Accepts
785-------
787`a` and `b` are lists.
789Source
790------
792 ++ weld :: concatenate
793 ~/ %weld
794 |* {a/(list) b/(list)}
795 => .(a ^.(homo a), b ^.(homo b))
796 |- ^+ b
797 ?~ a b
798 [i.a $(a t.a)]
801Examples
802--------
804 > (weld "urb" "it")
805 ~[~~u ~~r ~~b ~~i ~~t]
806 > (weld (limo [1 2 ~]) (limo [3 4 ~]))
807 ~[1 2 3 4]
811***
812### `++welp`
814Perfect weld
816Concatenate two `++list`s `a` and `b` without losing their type information
817to homogenization.
819Accepts
820-------
822`a` is a list.
824`b` is a list.
826Produces
827--------
829A list.
831Source
832------
834 ++ welp :: perfect weld
835 =| {* *}
836 |%
837 +- $
838 ?~ +<-
839 +<-(. +<+)
840 +<-(+ $(+<- +<->))
841 --
844Examples
845--------
847 > (welp "foo" "bar")
848 "foobar"
849 ~zod/arvo=/hoon/hoon> (welp ~[60 61 62] ~[%a %b %c])
850 [60 61 62 %a %b %c ~]
851 ~zod/arvo=/hoon/hoon>? (welp ~[60 61 62] ~[%a %b %c])
852 [60 61 62 %a %b %c ~]
853 [@ud @ud @ud %a %b %c %~]
854 ~zod/arvo=/hoon/hoon> (welp [sa+1 so+2 ~] si/3)
855 [[%sa 1] [%so 2] %si 3]
859***
860### `++zing`
862Cons
864Turns a `++list` of lists into a single list by promoting the elements of
865each sublist into the higher.
867Accepts
868-------
870A list of lists.
872Produces
873--------
875A list.
877Source
878------
880 ++ zing :: promote
881 =| *
882 |%
883 +- $
884 ?~ +<
885 +<
886 (welp +<- $(+< +<+))
887 --
891Examples
892--------
894 > (zing (limo [(limo ['a' 'b' 'c' ~]) (limo ['e' 'f' 'g' ~]) (limo ['h' 'i' 'j' ~]) ~]))
895 ~['a' 'b' 'c' 'e' 'f' 'g' 'h' 'i' 'j']
896 > (zing (limo [(limo [1 'a' 2 'b' ~]) (limo [3 'c' 4 'd' ~]) ~]))
897 ~[1 97 2 98 3 99 4 100]
900***