The & Meta-Function (Alternative Input / Output Specification)
The traditional way to specify the number of input arguments to pass to a function is to use the $ meta-function
2$: % Two-input version of :
Similarly, to specify the number of output arguments you can use the # meta-function specifying either the number of output arguments,
2#S % Two-output version of sort
or if you pass a number that is greater than the number of output arguments defined for a function, only the mod(N, numberOfOutputs) + 1 output is supplied.
4#S % Get only the second output of sort
You can additionally specify a logical array as the input to # to retrieve only specific output arguments.
TFT#u % Three output version of unique and discard the second output
All of these input / output specifications are handy but they drive up your byte-count very quickly. To deal with this, MATL introduced the & meta-function in the 17.0.0 release. This & meta-function acts as a shortcut for a particular input or output specification for a function. Let's see what that means.
In our example above, we wanted to use the two-input version of : (creates a vector of equally-spaced values). While the default number of input arguments to : is 1 (creates an array from [1...N]), it is very common that a user would want to specify the start value of the range which requires the second input. So for :, we have defined & to be a shortcut for 2$.
10 % Push 10 to the stack
12 % Push 12 to the stack
2$: % Create an array: [10, 11, 12]
Now becomes the following, saving a byte!
10 12 &:
How can we determine what the alternate number of arguments is?
The input / output specification that & translates to is function specific such that we optimize the byte-savings.
The input / output argument section of the help description for each function has been updated to indicate what this alternative number of inputs / outputs is (if any). The possible number of input or output arguments are displayed as a range and the default values for each are shown in parentheses. The input / output spec that can be substituted with & is shown after the / character within the parentheses.
Here is the input / output argument section of the help description for :
+- Min-Max range of # of inputs
| +----- Alt. Default # of inputs
| |
V V
1--3 (1 / 2); 1 <--- Possible / Default # of outputs
^
|
Default # of inputs
How did you determine what & means for each function?
Very carefully. Using the StackExchange API, we were able to download all MATL answers that have ever been used in a PPCG challenge. By parsing each of the answers, we were then able to determine the frequency with which each input / output spec was used for each function. Using this information we were then able to objectively identify the input / output specification the & meta-function should represent for each function. Sometimes there was no clear winner, so many functions currently don't have & defined.
Here is the script we used (unfortunately it's written in MATLAB and not MATL).
And here is an example of the histogram of $/# usage
5
It is definitely a great advantage if you know some Matlab/Octave. Some tricks from Tips for golfing in Matlab and Tips for golfing in Octave used in MATL too.
– flawr – 2016-04-22T06:34:17.790Suggestion: it looks like
accumarray(XQ) can be quite powerful (possibly even more than in MATLAB/Octave since those length function handles have handy numeric codes), but I don't know it well enough to illustrate with good examples. If it actually is useful, could someone create an answer with ideas on how to use it? – sundar - Reinstate Monica – 2018-07-10T14:22:31.530