Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vector of functions in APL

What is the syntax for a vector (array) of functions in APL?

I have tried the following but these are interpreted as a 3-train and a 2-train, respectively:

{1},{2}
{1} {2}

PS. I am looking to do this with more complex (and possibly named) functions by the way, the {1} above is just so the example is short.

like image 487
ᅙᄉᅙ Avatar asked Nov 28 '21 23:11

ᅙᄉᅙ


People also ask

What do the symbols mean in APL programming?

APL programmers often assign informal names when discussing functions and operators (for example, product for ×/) but the core functions and operators provided by the language are denoted by non-textual symbols. Most symbols denote functions or operators.

What are the functions and operators in APL?

APL programmers often assign informal names when discussing functions and operators (for example, product for ×/) but the core functions and operators provided by the language are denoted by non-textual symbols. Most symbols denote functions or operators. A monadic function takes as its argument the result of evaluating everything to its right.

What is a primitive function in APL?

Built-in APL functions (or 'primitive' functions) are denoted by symbols (such as +-÷×). Primitive functions can be either monadic(which means they take a single right argument), or dyadic(in which case they take an argument on the left and an argument on the right). The same symbol may have both monadic and dyadic forms. Execution order

What is an a line of APL?

A line of APL may consist of several functions, and arguments. There are therefore two points you must bear in mind: Expressions are evaluated from right to left. The results of one function become the argument of the next function. This simple example illustrates both of these points: 50×2-150


2 Answers

Arrays of functions can be obtained using ⎕OR (Object Representation) and the fact that such objects implicitly become reconstituted into functions when used as operands. (You can also do it explicitly using ⎕FX.) It is easiest to define some helper operators first:

      _Arrayify←{f←⍺⍺ ⋄ ⎕OR'f'}
      _Apply←{2=⎕NC'⍺':⍺ ⍺⍺ ⍵ ⋄ ⍺⍺ ⍵}

Now, let's define some sample functions:

      A←{2×⍵}
      B←{⍵÷2}
      C←{-⍵}

We create a 3-element vector of functions, and check that it is indeed a "normal" array:

      fnArray←(A _Arrayify⍬)(B _Arrayify⍬)(C _Arrayify⍬)
      ⍴fnArray
3

Let's extract the second function and apply it:

      (2⊃fnArray)_Apply 10
5

We can also create an application function, so we can use operators on it:

      Apply←{⍺ _Apply ⍵}
      fnArray Apply¨10
20 5 ¯10
like image 134
Adám Avatar answered Oct 20 '22 05:10

Adám


Dyalog APL does not officially support function arrays, you can awkwardly emulate them by creating an array of namespaces with identically named functions.

      (a←⎕NS⍬).f←2∘×
      (b←⎕NS⍬).f←÷∘2
      (c←⎕NS⍬).f←-
      f←(a b c).f
      f 4
8 2 ¯4

Maria Wells suggested an operator to produce an array of functions

like image 5
awagga Avatar answered Oct 20 '22 05:10

awagga