Sharpening your APL knife
Operators -3-
Spawn: background Processes
{R}←{X} f&Y
- & is used to start a background process in APL
- By far the easiest way to get into all sorts of trouble
- Not in the scope of this course
Composition (Form 1) -1-
{x}←fns1∘fns2 array
- fns1 may be any monadic function
- fns2 may be any monadic function that returns a result
- array must be appropriate to fns2
- The result of "fns2 array" must be appropriate to fns1
- Functions can be glued together either to be assigned to a name or "eached" against an array
Composition (Form 1) -2-
- Rank←⍴∘⍴
- Rank 2 3 4⍴1
- 3
- Mix←↓∘⍉∘↑
- p←('Gorge' 'Bill' 'Ronald')('Bush' 'Clinton' 'Reagan')
- ⍴⎕←Mix p
- Gorge Bush Bill Clinton Ronald Reagan
- 2
Composition (Form 1) -3-
Be very careful: can get easily hard to read:
Composition (Form 2) -1-
{x}←array1∘fns array2
- fns may be any dyadic function
- array1 may be any array that is appropriate as left argument to "fns"
- array2 must be appropriate as right argument to fns2
- Often used to glue an array as a left argument to a function which is then eached against an array
Composition (Form 2) -2-
- 2 2∘⍴¨array
- 'Clinton'∘≡¨array
- ⎕∘←∘⎕VR¨↓⎕NL 3 ⍝ List all fns in the workspace
Composition (Forms 3) -1-
{R}←(f∘B)Y
- f may be any dyadic function
- B: array appropriate as right argument to f
- Y: array appropriate as left argument to f
- The derived function is equivalent to:
- YfB
- Especially important for the search functions:
- ⍳ ∊ ∩ ∪ ~ and "matrix iota" (idiom)
Composition (Forms 3) -2-
{R}←YfB ←→ {R}←(f∘B)Y
- (∊∘⎕A)¨'This' 'And' 'That'
- 1 0 0 0 1 0 0 1 0 0 0
- We will come back to this later when it comes to hash tables and search functions
Composition (Forms 4) -1-
{R}←Xf∘gY
- f may be any dyadic function
- g may be any monadic function which returns a result
- Y: array appropriate to g
- result of gY must be appropriate as right argument to f
- X: array appropriate as left argument to f
- The derived function is equivalent to:
- XfgY
Composition (Forms 4) -2-
{R}←XfgY ←→ {R}←Xf∘gY
- 0,∘⍳¨⍳5
- 0 1 0 1 2 0 1 2 3 0 1 2 3 4 0 1 2 3 4 5
Composition Example -1-
- +/tfns¨100000⍴↓⎕NL 3 ⍝ ⎕fx 'r←tfns n' 'r←⊃⍴⎕CR n'
- 2.375
- +/{⊃⍴⎕CR⍵}¨100000⍴↓⎕NL 3
- 2.219
- +/⊃∘⍴∘⎕CR¨100000⍴↓⎕NL 3 ⍝ "Glue"
- 2.188
Commute -1-
L f⍨ R
- The commute operator effectively exchanges its operands
- Commute often eliminates the need for paranthesis
- As a result, expressions can be read from right to left
R f L ←→ L f⍨ R
Commute -2-
- (⊂'Clinton')∊'Bush' 'Clinton' 'Kennedy'
- ⍝ same with commute:
- 'Clinton'∊⍨∘⊂⍨'Bush' 'Clinton' 'Kennedy'
- OneOf←∊⍨∘⊂⍨
- ⍝ Assigned fns can improve readability...
- ⍝ ...rather than worsen:
- 'Clinton' OneOf 'Bush' 'Clinton' 'Kennedy'
- {0 1∊⍨≡⍵:⊂⍵ ⋄ ⍵}'I am simple'
- (nowadays a domain of the ⍣ operator)
Each (1) - Curse and Bless
{Result}←f¨ R
{Result}←L f¨ R
- We cannot live without "Each" nowadays
- But sometimes we should...
Each (2) - Examples
Get a text matrix with 10.000 rows and 1 column, each row containing digits
tm←⍕,[1.5]?10000⍴1000
- ⍝ transform to numbers with each:
- CPU'{}⍎¨↓tm'
- 7.047
- CPU'{}⍎,'' '',tm' ⍝ same without each
- 3.797
Each (3) - Tips
- More than 2 "Eaches" in a line: be on the alert!
- More than 4 "Eaches": something might be wrong
- More than 3 "Eaches": consider a dfns which can be "Eached" itself
- Consider also a :For loop: fast and traceable
- In case of processing large arrays: check CPU consumption
- The same holds true if a line is called very often
Power: Definition - Form 1
{R}←{X}(f⍣g)Y
Operand g is an integer scalar
- Function f is applied cumulatively g times to its argument
- If g is zero, function f is not applied at all, and R gets Y
- Useful to apply a function conditionally
Power: Definition - Form 2
{R}←{X}(f⍣g)Y
Operand g is a dyadic function
- g must return a boolean scalar
- Function f is applied cumulatively until g returns a zero
- I cannot think of any other function than = or ≡
Power Form 2: Sample -1-
- ⍝ Endless loop:
- ¯1∘+⍣=100
- ⍝ This introduces a stop condition:
- 0∘⌈∘¯1∘+⍣=100
Power Form 2: Sample -2-
- ⍝ Golden ration
- 1+∘÷⍣=1
- 1.618033989
End