1

Let's define deparse1 as inverse operation to q's native parse, so that the following holds:

q)aStringQExpression~deparse parse aStringQExpression
1b

Question

What's the definition of deparse function so that the above indeed works?

For example, in the below update expression, we know that "a*0^b+c-d" expression corresponds to (*;`a;(^;0;(+;`b;(-;`c;`d)))) parse tree:

q)-3!parse "update col:a*0^b+c-d from t"
"(!;`t;();0b;(,`col)!,(*;`a;(^;0;(+;`b;(-;`c;`d)))))"

So the envisaged deparse function should return:

q)deparse "(*;`a;(^;0;(+;`b;(-;`c;`d))))"
"a*0^b+c-d"
q)deparse "(!;`t;();0b;(,`col)!,(*;`a;(^;0;(+;`b;(-;`c;`d)))))"
"update col:a*0^b+c-d from t"

Motivation/Background/Use case: Inline expressions are arguably faster to grok by human eye (left-to-right) than deeply nested parse trees. Although in the background my code is editing the parse tree programatically, it is useful for debugging or presentation to conveniently convert that resulting parse tree into inline expression string.


1 Similar functionality described here: http://adv-r.had.co.nz/Expressions.html#parsing-and-deparsing

Daniel Krizian
  • 4,586
  • 4
  • 38
  • 75

2 Answers2

1

This unparse repository from Github solves the problem. Amazing:

q).unparse.unparse parse "update col:a*0^b+c-d from t"
"update col:(a*(0^(b+(c-d)))) from t"

q).unparse.unparse parse "a*0^b+c-d"
"(a*(0^(b+(c-d))))"
Daniel Krizian
  • 4,586
  • 4
  • 38
  • 75
0

I think the only way to do this would be to parse the list recursively and build up a string, e.g. for a dyadic:

q)deparse:{a:-3!'x;a[1],a[0],a[2]}
q)deparse parse "3*3"
"3*3"

So you can count last x to get it valency and build the string accordingly

Manish Patel
  • 4,411
  • 4
  • 25
  • 48