diff options
| author | Marshall Lochbaum <mwlochbaum@gmail.com> | 2021-08-07 20:41:31 -0400 |
|---|---|---|
| committer | Marshall Lochbaum <mwlochbaum@gmail.com> | 2021-08-07 20:41:31 -0400 |
| commit | 7bf2aa4054b8378a76dff63acdccbcdad91f68e6 (patch) | |
| tree | 1bff7a2a2b11fa259bc80722fdcb0609768c7718 /doc | |
| parent | 532796eb397c8374e0546de9e4ab70c2955349f3 (diff) | |
BREAKING: Don't allow First of empty or reshaping empty to non-empty
Diffstat (limited to 'doc')
| -rw-r--r-- | doc/fill.md | 14 | ||||
| -rw-r--r-- | doc/find.md | 4 | ||||
| -rw-r--r-- | doc/pick.md | 7 | ||||
| -rw-r--r-- | doc/reshape.md | 4 | ||||
| -rw-r--r-- | doc/types.md | 2 |
5 files changed, 12 insertions, 19 deletions
diff --git a/doc/fill.md b/doc/fill.md index 45d98487..ed93b02c 100644 --- a/doc/fill.md +++ b/doc/fill.md @@ -4,7 +4,7 @@ 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. +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 [Reshape](reshape.md) (`β₯`) when `π¨` contains `β`. 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 @@ -24,17 +24,13 @@ Nudge Left or Right shifts the array over and places a fill in the vacated space Β»β¨β© # 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. +[Reshape](reshape.md#computed-lengths) (`β₯`) uses the fill when `π¨` contains `β` and the product of the rest of `π¨` doesn't evenly divide the number of elements in `π©`. - β "" + ββΏ8 β₯ "completepart" - 4 β₯Β¨ β¨β0, ""β© +If for some reason you need to find an array's fill element, the easiest general way is probably `βΒ»1ββ₯a`. - 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" + βΒ»1ββ₯"string" ## How fills are computed diff --git a/doc/find.md b/doc/find.md index b91a5930..5309909f 100644 --- a/doc/find.md +++ b/doc/find.md @@ -18,13 +18,13 @@ Like Windows, the result usually doesn't have the same dimensions as `π©`. Thi "string" (β’ββ’ββ·) "substring" # APL style -If `π¨` is larger than `π©`, the result is empty, and there's no error even in cases where Windows would fail. One place this tends to come up is when applying [First](pick.md) (`β`) the result: `ββ·` tests whether `π¨` appears in `π©` at the first position, that is, whether it's a prefix of `π©`. If `π¨` is longer than `π©` it shouldn't be a prefix, so 0 is appropriate. +If `π¨` is larger than `π©`, the result is empty, and there's no error even in cases where Windows would fail. One place this tends to come up is when applying [First](pick.md#first) (`β`) the result: `ββ·` tests whether `π¨` appears in `π©` at the first position, that is, whether it's a prefix of `π©`. If `π¨` is longer than `π©` it shouldn't be a prefix. First will fail but using a [fold](fold.md) `0β£Β΄β₯ββ·` instead gives a 0 in this case. "loooooong" β· "short" 9 β "short" - β "loooooong" β· "short" + 0 β£Β΄ "loooooong" β· "short" This pattern also works in the high-rank case discussed below, testing whether `π¨` is a multi-dimensional prefix starting at the lowest-index corner of `π©`. diff --git a/doc/pick.md b/doc/pick.md index 9b014796..092f87ed 100644 --- a/doc/pick.md +++ b/doc/pick.md @@ -4,7 +4,7 @@ Pick (`β`) chooses elements from `π©` based on [index](indices.md) lists from `π¨`. `π¨` can be a plain list, or even one number if `π©` is a list, in order to get one element from `π©`. It can also be an array of index lists, or have deeper array structure: each index list will be replaced with the element of `π©` at that index, effectively applying to `π¨` at [depth](depth.md#the-depth-modifier) 1. -With no `π¨`, monadic `βπ©` takes the first element of `π©` in index order, or its [fill element](fill.md) if `π©` is empty (causing an error if no fill is known). +With no `π¨`, monadic `βπ©` takes the first element of `π©` in index order, with an error if `π©` is empty. While sometimes "scatter-point" indexing is necessary, using Pick to select multiple elements from `π©` is less array-oriented than [Select](select.md) (`β`), and probably slower. Consider rearranging your data so that you can select along axes instead of picking out elements. @@ -44,13 +44,10 @@ With no left argument, `β` is called First, and performs a slight generalizati β "First" β β4βΏ2βΏ5βΏ1 -If `π©` is empty then Pick always results in an error. First never gives an error: instead it returns the [fill element](fill.md) for `π©`. +If `π©` is empty then First results in an error, like Pick. β "" β β’Ο - β 0β<β¨" ",β4β© - -So one way to find the fill element for an array `π©` of any shape is `β0β₯π©`. In APL it's common to get the last element of a list with an idiom that translates to `ββ½`, or First-[Reverse](reverse.md). In BQN the most straightforward way is to select with index `Β―1` instead. I also sometimes use [Fold](fold.md) with the Right [identity function](identity.md). diff --git a/doc/reshape.md b/doc/reshape.md index 902beadc..4be0c608 100644 --- a/doc/reshape.md +++ b/doc/reshape.md @@ -88,11 +88,11 @@ If the left argument implies a smaller number of elements, then only the initial 3βΏ3 β₯ a -If the left argument implies a larger number of elements, then the argument elements are reused cyclically. Below, we reach the last element `247` and start over at `135`. If the array doesn't have any elements to start with, its [fill element](fill.md) is used instead, but it's probably best not to invoke this case! +If the left argument implies a larger number of elements, then the argument elements are reused cyclically. Below, we reach the last element `247` and start over at `135`. If the array doesn't have any elements to start with, you'll get an error as there aren't any elements available. 15 β₯ a - 4 β₯ β0 # Fill for β0 is 0 + 4 β₯ β0 Reshape is the idiomatic way to make an array filled with a constant value (that is, where all elements are the same). For an atom element, just reshape it directly; for an arbitrary element, first enclose it to create a unit, and then reshape it. diff --git a/doc/types.md b/doc/types.md index ad295676..f3b7f414 100644 --- a/doc/types.md +++ b/doc/types.md @@ -71,7 +71,7 @@ Other linear combinations such as adding two characters or negating a character A BQN array is a multidimensional arrangement of data. This means it has a certain [*shape*](shape.md), which is a finite list of natural numbers giving the length along each axis, and it contains an *element* for each possible [*index*](indices.md), which is a choice of one natural number that's less than each axis length in the shape. The total number of elements, or *bound*, is then the product of all the lengths in the shape. The shape may have any length including zero, and this shape is known as the array's *rank*. An array of rank 0, which always contains exactly one element, is called a *unit*, while an array of rank 1 is called a *list* and an array of rank 2 is called a *table*. -Each arrayβempty or nonemptyβhas an inferred property called a *fill*. The fill either indicates what element should be used to pad an array, or that such an element is not known and an error should result. Fills can be used by [Take](take.md) (`β`), the two [Nudge](shift.md) functions (`»«`), [First](pick.md) (`β`), and [Reshape](reshape.md) (`β₯`). +Each arrayβempty or nonemptyβhas an inferred property called a [*fill*](fill.md). The fill either indicates what element should be used to pad an array, or that such an element is not known and an error should result. Fills can be used by [Take](take.md) (`β`), the two [Nudge](shift.md) functions (`»«`), and [Reshape](reshape.md) (`β₯`). Arrays are value types (or immutable), so that there is no way to "change" the shape or elements of an array. An array with different properties is a different array. As a consequence, arrays are an inductive type, and it's not possible for an array to contain itself, or contain an array that contains itself, and so on. However, it is possible for an array to contain a function or other operation that has access to the array through a variable, and in this sense an array can "know about" itself. |
