"APL Object Notation" in Dyalog APL

How can I convert any APL Dyalog value to a character vector that can be passed to in ⍎

order to get the same value?

How it should look like:

      x←2 3⍴⍳6
      x←1,βŠ‚x
      x←x,βŠ‚'foo'
      y←desired_function x
      DPX y
β”Œβ†’β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚1,(βŠ‚2 3⍴⍳6),βŠ‚'foo'β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
      xβ‰‘βŽy
1

      

Update

The idea is to convert the value into human-editable APL source code so that he can insert it into the unit test function when a new problem scenario is found. I want these test scripts in the APL source code, not files, because in the environment I work with, the source code is well source controlled by the source code, but the files are not. And I want it to be human editable, not just serialized, so that it is easier to modify existing test scripts when the arguments / results change.

+3


source to share


5 answers


In my opinion, Execute and "transfer form" are not optimal solutions for a number of reasons:

  • Complex structures quickly become difficult to read.
  • Execution is a relatively heavy / inefficient tool to invoke, compare with other solutions.
  • If you're not careful, you open yourself up to "injection" attacks, malicious code in files that should contain constants but are executed when your application starts up.

Depending on your data, JSON might be a nice way - it's a format designed for just that:

In Dyalog 15.0:



       fromJSON←7159⌢ β‹„ toJSON←7160⌢ 
       (namespaceβ†βŽ•NS '').life←42
       toJSON (⍳4) 'Hello' namespace
[[1,2,3,4],"Hello",{"life":42}]

      

The disadvantage of JSON is that it cannot represent higher-dimensional arrays. Therefore, you need to lose a little weight if you want matrices:

       toJSON ↓3 4⍴⍳12 
[[1,2,3,4],[5,6,7,8],[9,10,11,12]]
       ↑fromJSON '[[1,2,3],[5,6,7]]'
1 2 3
5 6 7

      

In version 16.0, released later this month, experimental I-Beams became a βŽ•JSON system feature.

+3


source


I don't know if there is a way to do this with ⍎, but I mentioned that Dyalog Version 15.0 has 2 I-Beams to serialize and deserialize.

http://help.dyalog.com/15.0/Content/Language/Primitive%20Operators/Serialise%20Array.htm

eg.

]disp x
β”Œβ†’β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”
β”‚1β”‚0 1 2β”‚fooβ”‚
β”‚ β”‚3 4 5↓   β”‚
└─┴~β”€β”€β”€β†’β”΄β”€β”€β†’β”˜

   y← 0(220⌢) 0(219⌢) 1(219⌢) 1(220⌢) x

  ]disp y
β”Œβ†’β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”
β”‚1β”‚0 1 2β”‚fooβ”‚
β”‚ β”‚3 4 5↓   β”‚
└─┴~β”€β”€β”€β†’β”΄β”€β”€β†’β”˜

      y ≑ x
1

      



Alternatively, you can join our forums as you will reach more Dyalog APL users if you post your question there.

http://www.dyalog.com/forum/

Hello,

Vince

+1


source


βŽ•SE.Dyalog.Utils.repObj

For example:

      x←2 3⍴⍳6
      x←1,βŠ‚x
      x←x,βŠ‚'foo'
      yβ†βŽ•SE.Dyalog.Utils.repObj x
      ]Display y
β”Œβ†’β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚1 (2 3⍴1-βŽ•io-⍳6) 'foo'β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
      xβ‰‘βŽy
1

      

Try it online!

+1


source


Not sure about APL Dyalog, but most other APLs have built-in functions to achieve this.

In IBM APL2 (and therefore also GNU APL) you can use 2 βŽ•TF to convert between a value (actually a variable with that value) and the APL code that produces it (although not through ⍎ , but through another 2 βŽ•TF ):

      4 βŽ•CR x
┏→━━━━━━━━━━━━━━┓
┃1 ┏→━━━━┓ ┏→━━┓┃
┃  ↓1 2 3┃ ┃foo┃┃
┃  ┃4 5 6┃ ┗━━━┛┃
┃  ┗━━━━━┛      ┃
β”—βˆŠβ”β”β”β”β”β”β”β”β”β”β”β”β”β”β”›


      βŽ•β†text←2 βŽ•TF 'x'
x←1 (2 3⍴1 2 3 4 5 6) 'foo'

      )erase x
      2 βŽ•TF text
x
      4 βŽ•CR x
┏→━━━━━━━━━━━━━━┓
┃1 ┏→━━━━┓ ┏→━━┓┃
┃  ↓1 2 3┃ ┃foo┃┃
┃  ┃4 5 6┃ ┗━━━┛┃
┃  ┗━━━━━┛      ┃
β”—βˆŠβ”β”β”β”β”β”β”β”β”β”β”β”β”β”β”›

      

In GNU APL, you can also use 10 βŽ•CR for this purpose. The result consists of multiple APL statements, so you need ⍎¨ :

      10 βŽ•CR 'x'
 x←1 00 00   ((βŽ•IO+1)βŠƒx)←2 3⍴1 2 3 4 5 6   ((βŽ•IO+2)βŠƒx)←'foo' 

      βŠƒ10 βŽ•CR 'x'
    x←1 00 00                    
      ((βŽ•IO+1)βŠƒx)←2 3⍴1 2 3 4 5 6
      ((βŽ•IO+2)βŠƒx)←'foo'          

      

0


source


If I am missing something, the interpreter does not provide a mechanism for saving and restoring any possible variable type (there is a lot of complications with namespaces, objects, references to any of them, etc.). "Serialization" may come close, but (a) it is not human readable and (b) it is more of an interpreter mechanism than a tool for application developers, so I don't even want to talk about it further - there are better ways to deal with it;)

However, there is a custom command that will allow you to do this:

]save (Object) (Filename)

      

So

]save x h:\temp\x

      

saved it to a unicode file h: \ temp \ x.dyalog, which can also be edited with any Unicode compatible editor (or even) to edit h: \ temp \ x.dyalog).

By making

]load h:\temp\x

      

you can load this object into the workspace. Alternatively use

]xyz←load h:\temp\x -noname

      

to assign the value xyz

instead of loading in x

again.

And there is more ... I assume your question is the result of a desire to reproduce some data. Did you know about a "component filesystem" that offers an easy way to save variables of type x

to files and reload them from there? Example:

{x βŽ•fappend ⍡ β‹„ βŽ•FUNTIE ⍡}'h:\temp\x'βŽ•FCREATE 0

      

to save x

and then

{rβ†βŽ•fread ⍡,1 β‹„ βŽ•FUNTIE ⍡ β‹„ r}'h:\temp\x'βŽ•FTIE 0

      

to read it again. (Disclaimer: These examples are overly simplistic, since in case the file you are creating already exists, etc., this usually needs to be taken care of as well, but that would be too much of a distraction for this small sample. ..)

So finally, I think my real concern has to do with the context of this issue and the goal you are trying to achieve. In my entire APL life, I've rarely seen things that were "not doable", but sometimes the path is different from what you expect (knowing other languages) ...

0


source







All Articles