APLX Help : Help on APL language : APL Fundamentals : Operators
|
|
![]() |
User-defined Operators |
An 'operator' modifies the behaviour of a primitive or user-defined function. It has an operand or operands that are primitive, derived or user-defined functions or data. The result of using an operator is known as a derived function which can then be applied monadically or dyadically to data or alternatively it may be, in turn, used as an argument to another operator. As well as the primitive (built-in) operators, user-defined operators are permitted. These are created an edited in the same way as user-defined functions (using the Header line for user-defined operatorsThe format for an operator header follows one of the following forms, where R←(LO OPERATOR) B (Monadic operator with one argument) or R←A (LO OPERATOR) B (Monadic operator with two arguments) or R←(LO OPERATOR RO) B (Dyadic operator with one argument) or R←A (LO OPERATOR RO) B (Dyadic operator with two arguments) User-defined operators need not return explicit results. ExampleThis simple monadic operator with two arguments COMMUTE reverses the arguments of a function. In this example, FN represents the function (the left operand) which will be combined with the operator to make a derived function, L represents the left argument supplied to the derived function, and R represents the right argument supplied to the derived function: ∇Z←L (FN COMMUTE) R [1] ⍝ Operator which reverses the arguments to a dyadic function [2] Z←R FN L ∇ 100 ÷ 3 33.33333333 100 ÷ COMMUTE 3 ⍝ Equivalent to 3 ÷ 100 0.03 100 ⍴ COMMUTE 3 ⍝ Equivalent to 3 ⍴ 100 100 100 100 100 ⎕DR COMMUTE 1 ⍝ Equivalent to 1 ⎕DR 100 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 0 0 Using data as operandsThe left and/or right operands to a user-defined operator do not have to be functions; they can alternatively be arrays. The effect is to substitute the supplied array for in expression which references the operand: 'FRUIT' ('OLD' COMMUTE) 'HELLO' HELLO OLD FRUIT |
|
APLX Help : Help on APL language : APL Fundamentals : Operators
|
Copyright © 1996-2010 MicroAPL Ltd