This document is meant to list common operations that a programmer may need.
Test an array for a specific prefix
This function tests if the left argument is a prefix of the array in the right argument.
The examples below returns 1 because foo is indeed a prefix of fooabc.
Explicit form:
"foo" {↑ ⍺⍷⍵} "fooabc"
Tacit form:
"foo" (↑⍷) "fooabc"
This works because ⍷ returns a boolean array with the same dimensions of the argument to the left (i.e. the string that is searched).
This array contains a 1 at every position where the left argument is found as a subarray.
Then ↑ is used to get the first element of this array, since we’re only checking to see if the match is at the start of the array.
Note that since the return value of ⍷ is a lazy value, and we drop all but the first result, this function will only search the part of the string that is needed to produce the expected output.
Split an array by separator
Explicit form:
{(⍵≠@\s) ⊂ ⍵} "foo bar test"
Tacit form:
(@\s≠)⍛⊂ "foo bar test"
This expression compares each character with space (@\s) in order to create a bitmap that selects the groups of characters, and then uses the ⊂ function to split sequences of ones into groups.
Split an array by separator, without combining separators
The Split an array by separator solution groups sequences of separators together.
This makes sense if you split a string by spaces for example, but if you have empty groups, you may want to keep them.
In the following example, we split by the character | such that the following string ab|c|||de|f is split into the following: "ab" "c" "" "" "de" "f":
Explicit form:
{ ↓¨ { (@|=⍵) ⊆ ⍵ } @|,⍵} "ab|c|||de|f"
Tacit form:
(↓¨ (@|=)⍛⊆ @|,) "ab|c|||de|f"