aboutsummaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorMarshall Lochbaum <mwlochbaum@gmail.com>2021-02-11 14:02:31 -0500
committerMarshall Lochbaum <mwlochbaum@gmail.com>2021-02-11 14:02:31 -0500
commitf07e748444200bad23c6a44a30af82bc7b7c8e48 (patch)
tree76541d07cc671763e1bdcddf047f110b53a73341 /spec
parent0479522e13e439f95bfc533a1297cff4f8175622 (diff)
Always make structural functions enclose atoms
Diffstat (limited to 'spec')
-rw-r--r--spec/primitive.md8
-rw-r--r--spec/reference.bqn4
2 files changed, 6 insertions, 6 deletions
diff --git a/spec/primitive.md b/spec/primitive.md
index 38c06d00..7fb3cd83 100644
--- a/spec/primitive.md
+++ b/spec/primitive.md
@@ -145,7 +145,7 @@ The iteration modifiers `⌜¨⚇˘⎉` process elements or cells in index order
**Deshape** (`⥊`) differs from the provided function (which returns the element list of an array) only in that it accepts an atom, returning a one-element list containing it. **Reshape** (`⥊`) is extended in numerous ways. It accepts any list of natural numbers (including as a unit array or atom) for the left argument and any right argument; `𝕩` is deshaped first so that it is treated as a list of elements. These elements are repeated cyclically to fill the result array in ravel order. If `𝕩` is empty but the result is not, then the result consists of fill elements for `𝕩`. Furthermore, at most one element of `𝕨` can be a "length code": one of the primitives `∘⌊⌽↑`. In this case, a target length is computed from the number of elements in `𝕩` divided by the product of the other elements of `𝕨` (which must not be zero). If the target length is an integer then it is used directly for the length code. Otherwise, an error is given if the length code is `∘`, and the target length is rounded down if the code is `⌊` and up if it's `⌽` or `↑`. With code `⌽`, elements are repeated cyclically as usual, but with code `↑`, the extra elements after each argument element is used are fill values for `𝕩`.
-**Transpose** (`⍉`) reorders axes of its argument to place the first axis last; if the argument has one or fewer axes then it's returned unchanged. **Reorder Axes** (`⍉`) requires the left argument to be a list or unit of natural numbers, with length at most the rank of the right argument. This list is extended to match the right argument rank exactly by repeatedly appending the least unused natural number (for example, given `1‿3‿0‿0`, `2` is appended). After extension, it specifies a result axis for each axis of the right argument. There must be no gaps in the list: that is, with the result rank equal to one plus the greatest value present, every result axis must appear at least once. Now each argument axis is "sent to" the specified result axis: in terms of indices, `i⊑𝕨⍉𝕩` is `(𝕨⊏i)⊑𝕩` if `𝕨` is complete. If multiple argument axes correspond to the same result axis, then a diagonal is taken, and it's as long as the shortest of those argument axes. While Transpose does not enclose an atom right argument, Reorder Axes does, so that its result is always an array.
+**Transpose** (`⍉`) reorders axes of its argument to place the first axis last; if the argument has one or fewer axes then it's enclosed if it's an atom and otherwise returned unchanged. **Reorder Axes** (`⍉`) requires the left argument to be a list or unit of natural numbers, with length at most the rank of the right argument. This list is extended to match the right argument rank exactly by repeatedly appending the least unused natural number (for example, given `1‿3‿0‿0`, `2` is appended). After extension, it specifies a result axis for each axis of the right argument. There must be no gaps in the list: that is, with the result rank equal to one plus the greatest value present, every result axis must appear at least once. Now each argument axis is "sent to" the specified result axis: in terms of indices, `i⊑𝕨⍉𝕩` is `(𝕨⊏i)⊑𝕩` if `𝕨` is complete. If multiple argument axes correspond to the same result axis, then a diagonal is taken, and it's as long as the shortest of those argument axes. Like Transpose, Reorder Axes encloses `𝕩` if it's an atom, so that its result is always an array.
### Indices and selection
@@ -169,15 +169,15 @@ For **Select** (`⊏`), `𝕨` is an array of natural numbers, or a list of such
Monadic structural functions work on the first axis of the argument, so they require it to have rank at least 1. **Reverse** (`⌽`) reverses the ordering of the major cells of `𝕩`. **Nudge** (`»`) shifts them forward, removing the last and placing a major cell made up of fill elements at the beginning, while **Nudge Back** (`«`) does the same in the reverse direction, so it removes the first cell and places fills at the end. **Prefixes** (`↑`) and **Suffixes** (`↓`) each return lists with length one higher than `𝕩`, whose elements are arrays with the same rank as `𝕩`. For Prefixes, the element of the result at index `i` contains the first `i` major cells of `𝕩` in order, and for Suffixes, it contains all but these major cells.
-The remainder of the functions discussed in this section are dyadic. There are four functions for which `𝕨` is a list of whole numbers—but an atomic number or enclosed number is also permitted, and treated as a 1-element list—and its elements are matched with leading axes of `𝕩`. These functions independently manipulate each axis: one way to define such a process is to consider lists running along the axis, where every element of the index is fixed except one. A change to this axis retains the fixed indices, but can move elements from one location to another along the variable index, add fill elements, or split the axis into two axes. A change to a different axis can rearrange these lists along the original axis, but can't affect the placement of elements within them. In the reference implementations, working on leading axes is accomplished using the Cells (`˘`) modifier recursively, so that action on the first axes doesn't use Cells, on the next is affected by Cells once, then twice, and so on.
+The remainder of the functions discussed in this section are dyadic. For all of these, an atom value for `𝕩` is treated as an array by enclosing it before acting, so that the result is never an atom. There are four functions for which `𝕨` is a list of whole numbers—but an atomic number or enclosed number is also permitted, and treated as a 1-element list—and its elements are matched with leading axes of `𝕩`. These functions independently manipulate each axis: one way to define such a process is to consider lists running along the axis, where every element of the index is fixed except one. A change to this axis retains the fixed indices, but can move elements from one location to another along the variable index, add fill elements, or split the axis into two axes. A change to a different axis can rearrange these lists along the original axis, but can't affect the placement of elements within them. In the reference implementations, working on leading axes is accomplished using the Cells (`˘`) modifier recursively, so that action on the first axes doesn't use Cells, on the next is affected by Cells once, then twice, and so on.
**Rotate** (`⌽`) is the simplest of these four functions: each element of `𝕨` gives an amount to rotate the corresponding axis, where a rotation of `r` moves the element at index `i+r` to `i` when all indices are taken modulo the length of the axis. **Windows** (`↕`) splits each axis of `𝕩` that corresponds to an element of `𝕨` in two, so that the result has one set of axes corresponding to elements of `𝕨`, then another, then the unchanged trailing axes. The second set of axes has lengths given by `𝕨` (which must consist of natural numbers), while the first has lengths `s¬𝕨`, where `s` contains the lengths of leading axes of `𝕩`. Position `i` in the first set of axes and `j` in the second corresponds to `i+j` in the argument, so that fixing one of these positions and varying the other gives a slice of the argument. In both Rotate and Windows, the length of `𝕨` is at most the rank of `𝕩`.
-**Take** (`↑`) offers several possibilities. The absolute value of `𝕨` gives the final lengths of the axes in the result. It may be positive to indicate that the axis aligns with `𝕩` at the beginning, or negative to indicate it aligns at the end. A zero value gives no result elements, so there is no need to consider alignment. If the absolute value of an element of `𝕨` is smaller than or equal to the corresponding length in `𝕩`, then the first or last few elements are taken along that axis. If it is larger, then instead fill elements are added to the end (if positive) or beginning (if negative) to make up the difference in length. **Drop** (`↓`) gives `𝕨` a similar meaning, but excludes all elements that Take includes (maintaining the order of the retained ones). The result of Drop never uses fill elements. In a case where Take would use fill elements, it would include all positions from `𝕩`, so Drop should include none of them, and the result will have length `0` for that axis. Take and Drop are extended to allow an argument with length greater than the rank of `𝕩`. In this case leading length-1 axes are added to `𝕩` so that its rank matches `𝕨` before taking or dropping. If `𝕩` is an atom, it is always enclosed to an array first, even if `𝕨` is empty.
+**Take** (`↑`) offers several possibilities. The absolute value of `𝕨` gives the final lengths of the axes in the result. It may be positive to indicate that the axis aligns with `𝕩` at the beginning, or negative to indicate it aligns at the end. A zero value gives no result elements, so there is no need to consider alignment. If the absolute value of an element of `𝕨` is smaller than or equal to the corresponding length in `𝕩`, then the first or last few elements are taken along that axis. If it is larger, then instead fill elements are added to the end (if positive) or beginning (if negative) to make up the difference in length. **Drop** (`↓`) gives `𝕨` a similar meaning, but excludes all elements that Take includes (maintaining the order of the retained ones). The result of Drop never uses fill elements. In a case where Take would use fill elements, it would include all positions from `𝕩`, so Drop should include none of them, and the result will have length `0` for that axis. Take and Drop are extended to allow an argument with length greater than the rank of `𝕩`. In this case leading length-1 axes are added to `𝕩` so that its rank matches `𝕨` before taking or dropping.
**Replicate** (`/`) is similar to the four dyadic structural functions above, but `𝕨` gives a list of containing *lists* of natural numbers, or plain or enclosed natural numbers, instead of a simple list. If `𝕨` has depth less than `2`, it's considered to be a single value corresponding to one axis of `𝕩`, while if it has depth `2` then it's a list of values. If `𝕨` is the empty list `⟨⟩` then it is defined to be in the second case despite having a depth of `1`. On a single axis of `𝕩` the corresponding value `r` from `𝕨` is either a list or a unit: if it's a unit then it is repeated to match the length of that axis of `𝕩`, and if it's a list it must already have the same length as that axis. Each number in `r` now specifies the number of times to repeat the corresponding position in `𝕩`. This is equivalent to calling Indices on `r` and using the result for selection.
-**Shift Before** (`»`) and **Shift After** (`«`) are derived from Join To and share most of its behavior. The difference is that only a portion of the result of Join To is returned, matching the length of `𝕩`. This portion comes from the beginning for Shift Before and the end for Shift After. The only difference in conditions between the shift functions and Join To is that Join To allows `𝕩` to have smaller rank than `𝕨` while for shifting it must have the same or greater rank.
+**Shift Before** (`»`) and **Shift After** (`«`) are derived from Join To and share most of its behavior. The difference is that only a portion of the result of Join To is returned, matching the length of `𝕩`. This portion comes from the beginning for Shift Before and the end for Shift After. The only difference in conditions between the shift functions and Join To is that Join To allows the result to have higher rank than `𝕩`. Shifts do not, so the rank of `𝕩` be at least 1 and at least as high as `𝕨`.
### Searching
diff --git a/spec/reference.bqn b/spec/reference.bqn
index e59e8dc7..40cbefb9 100644
--- a/spec/reference.bqn
+++ b/spec/reference.bqn
@@ -281,7 +281,7 @@ _onAxes_←{
! 𝕨≤○≠≢𝕩
R←{(⊑𝕨)F(1 DropV 𝕨)⊸R˘𝕩}⍟{0<≠𝕨}
𝕨R𝕩
- }
+ }⟜ToArray
}
SelSub←{
@@ -419,7 +419,7 @@ ReorderAxes←{
𝕨↩𝕨∾𝕨(¬∘∊˜/⊢)↕r
(𝕨⊸⊏⊑𝕩˙)¨↕⌊´¨𝕨⊔≢𝕩
}
-Transpose←(=-1˙)⊸ReorderAxes⍟(0<=)
+Transpose←(0<=)◶⟨ToArray,(=-1˙)⊸ReorderAxes⟩
# Sorting
Cmp ← ∨○IsArray◶{ # No arrays