Sharpening your APL knife

Dynamic Functions

Dynamic Functions in general -1-

Note: There are also dynamic operators which are not covered by this course

Dynamic Functions in general -2-

Any :ControlWords must not appear in a dfns. As a consequence, a dfns cannot...

Note: There are also dynamic operators which are not covered by this course

Dynamic Functions in general -3-

Scope in tfns

Dynamic Scope

In a tfns, a local variable is "visible" from a called function (semi-global)

Scope in dfns

Static Scope

In a dfns, a local variable is not visible from a called function

Simplest possible Dynamic Functions

The simplest possible dynamic function:

{}

"Where" with dnfs

Where←{⍵/⍳⍴⍵}

Optional left argument

  •   Optional←{⍺←'Default' ⋄ ⍺}
  •   Optional ⍬
  • Default
  •   'Override' Optional ⍬
  • Override

Guards -1-

Guards are the :If-aquivalent of dfns:

  • Value←{
  • ⍵<0:'Negative'
  • ⍵>0:'Positive'
  • 'Is Zero'
  • }

Guards -2-

Use this technique to implement :if-:Else with dfns:

  • LargeDfns←{
  • value←{⍵<0:'Negative' ⋄ ⍵>0:'Positive' ⋄ 'Is Zero'}⍵
  • ...
  • }

Error Guards

  • Exec←{
  • 6::'Unknown'
  • ⍎⍵}

Applications -1-


{F.lv.SetColSize ⍵ ¯3}¨⍳5

Try to do this without a dfns!

Dfns: Pros -1-

n←(⊃¨1⊃¨ind[2]⊃¨ip[b])(⊃¨2⊃¨ind[2]⊃¨ip[b])

Dfns: Pros -2-

n←(⊃¨1⊃¨ind[2]⊃¨ip[b])(⊃¨2⊃¨ind[2]⊃¨ip[b])

Dfns: Performance

Given a vtv with a total of one million characters, what is supposed to be the fastest solution?

  • +/∨/¨' wiki'∘⍷¨buffer
  • +/{∨/'wiki'⍷⍵}¨buffer
  • :For this :In buffer ⋄ {}+/∨/'wiki'⍷this ⋄ :EndFor

See "#.dfnsPerformance.test" in the "Author" workspace

Dfns: Examples -1-

  • path←'c:\MyDocs\Special'
  • files←'one.html' 'two.jpeg' 'three.doc'
  • (⊂path,'\'),¨files
  • path∘{⍺,'\',⍵}¨files
  • {⍵,'\',⍺}∘path¨files

Recursive calls


  • recursive←{
  • 1<≡⍵:∇¨⍵
  • do_something ⍵
  • }

Tail calls -1-


  • (⍺×⍵)fact ⍵-1       ⍝ Tail call on fact.
  • ⍵×fact ⍵-1          ⍝ Embedded call on fact.

Tail calls -2-

Tails Calls...

Demonstrate the "#.sieve.test*" functions in the "Author" workspace

Dfns: Variables

Every assignment in a dfns creates a new variable


Exceptions are:

Don't forget this when dealing with very large variables!

Dfns: Cons

As everything else in APL, dfns can easily be abused

If misapplied, dfns can be...

The arguments of dfns have fixed names: explain them in Detail if not absolutely clear from the context

Dfns: Advice

  • dfns1 dfns2 dfns3 dfns4 dfns4¨LARGE_Data

Dynamic Operators

Not in the scope of this course

End