diff options
Diffstat (limited to 'doc')
| -rw-r--r-- | doc/find.md | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/doc/find.md b/doc/find.md new file mode 100644 index 00000000..b91a5930 --- /dev/null +++ b/doc/find.md @@ -0,0 +1,41 @@ +*View this file with results and syntax highlighting [here](https://mlochbaum.github.io/BQN/doc/find.html).* + +# Find + +Find (`β·`) searches for occurrences of an array `π¨` within `π©`. The result contains a boolean for each possible location, which is 1 if `π¨` was found there and 0 if not. + + "xx" β· "xxbdxxxcx" + +More precisely `π¨` needs to [match](match.md) a contiguous selection from `π©`, which for strings means a substring. These subarrays of `π©` are also exactly the cells in the result of [Windows](windows.md). In fact we can use Windows to see all the arrays `π¨` will be compared against. + + 2 β "xxbdxxxcx" + + "xx"βΈβ‘Λ 2 β "xxbdxxxcx" + +Like Windows, the result usually doesn't have the same dimensions as `π©`. This is easier to see when `π¨` is longer. It differs from APL's version, which includes trailing 0s in order to maintain the same length. Bringing the size up to that of `π©` is easy enough with [Take](take.md) (`β`), while shortening a padded result would be harder. + + "string" β· "substring" + + "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. + + "loooooong" β· "short" + + 9 β "short" + + β "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 `π©`. + +### Higher ranks + +If `π¨` and `π©` are two-dimensional then Find does a two-dimensional search. The cells used are also found in `π¨β’βΈβπ©`. For example, the bottom-right corner of `π©` below matches `π¨`, so there's a 1 in the bottom-right corner of the result. + + β’ a β 7 (4|βΛ)βββ 9 # Array with patterns + + (0βΏ3βΏ0β0βΏ1βΏ0) β· a + +It's also allowed for `π¨` to have a smaller rank than `π©`; in this case leading axes of `π©` are mapped over so that axes of `π¨` correspond to trailing axes of `π©`. This is a minor violation of the [leading axis](leading.md) principle, which would match axes of `π¨` to leading axes of `π©` in order to make a function that's useful with the Rank operator, but such a function would be quite strange and hardly ever useful. + + 0βΏ1βΏ0βΏ1 β· a |
