Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"APL Object Notation" in Dyalog APL

Tags:

apl

dyalog

How to convert any Dyalog APL value to a character vector that could be passed to to get the same value?

How this is expected to 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 a value to a human-editable APL source code to be able to insert it to a unit test function, when a new problematic scenario has been found. I want those test scenarios to be in APL source code, not in files, because in a framework I work with, source code is nicely managed by a version control system, while files are not. And I want it to be human-editable, not just serialized, to make it easier to amend existing test scenarios when arguments/results change.

like image 698
Olexa Avatar asked May 29 '17 12:05

Olexa


3 Answers

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

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

Depending on what your data looks like, JSON might be a nice way to go - it is a format designed just for this:

In Dyalog 15.0:

       fromJSON←7159⌶ ⋄ toJSON←7160⌶ 
       (namespace←⎕NS '').life←42
       toJSON (⍳4) 'Hello' namespace
[[1,2,3,4],"Hello",{"life":42}]

The downside of JSON is that it cannot represent higher dimensional arrays. So you'll need to massage things a little if you need 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, to be released at the end of this month, the experimental I-Beams have become a system function ⎕JSON.

like image 176
Morten Kromberg Avatar answered Nov 20 '22 06:11

Morten Kromberg


⎕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!

like image 4
Adám Avatar answered Nov 20 '22 07:11

Adám


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

In IBM APL2 (and therefore also in GNU APL) you can use 2 ⎕TF to convert between a value (actually a variable with that value) and APL code that produces it (although not via but via 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 that purpose. The result consists of multiple APL statements so you have to ⍎¨ over the result:

      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'          
like image 1
Jürgen Sauermann Avatar answered Nov 20 '22 06:11

Jürgen Sauermann