on 140ac0e help sign in
895 lines 13 kB history raw
1# 2b: list logic
2Reverse
4### `++flop`
6Produces the list `a` in reverse order.
8Accepts
9-------
11`a` is a list.
13Produces
14--------
16A list.
18Source
19------
21 ++ flop :: reverse
22 ~/ %flop
23 |* a/(list)
24 => .(a (homo a))
25 ^+ a
26 =+ b=`_a`~
27 |-
28 ?~ a b
29 $(a t.a, b [i.a b])
33Examples
34--------
36 > =a (limo [1 2 3 ~])
37 > (flop a)
38 ~[3 2 1]
42***
43### `++homo`
45Homogenize
47Produces a `++list` whose type is a fork of all the contained types in the
48list `a`. Used when you want to make all the types of the elements of a list the same.
50Accepts
51-------
53`a` is a list.
55Produces
56--------
58a list.
60Source
61------
63 ++ homo :: homogenize
64 |* a/(list)
65 ^+ =< $
66 |% +- $ ?:(*? ~ [i=(snag 0 a) t=$])
67 --
68 a
71Examples
72--------
74 > lyst
75 [i=1 t=[i=97 t=[i=2 t=[i=98 t=[i=[~ u=10] t=~]]]]]
76 > (homo lyst)
77 ~[1 97 2 98 [~ u=10]]
78 > =a (limo [1 2 3 ~])
79 > a
80 [i=1 t=[i=2 t=[i=3 t=~]]]
81 > (homo a)
82 ~[1 2 3]
86***
87### `++lent`
89List length
91Produces the length of any `++list` `a` as an atom.
93Accepts
94-------
96`a` is a list.
98Produces
99--------
101an atom.
103Source
104------
106 ++ lent :: length
107 ~/ %lent
108 |= a/(list)
109 ^- @
110 =+ b=0
111 |-
112 ?~ a b
113 $(a t.a, b +(b))
116Examples
117--------
119 > (lent (limo [1 2 3 4 ~]))
120 4
121 > (lent (limo [1 'a' 2 'b' (some 10) ~]))
122 5
126***
127### `++levy`
129Logical "and" on list
131Computes the Boolean logical "and" on the results of gate `b` applied to each individual element in `++list` `a`.
133Accepts
134-------
136`a` is a list.
138`b` is a gate.
140Produces
141--------
143A boolean.
145Source
146------
148 ++ levy
149 ~/ %levy :: all of
150 |* {a/(list) b/$-(* ?)}
151 |- ^- ?
152 ?~ a &
153 ?. (b i.a) |
154 $(a t.a)
157Examples
158--------
160 > =a |=(a/@ (lte a 1))
161 > (levy (limo [0 1 2 1 ~]) a)
162 %.n
163 > =a |=(a/@ (lte a 3))
164 > (levy (limo [0 1 2 1 ~]) a)
165 %.y
169***
170### `++lien`
172Logical "or" on list
174Computes the Boolean logical "or" on the results of applying [gate]() `b` to every element of [`++list`]() `a`.
176Accepts
177-------
179`a` is a list.
181`b` is a gate.
183Source
184------
186 ++ lien :: some of
187 ~/ %lien
188 |* {a/(list) b/$-(* ?)}
189 |- ^- ?
190 ?~ a |
191 ?: (b i.a) &
192 $(a t.a)
194Examples
195--------
197 > =a |=(a=@ (gte a 1))
198 > (lien (limo [0 1 2 1 ~]) a)
199 %.y
200 > =a |=(a=@ (gte a 3))
201 > (lien (limo [0 1 2 1 ~]) a)
202 %.n
206***
207### `++limo`
209List Constructor
211Turns a null-terminated tuple into a [`++list`]().
213Accepts
214-------
216`a` is a null-terminated tuple.
218Produces
219--------
221A `++list`.
223Source
224------
226 ++ limo :: listify
227 |* a=*
228 ^+ =< $
229 |% +- $ ?~(a ~ ?:(_? i=-.a t=$ $(a +.a)))
230 --
231 a
233Examples
234--------
236 > (limo [1 2 3 ~])
237 [i=1 t=[i=2 t=[i=3 t=~]]]
241***
242### `++murn`
244Maybe transform
246Passes each member of `++list` `a` to gate `b`, which must produce a
247`++unit`. Produces a new list with all the results that do not produce
248`~`.
250Accepts
251-------
253`a` is a list.
255`b` is a gate that produces a unit.
257Produces
258--------
260A unit.
262Source
263------
265 ++ murn :: maybe transform
266 ~/ %murn
267 |* {a/(list) b/$-(* (unit))}
268 |-
269 ?~ a ~
270 =+ c=(b i.a)
271 ?~ c
272 $(a t.a)
273 [i=u.c t=$(a t.a)]
275Examples
276--------
278 > =a |=(a=@ ?.((gte a 2) ~ (some (add a 10))))
279 > (murn (limo [0 1 2 3 ~]) a)
280 [i=12 t=[i=13 t=~]]
284***
285### `++reap`
287Replicate
289Replicate: produces a `++list` containing `a` copies of `b`.
291Accepts
292-------
294`b` is a noun.
296Produces
297--------
299A list.
301Source
302------
304 ++ reap :: replicate
305 ~/ %reap
306 |* {a/@ b/*}
307 |- ^- (list _b)
308 ?~ a ~
309 [b $(a (dec a))]
311Examples
312--------
314 > (reap 20 %a)
315 ~[%a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a %a]
316 > (reap 5 ~s1)
317 ~[~s1 ~s1 ~s1 ~s1 ~s1]
318 > `@dr`(roll (reap 5 ~s1) add)
319 ~s5
323***
324### `++reel`
326Right fold
328Right fold: moves right to left across a `++list` `a`, recursively slamming
329a binary gate `b` with an element from `a` and an accumulator, producing
330the final value of the accumulator.
332Accepts
333-------
335`b` is a binary gate.
337Produces
338--------
340The accumulator, which is a noun.
342Source
343------
345 ++ reel :: right fold
346 ~/ %reel
347 |* {a/(list) b/_|=({* *} +<+)}
348 |- ^+ +<+.b
349 ?~ a
350 +<+.b
351 (b i.a $(a t.a))
354Examples
355--------
357 > =sum =|([p=@ q=@] |.((add p q)))
358 > (reel (limo [1 2 3 4 5 ~]) sum)
359 15
360 > =a =|([p=@ q=@] |.((sub p q)))
361 > (reel (limo [6 3 1 ~]) a)
362 4
363 > (reel (limo [3 6 1 ~]) a)
364 ! subtract-underflow
365 ! exit
369***
370### `++roll`
372Left fold
374Left fold: moves left to right across a list `a`, recursively slamming a
375binary [gate]() `b` with an element from the [`++list`]() and an accumulator,
376producing the final value of the accumulator.
378Accepts
379-------
381`b` is a binary [gate]().
383Produces
384--------
386The accumulator, which is a [noun]().
388Source
389------
391 ++ roll :: left fold
392 ~/ %roll
393 |* [a=(list) b=_|=([* *] +<+)]
394 |- ^+ +<+.b
395 ?~ a
396 +<+.b
397 $(a t.a, b b(+<+ (b i.a +<+.b)))
399Examples
400--------
402 > =sum =|([p=@ q=@] |.((add p q)))
403 > (roll (limo [1 2 3 4 5 ~]) sum)
404 q=15
405 > =a =|([p=@ q=@] |.((sub p q)))
406 > (roll (limo [6 3 1 ~]) a)
407 ! subtract-underflow
408 ! exit
409 > (roll (limo [1 3 6 ~]) a)
410 q=4
414***
415### `++scag`
417Prefix
419Accepts an atom `a` and [list]() `b`, producing the first `a` elements of
420the front of the list.
422Accepts
423-------
425`b` is a list.
427Produces
428--------
430A list of the same type as `b`.
432Source
433------
435 ++ scag :: prefix
436 ~/ %scag
437 |* [a=@ b=(list)]
438 |- ^+ b
439 ?: |(?=(~ b) =(0 a)) ~
440 [i.b $(b t.b, a (dec a))]
442Examples
443--------
445 > (scag 2 (limo [0 1 2 3 ~]))
446 [i=0 t=[i=1 t=~]]
447 > (scag 10 (limo [1 2 3 4 ~]))
448 [i=1 t=[i=2 t=[i=3 t=[i=4 t=~]]]]
452***
453### `++skid`
455Separate
457Separates a `++list` `a` into two lists - Those elements of `a` who produce
458true when slammed to gate `b` and those who produce `%.n`.
460Accepts
461-------
463`b` is a gate that accepts one argument and produces a loobean.
465Produces
466--------
468A cell of two lists.
470Source
471------
473 ++ skid :: separate
474 ~/ %skid
475 |* {a/(list) b/$-(* ?)}
476 |- ^+ [p=a q=a]
477 ?~ a [~ ~]
478 =+ c=$(a t.a)
479 ?:((b i.a) [[i.a p.c] q.c] [p.c [i.a q.c]])
483Examples
484--------
486 > =a |=(a/@ (gth a 1))
487 > (skid (limo [0 1 2 3 ~]) a)
488 [p=[i=2 t=[i=3 t=~]] q=[i=0 t=[i=1 t=~]]]
492***
493### `++skim`
495Suffix
497Cycles through the members of a list `a`, passing them to a gate `b` and
498producing a list of all of the members that produce `%.y`. Inverse of
499`++skip`.
501Accepts
502-------
504`b` is a gate that accepts one argument and produces a boolean.
506Produces
507--------
509A boolean
511Source
512------
514 ++ skim :: only
515 ~/ %skim
516 |* {a/(list) b/$-(* ?)}
517 |-
518 ^+ a
519 ?~ a ~
520 ?:((b i.a) [i.a $(a t.a)] $(a t.a))
523Examples
524--------
526 > =a |=(a/@ (gth a 1))
527 > (skim (limo [0 1 2 3 ~]) a)
528 [i=2 t=[i=3 t=~]]
532***
533### `++skip`
535Except
537Cycles through the members of `++list` `a`, passing them to a gate `b`.
538Produces a list of all of the members that produce `%.n`. Inverse of
539`++skim`.
541Accepts
542-------
544`b` is a gate that accepts one argument and produces a loobean.
546Produces
547--------
549A list of the same type as `a`.
551Source
552------
554 ++ skip :: except
555 ~/ %skip
556 |* {a/(list) b/$-(* ?)}
557 |-
558 ^+ a
559 ?~ a ~
560 ?:((b i.a) $(a t.a) [i.a $(a t.a)])
563Examples
564--------
566 > =a |=(a/@ (gth a 1))
567 > (skip (limo [0 1 2 3 ~]) a)
568 [i=0 t=[i=1 t=~]]
572***
573### `++slag`
575Suffix
577Accepts an atom `a` and list `b`, producing the remaining elements from
578`b` starting at `a`.
580Accepts
581-------
583`b` is a list.
585Produces
586--------
588A list of the same type as `b`.
590Source
591------
593 ++ slag :: suffix
594 ~/ %slag
595 |* {a/@ b/(list)}
596 |- ^+ b
597 ?: =(0 a) b
598 ?~ b ~
599 $(b t.b, a (dec a))
602Examples
603--------
605 > (slag 2 (limo [1 2 3 4 ~]))
606 [i=3 t=[i=4 t=~]]
607 > (slag 1 (limo [1 2 3 4 ~]))
608 [i=2 t=[i=3 t=[i=4 t=~]]]
612***
613### `++snag`
615Index
617Accepts an atom `a` and a `++list` `b`, producing the element at the index
618of `a`and failing if the list is null. Lists are 0-indexed.
620Accepts
621-------
623`b` is a list.
625Produces
626--------
628Produces an element of `b`, or crashes if no element exists at that index.
630Source
631------
633 ++ snag :: index
634 ~/ %snag
635 |* {a/@ b/(list)}
636 |-
637 ?~ b
638 ~|('snag-fail' !!)
639 ?: =(0 a) i.b
640 $(b t.b, a (dec a))
643Examples
644--------
646 > (snag 2 "asdf")
647 ~~d
648 > (snag 0 `(list ,@ud)`~[1 2 3 4])
649 1
653***
654### `++sort`
656Quicksort
658Quicksort: accepts a `++list` `a` and a gate `b` which accepts two nouns and
659produces a loobean. `++sort` then produces a list of the elements of `a`,
660sorted according to `b`.
662Accepts
663-------
665`b` is a gate that ccepts two nouns and produces a boolean.
667Produces
668--------
670A list
672Source
673------
675 ++ sort !. :: quicksort
676 ~/ %sort
677 |* {a/(list) b/$-([* *] ?)}
678 => .(a ^.(homo a))
679 |- ^+ a
680 ?~ a ~
681 %+ weld
682 $(a (skim t.a |=(c/_i.a (b c i.a))))
683 ^+ t.a
684 [i.a $(a (skim t.a |=(c/_i.a !(b c i.a))))]
687Examples
688--------
690 > =a =|({p=@ q=@} |.((gth p q)))
691 > (sort (limo [0 1 2 3 ~]) a)
692 ~[3 2 1 0]
696***
697### `++swag`
699Infix
701Similar to `substr` in Javascript: extracts a string infix, beginning at
702inclusive index `a`, producing `b` number of characters.
704Accepts
705-------
707`c` is a list.
709Produces
710--------
712A list of the same type as `c`.
714Source
715------
717 ++ swag :: infix
718 |* {{a/@ b/@} c/(list)}
719 (scag +<-> (slag +<-< c))
722Examples
723--------
725 > (swag [2 5] "roly poly")
726 "ly po"
727 > (swag [2 2] (limo [1 2 3 4 ~]))
728 [i=3 t=[i=4 t=~]]
732***
733### `++turn`
735Gate to list
737Accepts a `++list` `a` and a gate `b`. Produces a list with the gate applied
738to each element of the original list.
740Accepts
741-------
743`b` is a gate.
745Produces
746--------
748A list.
750Source
751------
753 ++ turn :: transform
754 ~/ %turn
755 |* {a/(list) b/$-(* *)}
756 |-
757 ?~ a ~
758 [i=(b i.a) t=$(a t.a)]
761Examples
762--------
764 > (turn (limo [104 111 111 110 ~]) ,@t)
765 <|h o o n|>
766 > =a |=(a/@ (add a 4))
767 > (turn (limo [1 2 3 4 ~]) a)
768 ~[5 6 7 8]
772***
773### `++weld`
775Concatenate
777Concatenate two `++list`s `a` and `b`.
779Accepts
780-------
782`a` and `b` are lists.
784Source
785------
787 ++ weld :: concatenate
788 ~/ %weld
789 |* {a/(list) b/(list)}
790 => .(a ^.(homo a), b ^.(homo b))
791 |- ^+ b
792 ?~ a b
793 [i.a $(a t.a)]
796Examples
797--------
799 > (weld "urb" "it")
800 ~[~~u ~~r ~~b ~~i ~~t]
801 > (weld (limo [1 2 ~]) (limo [3 4 ~]))
802 ~[1 2 3 4]
806***
807### `++welp`
809Perfect weld
811Concatenate two `++list`s `a` and `b` without losing their type information
812to homogenization.
814Accepts
815-------
817`a` is a list.
819`b` is a list.
821Produces
822--------
824A list.
826Source
827------
829 ++ welp :: perfect weld
830 =| {* *}
831 |%
832 +- $
833 ?~ +<-
834 +<-(. +<+)
835 +<-(+ $(+<- +<->))
836 --
839Examples
840--------
842 > (welp "foo" "bar")
843 "foobar"
844 ~zod/arvo=/hoon/hoon> (welp ~[60 61 62] ~[%a %b %c])
845 [60 61 62 %a %b %c ~]
846 ~zod/arvo=/hoon/hoon>? (welp ~[60 61 62] ~[%a %b %c])
847 [60 61 62 %a %b %c ~]
848 [@ud @ud @ud %a %b %c %~]
849 ~zod/arvo=/hoon/hoon> (welp [sa+1 so+2 ~] si/3)
850 [[%sa 1] [%so 2] %si 3]
854***
855### `++zing`
857Cons
859Turns a `++list` of lists into a single list by promoting the elements of
860each sublist into the higher.
862Accepts
863-------
865A list of lists.
867Produces
868--------
870A list.
872Source
873------
875 ++ zing :: promote
876 =| *
877 |%
878 +- $
879 ?~ +<
880 +<
881 (welp +<- $(+< +<+))
882 --
886Examples
887--------
889 > (zing (limo [(limo ['a' 'b' 'c' ~]) (limo ['e' 'f' 'g' ~]) (limo ['h' 'i' 'j' ~]) ~]))
890 ~['a' 'b' 'c' 'e' 'f' 'g' 'h' 'i' 'j']
891 > (zing (limo [(limo [1 'a' 2 'b' ~]) (limo [3 'c' 4 'd' ~]) ~]))
892 ~[1 97 2 98 3 99 4 100]
895***