Passing a User-Defined Function as a Argument

Learning APL or new to Dyalog? Ask "silly" questions here, without fear...

Passing a User-Defined Function as a Argument

Postby REMINGTON30 on Wed Oct 18, 2017 8:30 pm

Consider a dyadic user-defined function x Dy y which produces a result. Is there a way to pass the function Dy to another function as an argument? For example, if a monadic function Gg F1 requires function Dy to operate, is there a way to put Dy in the argument F1?
REMINGTON30
 
Posts: 22
Joined: Fri Aug 11, 2017 2:17 pm

Re: Passing a User-Defined Function as a Argument

Postby Phil Last on Thu Oct 19, 2017 2:22 pm

To answer your question directly, what you could do is create a namespace
      ns←⎕NS'Dy'
and pass the namespace as an argument or a part of an argument to Gg. Within Gg you could then call
      arg.Dy
A problem is that if Dy had dependencies then inside the namespace above represented by arg there would be none of them. No doubt there are even more perverse ways to do it but the straightforward APL way is to make Gg an operator rather than a function. Operators take functions as arguments (operands) which can be called from within 'though they actually operate in the environment where they reside.
      .
 Gg←{
   ...
   z←⍺⍺ ⍵ ⍝ runs Dy
   ...
 }
      Dy Gg F1
User avatar
Phil Last
 
Posts: 628
Joined: Thu Jun 18, 2009 6:29 pm
Location: Wessex

Re: Passing a User-Defined Function as a Argument

Postby REMINGTON30 on Fri Oct 20, 2017 4:21 pm

Thanks Phil. I will read the Dyalog documentation on user-defined operators and try that method.
REMINGTON30
 
Posts: 22
Joined: Fri Aug 11, 2017 2:17 pm


Return to New to Dyalog?

Who is online

Users browsing this forum: No registered users and 1 guest