A+ Reference: Monadic Operators

# Monadic Operators

Common Error Reports
Definitions of Monadic Operators

Apply g¡x and y g¡x
Bitwise f®x and y f®x

Bitwise Cast y ©®x

Table: Shape of the Result of Bitwise Cast

Each f¡x and y f¡x
Reduce f/x

Table: Identity Scalars for Reduction

Scan f\x

# Common Error Reports

> Multiple errors elicit but one report. If an error report in the following list is issued, then the ones preceding it do not apply. Five reports are common to all monadic operators:

> - parse;
> - value: an argument has no value;
> - nondata: an argument (not an operand) is a function or some other nondata object;
> - an error report from the operand function;
> - wsfull: the workspace is currently not large enough to execute the function in; a bare left arrow (û), which dictates resumption of execution, causes the workspace to be enlarged if possible;
> - interrupt (not an error): the user pressed c twice (once if A+ was started from a shell) while holding the Control key down.

# Definitions of Monadic Operators

## Apply g¡x and y g¡x

Arguments

> g is a function scalar; that is, g is <{f} for some function f (other than Assignment). The arguments y and x are suitable arguments for f.

Definition

> The function f described in the arguments section is evaluated, as f x in the monadic case or as y f x in the dyadic case. Indeed, although the two infix forms are shown for definiteness, f{...} is evaluated for any valence of f, zero through nine.

> Warning! If instead of a function scalar you give a function expression, (e.g., instead of (+;-;«)[1]¡x or (<{f})¡x you give -¡x or f¡x), you will get a wrong result, namely, the result for the Each operator.

Additional Error Report

> The following report is issued only if there is no parse or value error (see "Common Error Reports"):

> - a nonfunction error is reported if d is not a function scalar (or function expression: see the warning in the definition section); d precedes the colon in the error report.

Examples

>          fnsû(«;ß;Ò)   ã Equivalent to <{«},<{ß},<{Ò}

>          2 3 fns[0]¡ 4
>      8 12

>          2 3 fns[1]¡ 4
>      0.5 0.75

>          2 3 fns[2]¡ 4
>      4 4 4
>      4 4 4

## Bitwise f®x and y f®x

Arguments

> The operand f is Cast, And, Or, Not, or one of the six relational functions (< ¤ = ¦ > ¨). The arguments y and x are suitable arguments for f.

Definition

> The Bitwise operator applies Not to each bit of the argument and applies all the other functions except Cast to each pair of corresponding bits of the arguments.

> The derived function Bitwise Not is a monadic scalar function and all the others except Bitwise Cast are dyadic scalar functions, obeying the usual rules of conformability.

> Examples for Bitwise Not, And, and Unequal

> ~® ¢16 15 ¢123 122
> 15 ¢16 122 ¢123 ã One's complement.

>          5 ^® É10
>      0 1 0 1 4 5 4 5 0 1

>          5 ¨® É10
>      5 4 7 6 1 0 3 2 13 12

> And and Or, without the Bitwise operator, give boolean results for all valid arguments - see above.

> Bitwise Cast ©®x

> Bitwise Cast leaves the data part of the variable unchanged but changes the type indicator and (usually) shape. The conversion of symbol to character, for example, is quite different from plain Cast: for a symbol, the data part is a four-byte pointer, which may differ from process to process but is guaranteed unique and invariant within a process. The data part of a character is, of course, its ASCII code.

> The left argument can be `int, `float, or `char; a Bitwise Cast to `sym is not allowed since not all integers are valid symbols, i.e., not all point to symbols. The type of the right argument can be of any of the four.

> Since scalars of various types are represented using various numbers of bytes, the length of the last dimension of the result may differ from that of the right argument. There is no padding or truncation. If rb bytes are required to represent the result type and ab to represent the right argument type and rb>ra, then the last dimension of the right argument must be divisible by rbßab. The table below exhibits the relation between the shape of the right argument and that of the result.

> The rank is changed only when a scalar is cast bitwise to a type that is represented in fewer bytes.

Shape of the Result of Bitwise Cast, for Argument Shape s
| From `int | From `float | From `char | From `sym
--- | --- | --- | --- | ---
To `int | s | (¢1Õs), 2«¢1Ù1,s | (¢1Õs), (¢1Ùs)ß4 | s
To `float | (¢1Õs), (¢1Ùs)ß2 | s | (¢1Õs), (¢1Ùs)ß8 | (¢1Õs), (¢1Ùs)ß2
To `char | (¢1Õs), 4«¢1Ù1,s | (¢1Õs), 8«¢1Ù1,s | s | (¢1Õs), 4«¢1Ù1,s

> Examples for Bitwise Cast

>          `int ©® `
>      4163890

>          `int ©® 1.2 3.4
>      1072902963 858993459 1074475827 858993459

>          `char © `int ©® 'abcdefgh'
>     dh  ã Plain Cast converts just the last byte of an integer to character

>          `char ©® `int ©® 'abcdefgh'
>     abcdefgh

>          `int ©® 'ab'
>     ã[error]  ©®: length  ã The shape is 2, which is not divisible by 4.

Additional Report

> - An "undefined" token report is issued if f is not one of the functions listed above.

## Each f¡x and y f¡x

Arguments and Result

> f is any function expression (so Assignment is excluded, as it is for all operators). The result is nested. In the monadic case, the shape of the result is the shape of x. In the dyadic case, the shapes of the arguments and result follow the rules for dyadic scalar functions (see "Application, Conformability, and Result Shape"), except that only scalars are extended, not one-element arrays in general, and nonscalar arguments of differing ranks elicit a rank error report.

Definition

> The derived function, f¡, is a scalar function.

> In the case of monadic f:

>          (i#f¡x) ûý < f >i#x

> for every valid index i. That is, each element of x is selected, f is applied to the Disclose of the selected element, and the result of f is enclosed and inserted in the result of f¡. (See "Disclose" for the definition of > and "Enclose" for <.) f¡x is equivalent to (f Each)x for the following defined operator Each:

>          (f Each) x: {
>               sûÒx;
>               xû,x;
>               zû(Òx)Ò<();
>               (iûÒz) do z[i]û< f >x[i];
>               sÒz}

> In the dyadic case, if x and y have the same shape, then

>          (i#y f¡x) ûý <(>i#y)f>i#x

> for every valid index i. If x has one element, then

>          (i#y f¡x) ûý <(>i#y)f 0Øx

> for every valid index i. (See "Pick" for the definition of Ø.) The definition is similar if y has one element, or both have one element.

> Warning! If instead of a function expression you give a function scalar, (e.g., instead of -¡x or f¡x you give (+;-;«)[1]¡x or (<{f})¡x), you will get a wrong result, namely, the result for the Apply operator.

Additional Error Reports

> Each of the following reports is issued only if there is no parse or value error (see "Common Error Reports") and none of the reports preceding it on this list applies:
> - a nonfunction error is reported if d is not a function expression (or function scalar: see the warning in the definition section); d precedes the colon in the error report;
> - a rank error is reported, when the derived function is dyadic, if neither argument is a one-element array and their ranks differ;
> - a length error is reported, when the derived function is dyadic, if neither argument is a one-element array and their shapes differ.

Examples

> In the first expression, the Each operator is applied to a monadic function (Shape) to give the rank of a scalar, a vector, and a matrix. In the second, it is applied to a monadic (Count, to find the vector lengths) as well as to a dyadic (Take, to pad the vectors to equal lengths for the principal function, Disclose); notice the other use of Disclose to make the Count Each result simple for Max Reduce, which finds the greatest length.

>          ØÒ¡Ò¡ (2;É2;É2 3)
>      0 1 2

>          (Ó/>#¡vs)Ù¡vsû('ab';'----';'cde')
>     ab
>     ----
>     cde

## Reduce f/x

Arguments and Result

> f is one of +, «, Ä, Ó, ^, ©. The argument x is any array whose items are suitable left and right arguments of f. The shape of the result is 1ÕÒx.

Definition

> If #x is at least two, i.e., if x has at least two items, then f/x is defined to be:

>          x[0] f x[1] f ... f x[¢1+#x]

> If #x is one, then f/x is x; while if #x is zero, then f/x is (1ÕÒx)Òidentity, where identity is a scalar that depends on f, and whose type for + and « depends on x. See the table "Identity Scalars for Reduction".

> As pointed out in "Operators and Derived Functions", Reduce is not in the strictest sense an operator, but for most purposes it can be regarded as one.

> Identity Scalars for Reduction
> Function | Identity
> --- | ---
> + | 0
> « | 1
> Function | Identity
> --- | ---
> © | 0
> ^ | 1
> Function | Identity
> --- | ---
> Ó | ¢Inf
> Ä | Inf

Additional Error Reports

> If there is no error reported as a parse error then a similar error may be reported:

> - a token error is reported if f is a primitive function or operator symbol but not one of +«ÄÓ^©.
> The following report is issued only if there is no parse, token, or value error (see "Common Error Reports"):

> - a valence error is reported if f is a defined function (this report seems worth mentioning here, although it actually comes from Replicate - which of course also produces error reports and results when f is a variable).

Examples

>          +/3 5 ¢2
>      6

>          3+5+¢2
>      6

>          Ó/É6 2
>      10 11

>          +/É0
>      0

## Scan f\x

Arguments and Result

> f is one of +, «, Ä, Ó, ^, ©. The argument x is any array whose items are suitable left and right arguments of f. The shape of the result is the same as the shape for x.

Definition

> If #x is at least one, i.e., if x has at least one item, then:

>          (f\x)[i] ûý f/x[Éi+1]

> for every valid scalar index i. That is, Scan produces a sequence of moving reductions. If #x is zero, then f\x is empty, and its type is integer for ^ and © and depends upon x for the rest.

> As pointed out in "Operators and Derived Functions", Scan is not in the strictest sense an operator, but for most purposes it can be regarded as one.

Additional Error Reports

> If there is no error reported as a parse error then a similar error may be reported:

> - a token error is reported if f is a primitive function or operator symbol but not one of +«ÄÓ^©.
> The following report is issued only if there is no parse, token, or value error (see "Common Error Reports"):

> - a valence error is reported if f is a defined function (this report seems worth mentioning here, although it actually comes from Expand - which of course also produces error reports and results when f is a variable).

Examples

>          +\3 5 ¢2
>      3 8 6

>          Ä\É3 4
>      0 1 2 3
>      0 1 2 3
>      0 1 2 3

>          Ó\É3 4
>       0  1  2  3
>       4  5  6  7
>       8  9 10 11