diff options
| author | Marshall Lochbaum <mwlochbaum@gmail.com> | 2021-07-24 22:47:46 -0400 |
|---|---|---|
| committer | Marshall Lochbaum <mwlochbaum@gmail.com> | 2021-07-24 22:47:46 -0400 |
| commit | a17782ce2ec31709ce30edb3d96fe2f3a9a6ed1f (patch) | |
| tree | b601681b2282f1a51042f8faf5bfe0e0242c0c31 /doc/fill.md | |
| parent | 436bf368830c828f8008bf55632e2bb4c2a2578f (diff) | |
Documentation on fill elements
Diffstat (limited to 'doc/fill.md')
| -rw-r--r-- | doc/fill.md | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/doc/fill.md b/doc/fill.md new file mode 100644 index 00000000..45d98487 --- /dev/null +++ b/doc/fill.md @@ -0,0 +1,65 @@ +*View this file with results and syntax highlighting [here](https://mlochbaum.github.io/BQN/doc/fill.html).* + +# Fill elements + +A few array operations need an array element to use when no existing element applies. BQN tries to maintain a "default" element for every array, known as a fill element, for this purpose. If it's known, the fill element is a nested array structure where each atom is either `0` or `' '`. If no fill is known, a function that requests it results in an error. + +Fills are used by [Take](take.md) (`β`) when a value in `π¨` is larger than the corresponding length in `π©`, by the two [Nudge](shift.md) functions (`»«`) when `π©` is non-empty, and by [First](pick.md) (`β`) and [Reshape](reshape.md) (`β₯`) when `π©` is empty. Except for these specific cases, the fill value an array has can't affect the program. The result of [Match](match.md) (`β‘`) doesn't depend on fills, and any attempt to compute a fill can't cause side effects. + +## Using fills + +For the examples in this section we'll use the fact that an all-number array usually has `0` as a fill while a string has `' '` (BQN maintains fills alongside array values rather than deriving them from arrays, so it's possible to construct arrays where this isn't true, but this probably wouldn't happen in ordinary code). + +[Take](take.md) (`β`) and [Nudge](shift.md) (`»«`) in either direction use the fill for padding, to extend the array past its boundary. For example, `π¨βπ©` will add elements to one side when a number in `|π¨` is larger than the corresponding length in `β’π©`. + + Β―7 β 4β₯3 # Fill with 0 + + Β―7 β "qrst" # Fill with space + +Nudge Left or Right shifts the array over and places a fill in the vacated space, effectively extending it backwards by one. If `π©` is empty then it shouldn't give an error, but it's safer not to rely on this. + + Ȭ β¨4β₯3,"qrst"β© + + 3ββ¨β© # Fill unknown + + Β»β¨β© # Fill not needed + +[First](pick.md) (`β`) and [Reshape](reshape.md) (`β₯`) use the fill when `π©` is empty, and in the case of Reshape only when the result needs to be non-empty. + + β "" + + 4 β₯Β¨ β¨β0, ""β© + + 0βΏ3 β₯ β¨β© # Fill not needed + +If for some reason you need to find an array's fill element, the easiest way is `β0β₯a`. + + β0β₯"string" + +## How fills are computed + +For the exact requirements placed on fill, see [the specification](../spec/inferred.md#fill-elements) (particularly "required functions"). This section loosely describes behavior in existing BQN implementations, and includes some parts that aren't required in the specification. + +A fill element should encompass something that's necessarily true for all elements of an array. If the way an array is computed implies it's all numbers, the fill should be `0`. If every element is a list of two numbers, then the fill should be `β¨0,0β©`. If every element is a list but the lengths might vary, `β¨β©` is probably a reasonable fill element. + +For [arithmetic](arithmetic.md) primitives, the fill is found by the rules of pervasion, applying the function to both argument fills. Generally this means it consists of `0`, but character arithmetic also allows space fills. + + Β» "abc" + 4βΏ3βΏ2 + +[Mapping](map.md) modifiers Each and Table (`Β¨β`) might try to follow a similar strategy, applying `π½` to argument fills to obtain the result fill. The absolute rule here is that this computation cannot cause side effects or an error, so for a complicated `π½` such as a block function this procedure is likely to be aborted to avoid disrupting the rest of the program. + +Most other primitives fit in one of three broad categories as shown in the table below. Structural primitives, indicated by `β’`, don't change the fill of `π©`. Combining structural primitives, indicated by `β©`, only depend on the fill of all combined arraysβelements of `π©` in the one-argument case, or `π¨` and `π©` in the two-argument case. Finally, many functions such as [search functions](search.md) return only numbers and have a fill of `0`. + +| Fill | Monads | Dyads | Modifiers +|--------|--------------|-------------|---------- +| `β’` | `β§β¨β₯β»«β½βββ·` | `β₯ββββ½β/β` | `` π½` `` +| `β©` | `>βΎ` | `βΎβ»«` +| `0` | `β’/βββββ` | `ββββββ·` + +Besides these, there are a few primitives with special fills. [Enclose](enclose.md) (`<`) uses a fill derived directly from `π©`, with all numbers replaced by `0` and characters by `' '` (if it contains non-data atoms, the fill doesn't exist). [Range](range.md) (`β`) does the same, although the reason is less obvious: the result elements don't match `π©`, but they have the same structure. + +[Prefixes and Suffixes](prefixes.md) (`ββ`) use `0βπ©` for the fill, as do [Group](group.md) and Group Indices (`β`) in the single-axis case. Fills for multi-axis `β` are more complicated, but follow the rule that variable-length axes are changed to length 0. The *elements* of the result of `β` also have a fill specified: the same as `π©` for Group, or `0` for Group Indices. + + 6 β ββ3 # Two fills at the end + + Ȭ 3βΏ4βΏ1 /βΈβ "abc0123A" |
