2b: List Logic

+bake

Note: This function isn't specifically a +list function but is included in section 2b of the standard library so is documented here for completeness.

Convert wet $gate .f to a dry $gate by specifying argument mold .a.

+bake is a wet $gate that takes a wet $gate and produces a dry $gate.

Accepts

.f is a $gate.

.a is a $mold.

Produces

A dry $gate whose sample type is .a.

Source

++  bake
  |*  [f=gate a=mold]
  |=  arg=a
  (f arg)

Examples

> =wet-gate |*(a=* [a a])
> (wet-gate 42)
[42 42]
> (wet-gate ['foo' 'bar'])
[['foo' 'bar'] 'foo' 'bar']
> =dry-gate (bake wet-gate @ud)
> (dry-gate 42)
[42 42]
> (dry-gate ['foo' 'bar'])
-need.@ud
-have.[@t @t]
nest-fail

+fand

All indices in +list.

Produces the indices of all occurrences of .nedl in .hstk as a +list of $atoms.

Accepts

.nedl is a +list.

.hstk is a +list.

Produces

A +list.

Source

Examples


+find

First index in +list.

Produces the index of the first occurrence of .nedl in .hstk as the +unit of an $atom.

Accepts

.nedl is a +list.

.hstk is a +list.

Produces

The +unit of an $atom.

Source

Examples


+flop

Produces the +list .a in reverse order.

Accepts

.a is a +list.

Produces

A +list.

Source

Examples


+gulf

List from range.

Produces a +list composed of each consecutive integer starting from .a and ending with .b. .a and .b are themselves included.

Accepts

.a is an $atom.

.b is an $atom.

Produces

A +list.

Source

Examples


+homo

Homogenize.

Produces a +list whose type is a fork of all the contained types in the +list .a. Used when you want to make all the types of the elements of a +list the same.

Accepts

.a is a +list.

Produces

A +list.

Source

Examples


+into

Insert item at index.

Accepts a +list .a, an $atom .b, and a $noun .c, producing the +list of .a with the item .c inserted at index .b.

Accepts

.a is a +list.

.b is an $atom.

.c is a $noun.

Produces

The +list of .a with the item .c inserted at index .b.

Source

Examples


+join

Constructs a new +list, placing .sep between every element of .lit.

Accepts

.sep is a $noun.

.lit is a +list.

Produces

A +list.

Source

Examples


+lent

List length.

Produces the length of any +list .a as an $atom.

Accepts

.a is a +list.

Produces

An $atom.

Source

Examples


+levy

Logical "and" on +list.

Computes the $flag logical "and" on the results of $gate .b applied to each individual element in +list .a.

Accepts

.a is a +list.

.b is a $gate.

Produces

A $flag.

Source

Examples


+lien

Logical "or" on +list.

Computes the $flag logical "or" on the results of applying $gate .b to every element of +list .a.

Accepts

.a is a +list.

.b is a $gate.

Source

Examples


+limo

List Constructor.

Turns a null-terminated tuple into a +list.

Accepts

.a is a null-terminated tuple.

Produces

A +list.

Source

Examples


+murn

Maybe transform.

Passes each member of +list .a to $gate .b, which must produce a +unit. Produces a new +list with all the results that do not produce ~.

Accepts

.a is a +list.

.b is a $gate that produces a +unit.

Produces

A +list.

Source

Examples


+oust

Remove.

Removes elements from +list .c beginning at inclusive index .a, removing .b number of elements.

Accepts

.c is a +list.

Produces

A +list.

Source

Examples


+reap

Replicate.

Produces a +list containing .a copies of .b.

Accepts

.a is an $atom.

.b is a $noun.

Produces

A +list.

Source

Examples


+rear

Last item of +list.

Produces the last item in +list .a, crashing if .a is null.

Accepts

.a is a +list.

Produces

The type of the last element in .a.

Source

Examples


+reel

Right fold.

Moves right to left across a +list .a, recursively slamming a binary $gate .b with an element from .a and an accumulator, producing the final value of the accumulator.

(To "slam" means to call a $gate and give it a sample/samples. In this instance, .a is the +list of samples that are given to the $gate .b.)

The initial value of the accumulator is the bunt of .b's second argument (+<+). This can occasionally produce undesired behavior (see examples). If you need more control over the initial value, try making use of $_ and |:, or perhaps +spin or +spun.

Accepts

.a is a +list.

.b is a binary $gate.

Produces

The accumulator, which is a $noun.

Source

Examples

+mul's default sample is 1, so calling +reel with +mul yields the expected behavior:

However, if you build a $gate that uses +mul like so, the sample defaults to 0 since that is the bunt of @:

We can fix this with |::

If you check the definition of +mul, you'll see that it also utilizes this pattern.

We can check explicitly what sequence of operations +reel performs like this:


+roll

Left fold.

Moves left to right across a +list .a, recursively slamming a binary $gate .b with an element from the +list and an accumulator, producing the final value of the accumulator.

(To "slam" means to call a $gate and give it a sample/samples. In this instance, .a is the +list of samples that are given to the $gate .b.)

The initial value of the accumulator is .b's second argument (+<+). This can occasionally produce undesired behavior (see examples). If you need more control over the initial value, try making use of $_ and |:, or perhaps +spin or +spun.

Accepts

.a is a +list.

.b is a binary $gate.

Produces

The accumulator, which is a $noun.

Source

Examples

+mul's default sample is 1, so calling +roll with +mul yields the expected behavior:

However, if you build a $gate that uses +mul like so, the sample defaults to 0 since that is the bunt of @:

We can fix this with |::

If you check the definition of +mul, you'll see that it also utilizes this pattern.

We can check explicitly what sequence of operations +roll performs like this:

This is in contrast to what one might expect:


+scag

Prefix.

Accepts an $atom .a and +list .b, producing the first .a elements of the front of the +list.

Accepts

.a is an $atom.

.b is a +list.

Produces

A +list of the same type as .b.

Source

Examples


+skid

Separate.

Separates a +list .a into two +lists: those elements of .a who produce %.y when slammed to $gate .b, and those who produce %.n.

(To "slam" means to call a $gate and give it a sample/samples. In this instance, .a is the +list of samples that are given to the $gate .b.)

Accepts

.a is a +list.

.b is a $gate that accepts one argument and produces a $flag.

Produces

A cell of two +lists.

Source

Examples


+skim

Filter.

Cycles through the members of a +list .a, passing them to a $gate .b and producing a +list of all of the members that produce %.y. Inverse of +skip.

Accepts

.a is a +list.

.b is a $gate that accepts one argument and produces a $flag.

Produces

A +list.

Source

Examples


+skip

Except.

Cycles through the members of +list .a, passing them to a $gate .b. Produces a +list of all of the members that produce %.n. Inverse of +skim.

Accepts

.a is a +list.

.b is a $gate that accepts one argument and produces a $flag.

Produces

A +list of the same type as .a.

Source

Examples


+slag

Suffix.

Accepts an $atom .a and +list .b, producing the remaining elements from .b starting at .a.

Accepts

.a is an $atom.

.b is a +list.

Produces

A +list of the same type as .b.

Source

Examples


+snag

Index.

Accepts an $atom .a and a +list .b, producing the element at the index of .aand failing if the +list is null. +lists are 0-indexed.

Accepts

.a is an $atom.

.b is a +list.

Produces

Produces an element of .b, or crashes if no element exists at that index.

Source

Examples


+snap

Replace item at index.

Accepts a +list .a, an $atom .b, and a $noun .c, producing the +list of .a with the item at index .b replaced with .c.

Accepts

.a is a +list.

.b is a $atom.

.c is a $noun.

Produces

The +list of .a with the item at index .b replaced with .c.

Source

Examples


+snip

Drop tail off +list.

Removes the last element from +list .a.

Accepts

.a is a +list.

Produces

A +list.

Source

Examples


+snoc

Append.

Accepts a +list .a and a $noun .b, producing the +list of .b appended to .a.

Accepts

.a is a +list.

.b is a $noun.

Produces

Produces a +list of .b appended to .a.

Source

Examples


+sort

Quicksort.

Quicksort: accepts a +list .a and a $gate .b which accepts two $nouns and produces a $flag. +sort then produces a +list of the elements of .a, sorted according to .b.

Accepts

.a is a +list.

.b is a $gate that accepts two $nouns and produces a $flag.

Produces

A +list

Source

Examples


+spin

Gate to +list, with state.

Accepts a +list .a, some state .b, and a $gate .c. .c is called with a tuple (the head is an element of .a and the tail is the state .b), and should produce a tuple of the transformed element and the (potentially modified) state .b. Produces a cell where the first element is a +list of the transformed elements of .a, and the second element is the final value of .b.

Accepts

.a is a +list.

.b is a $noun.

.c is a $gate.

Produces

A pair of a +list and a $noun.

Source

Examples

Trivial example; does nothing with the state.

Form a pair with .p as the index and .q as the +list element.

Create 10 random numbers below 10.

Discussion

The expression (~(rads og eny) 2) creates a random number less than 2, seeding the random number generator with entropy (.eny). The head of the product is the random number, the tail is the continuation of the random number generator.


+spun

Gate to +list, with state.

Accepts a +list .a and a $gate .b. Unlike +spin, +spun doesn't have a .c parameter. Instead, it derives its intenral state by bunting the tail of the sample of .b.

Produces a +list with .b applied to each element of .a.

Accepts

.a is a +list.

.b is a $gate.

Produces

A +list.

Source

Examples

.p as the index and .q as the +list element.

Joins two +lists into a +list of pairs.


+swag

Infix.

Similar to substr() in Javascript: extracts a string infix, beginning at inclusive index .a, producing .b number of characters.

Accepts

.a is an $atom.

.b is an $atom.

.c is a +list.

Produces

A +list of the same type as .c.

Source

Examples


+turn

Gate to +list.

Accepts a +list .a and a $gate .b. Produces a +list with the $gate applied to each element of the original +list.

Accepts

.a is a +list.

.b is a $gate.

Produces

A +list.

Source

Examples

Discussion

+turn is Hoon's version of map in Haskell.


+weld

Concatenate.

Concatenate two +lists .a and .b.

Accepts

.a and .b are +lists.

Source

Examples


+welp

Perfect weld.

Concatenate two +lists .a and .b without losing their type information to homogenization.

Accepts

.a is a +list.

.b is a +list.

Produces

A +list.

Source

Examples


+zing

Turns a +list of +lists into a single +list by promoting the elements of each sublist into the higher.

Accepts

A +list of +lists.

Produces

A +list.

Source

Examples


Last updated