Sharpening your APL knife

Operators -1-

Operators in general

Reduce definition

Result←f/R

Reduce and empty arrays -1-

  •   ⍴f/0 3 6⍴⍳36
  • 0 3
  •   ⍴f/2 0 6⍴⍳36
  • 2 0

Reduce and empty arrays -2-

Result←f/2 3 0⍴⍳6

  •   ⎕←+/2 3 0⍴⍳6
  • 0 0 0
  • 0 0 0

Reduce: specialties

Result←f/array

If the last axis of "array" is 1, the last axis is simply removed

If "array" is a scalar (or a one-element-vector or...), "Result" gets "array"

Do one really need to know this?

As it will turn out soon, the answer is yes!

Prototype and reduce

  •   array←2 3 4⍴⍳12
  •   ind←{⍵/⍳⍴⍵}0>⍳¯1↑⍴array
  •   ⍴array[;;ind]
  • 2 3 0
  •   ⍴⎕←+/array[;;ind]
  • 0 0 0
  • 0 0 0
  • 2 3

Reduce and its applications

  • ∧/
  • ∨/
  • ⌈/
  • ⌊/
  • ,/
  • ⊃{}/

Reduce Applications: Example -1-

  •   p←('George' 'Bush')('Bill' 'Clinton')
  •   ⊃¨{'Mr ',⍵,', ',⍺}/¨p
  • Mr Bush, George  Mr Clinton, Bill 

Reduce Applications: Example -2-

  •   p←⊂('George' 'Bush')
  •   ⊃¨{'Mr ',⍵,', ',⍺}/¨p
  • Mr Bush, George
  •   p←'Bush' 'Clinton' 'Reagan'
  •   {'"',⍺,'", "',⍵,'"'}/p
  • "Bush", ""Clinton", "Reagan""     ⍝ !
  •   p←⊂'Bush'
  •   {'"',⍺,'", "',⍵,'"'}/p
  • Bush

Reduce N-Wise -1-

  •   v←2 8 6 7 3 5 9 10 1 4
  •   2>/v     ⍝ defines a "gliding window"
  • 0 1 0 1 0 0 0 1 0
  •   3+/v
  • 16 21 16 15 17 24 20 15

Reduce N-Wise -2-

  •   p←'Bush' 'Clinton' 'Reagan'
  •   {'"',⍺,'", "',⍵,'"'}/p
  • "Bush", ""Clinton", "Reagan""     ⍝ !
  •   2{'"',⍺,'", "',⍵,'"'}/p
  • "Bush", "Clinton", "Reagan"
  •   ⍴2{'"',⍺,'", "',⍵,'"'}/1⍴p
  • 0   ⍝ !!!

Reduce N-Wise Applications

  •   v←2 8 6 7 3 5 9 10 1 4 5 6 7 8 5 4 0 3
  •   ¯2-/v ⍝ First difference
  • 6 ¯2 1 ¯4 2 4 1 ¯9 3 1 1 1 1 ¯3 ¯1 ¯4 3
  •   12+/v  ⍝ yearly running total
  • 66 71 71 70 67 64 62

Proper Solution for the Quotes-Problem

  •   p←'Bush' 'Clinton' 'Reagan'
  •   beautify←{⍺,', ',⍵}
  •   addQuotes←{'"',⍵,'"'}
  •    ⊃beautify/addQuotes¨p
  • "Bush", "Clinton", "Reagan"
  •    ⊃beautify/addQuotes¨2⍴p
  • "Bush", "Clinton"
  •    ⊃beautify/addQuotes¨1⍴p
  • "Bush"

Dynamic FNS + Reduce: list

  •   n←'George' 'Dan' 'Tim' 'Nils'
  •   list←{⍺,', ',⍵}
  •   ≡⎕←⊃list/n
  • George, Dan, Tim, Nils
  • 1
  •   ≡⎕←⊃list/n[1]
  • George
  • 1

If the array is a scalar, the derived function is not applied!

Dynamic FNS + Reduce: list

  •   list←{⍺,', ',⍵}
  •   ⊃list/''
  • DOMAIN ERROR
  •   ⍝ Guards to the rescue:
  •   list←{0∊⍴⍵:'' ⋄ ⊃{⍺,', ',⍵}/⍵}
  •   ⍴list ''
  • 0

Mention diamonds

Dynamic FNS + Reduce: pairs

  • n←⊂'Firstname' 'George'
  • n,←⊂'Surname' 'Bush'
  • n,←⊂'Mark'  6
  • pair←{⍺,'←',{82=⎕dr ⍵:'''',⍵,'''' ⋄ ⍕⍵}⍵}
  • ⍎¨⊃¨pair/¨n

Creates three variables: Firstname, Surname and Mark

Scan definition

Result←f\R

Scan definition -2-

Result←f\array

  • Result[1] ←→ 1↑[⍴⍴array]array
  • Result[2] ←→ f/2↑[⍴⍴array]array
  • Result[3] ←→ f/3↑[⍴⍴array]array
  • ...
  • Result[⍴Result] ←→ f/n↑[⍴⍴array]array

Scan and its applications

  • ∨\
  • ∧\
  • ⌈\
  • +\
  • ×\
  • ≠\
  • ,\
  • ⊃{}\

Scan sample -1-

  •   +\1 2 3 4
  • 1 3 6 10
  •   <\1 0 1
  • 1 0 0
  •   ,\,¨'abcd'
  • a  ab  abc  abcd

Any idea why the last example says ,¨ ?

Scan sample -2-

  •   array←'abc(de)f(g)hi'
  •   array∊'()'
  • 0 0 0 1 0 0 1 0 1 0 1 0 0
  •   ≠\array∊'()'
  • 0 0 0 1 1 1 0 0 1 1 0 0 0
  •   {({⍵∨≠\⍵}⍵∊'()')/⍵}array
  • (de)(g)
  •   {(({⍵∨≠\⍵}⍵∊'()')/⍵)~'()'}array
  • deg
  •   ⍝ {'()'~⍨⍵/⍨{⍵∨≠\⍵}⍵∊'()'}array  ⍝ readability?!

Scan sample -3-

  • Between←{
  • ⍺←'<>'
  • ⍵/⍨{~⍵∨≠\⍵}⍵∊⍺}

Get the source code of a valid HTML page and show the effect of Between.

Discuss also the disadvantages of this solution if the HTML is invalid.

User-defined Operators -1-

User-defined Operators -2-

  • numbers←?1000⍴1000
  • numbers←500000⍴numbers
  • ⍕¨numbers
  • ⍕ Each numbers ⍝ supposed to be faster!

See #.EachOp in the "Author" workspace

End