Function as a display form of a namespace

General APL language issues

Function as a display form of a namespace

Postby OSRK on Mon Mar 05, 2018 12:18 pm

Is it possible to use a function as a display form of a namespace, to make it showing some meaningful representation of current content of the namespace?
Posts: 5
Joined: Tue Dec 27, 2016 8:48 am

Re: Function as a display form of a namespace

Postby Michael|Dyalog on Mon Mar 05, 2018 7:16 pm

From your question it is not totally obvious in which context you need this fn...i.e. is it for your own interactive explorations of the ws, are u thinking of a fn to use in your app etc.?

Sorry if this sounds too basic, but you've already found WS Explorer for this, right?

The cases where I wanted a quick way to get info about the cntents of a ns, I was concerned with the variables in the ns and their values...and a nice shortcut to do this is by just doing "⎕JSON ns". This gives you a JSON-Representation of the contents of the ns, this is nice and easy if you have a few text-variables or simple numerics...but if there a many large matrices in the ns, it looses its appeal ;-)

If you are interested in fns, vars etc., the user-command ]map might be interesting. Just do a "]?map" for some doc on its usage and "]map ns" to apply it on a ns.
Posts: 8
Joined: Thu Mar 02, 2017 4:29 pm

Re: Function as a display form of a namespace

Postby Morten|Dyalog on Tue Mar 06, 2018 7:30 am

I'm guessing your questions is related to the fact that ⎕DF only allows you to set an array as the "display form" for a namespace. In version 17.0 you will be able to get a little closer to what you want with the extension to trigger functions to allow them to trigger on any change in a namespace:

Code: Select all
:Namespace MyNs                                     
    ∇ setdisplay           
      :Implements Trigger *
      ⎕DF 'x is now: ',⍕x   

x is now: 99
User avatar
Posts: 316
Joined: Tue Sep 09, 2008 3:52 pm

Re: Function as a display form of a namespace

Postby OSRK on Tue Mar 06, 2018 10:55 am

Michael|Dyalog, the idea is to watch a "value" (a representation, defined by myself) of a namespace while tracing. I want to press Shift+Enter on a namespace, and see its "value". Then continue tracing, and watch how it changes.

WS Explorer is not suitable for this, as it requires a lot of clicks, and allows to watch one property only.

When I try "]map", I get VALUE ERROR:

      Dyalog APL/W-64 Version 16.0.31811
Unicode Edition

]map ns


Morten|Dyalog, this looks promising, thank you. Although, I would expect the representation calculation to be called only when the representation is actually requested, instead of being updated on every change of the namespace, to not to compromise performance. But even this solution could be enabled in dev mode only, not affecting real-world performance.
Posts: 5
Joined: Tue Dec 27, 2016 8:48 am

Re: Function as a display form of a namespace

Postby Adam|Dyalog on Sun Mar 11, 2018 5:21 pm

Here is another idea: Make your namespace into a class (and make everything "public shared" so it acts like a normal namespace). You can then define a "default property". The result of the property's getter function will be become the data for squad and bracket indexing instead of the ref itself:
      ↑⎕SRC cl
:Class cl
:field public shared x←42
:field public shared foo←'bar'
:field public shared baz←42
:property default df
:access public shared
∇ df←Get
df←↑('x is 'x)('foo is 'foo)('baz is 'baz)

│x is │42 │
│foo is │bar│
│baz is │42 │

Try it online!
User avatar
Posts: 26
Joined: Thu Jun 25, 2015 1:13 pm

Re: Function as a display form of a namespace

Postby petermsiegel on Sun Mar 11, 2018 11:37 pm

Regarding your (Morten's) namespace example, I believe the trigger code works already in version 16, unless I'm missing something. See Version 16 Release Notes, p. 26.

As a tweak of your v. 17 code, I used this code in 16, simply adding a right argument I don't reference (the trigger is simply and quietly ignored if the right argument is missing, at least in the Mac version):
Code: Select all
⍝ Place in MyNs
∇setdisplay _ignore_me
 :Implements Trigger *
  ⎕DF ⎕FMT↑{
     (⍵,' is now:')(⎕OR ⍵)   
  }¨↓⎕NL 2

     MyNs.(a b c)←10 'cats' (⍳10)
 a is now:                    10
 b is now:                  cats
 c is now:  1 2 3 4 5 6 7 8 9 10

The display form for MyNs will be updated when ANY variable in MyNs is changed...
"Hovering" over the name will show the (updated) ⎕DF display information.
(Bug: If you expunge a variable-- MyNs.⎕EX 'c' --, the TRIGGER function won't be called.)

The Class "trick" also works perfectly and would allow the same trigger code, as well.
Posts: 52
Joined: Thu Nov 11, 2010 11:04 pm

Return to Language

Who is online

Users browsing this forum: No registered users and 1 guest