Sharpening your APL knife

Operators -3-

Spawn: background Processes

{R}←{X} f&Y

Composition (Form 1) -1-

{x}←fns1∘fns2 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:

Function assignment

Composition (Form 2) -1-

{x}←array1∘fns array2

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 

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


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

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

Power: Definition - Form 1

{R}←{X}(f⍣g)Y

Operand g is an integer scalar

Power: Definition - Form 2

{R}←{X}(f⍣g)Y

Operand g is a dyadic function

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