JSONiq Matrix transposition

110 views
Skip to first unread message

her...@stamm-wilbrandt.de

unread,
Nov 30, 2013, 9:07:12 PM11/30/13
to jso...@googlegroups.com
Nice how simple matrix transposition can be done with JSONiq.

I just took the simple sample from Wikipedia:
http://en.wikipedia.org/wiki/Transpose

This JSONiq script just does the job:
(:  let $m := pxp:json($raw);  :)

declare function local:transpose($m as array()) as array()
{
  [  for $i in 1 to jn:size($m(1))  return [ $m()($i) ]  ]
};

let $m := [
  [1,2,3],
  [4,5,6]
]

return local:transpose($m)


It generates this output:
[ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]


Unfortunately just now "Share" does not work on http://try.zorba.io but here is Xidel permalink for playing:
http://videlibri.sourceforge.net/cgi-bin/xidelcgi?&data=[%20[1%2C2%2C3]%2C%20[4%2C5%2C6]%20]&=&extract=%28%3A%20%20let%20%24m%20%3A%3D%20pxp%3Ajson%28%24raw%29%3B%20%20%3A%29%0A%0Adeclare%20function%20local%3Atranspose%28%24m%20as%20array%28%29%29%20as%20array%28%29%0A{%0A%20%20[%20%20for%20%24i%20in%201%20to%20jn%3Asize%28%24m%281%29%29%20%20return%20[%20%24m%28%29%28%24i%29%20]%20%20]%0A}%3B%0A%0Alet%20%24m%20%3A%3D%20[%20%0A%20%20[1%2C2%2C3]%2C%20%0A%20%20[4%2C5%2C6]%20%0A]%0A%0Areturn%20local%3Atranspose%28%24m%29%0A&=&input-format=auto&printed-node-format=text&output-format=adhoc&compatibility=Standard%20XQuery%2BJSONiq&dot-notation=off&extract-kind=xquery1&no-extended-strings=true&only-json-objects=true&strict-type-checking=true&strict-namespaces=true&case-sensitive=true


DataPower has implemented version 0.4.42 of JSONiq Extension to XQuery which does not provide Array unboxing:
http://www.jsoniq.org/docs/JSONiqExtensionToXQuery/html-single/index.html#idm62098192

Good that the spec itself gives the implementation, and so DataPower can use "local:unbox()" function.

This is JSONiq script  transpose.xq:
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";

declare option output:method "json";
declare option jsoniq-version "0.4.42";

declare function local:unbox($a as array()) as json-item()*
{
  for $i in 1 to jn:size($a) return $a($i)
};

declare function local:transpose($m as array()) as array()
{
  [  for $i in 1 to jn:size($m(1))  return [ local:unbox($m)($i) ]  ]
};

local:transpose(.)


Recently I did publish coproc2all service which extends coproc2 to XQuery and JSONiq processing for DataPower:
https://www.ibm.com/developerworks/community/forums/html/topic?id=77777777-0000-0000-0000-000014590913#f5915ec6-99ff-426d-a3d2-c71d6d96efc9

Here is sample execution against JSONiq endpoint:
$ echo '[ [11,2,3], [4,5,6] ]' | coproc2 transpose.xq - http://dp6-l3:2226;echo

[
  [
    11,
    4
  ],
  [
    2,
    5
  ],
  [
    3,
    6
  ]
]
$


Hermann.

Ghislain Fourny

unread,
Dec 9, 2013, 4:44:11 AM12/9/13
to jso...@googlegroups.com
Hi Hermann,

Nice :-)

Note that unboxing can also be done with the jn:members($array) function -- $array() (in the extension) or $array[] (in pure JSONiq) is only a shortcut for this. If I am correct, jn:members should already have been there in 0.4.42.

Kind regards,
Ghislain
> --
> You received this message because you are subscribed to the Google Groups "JSONiq" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jsoniq+un...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

her...@stamm-wilbrandt.de

unread,
Dec 14, 2013, 12:54:15 PM12/14/13
to jso...@googlegroups.com
Thanks Ghislain,

as you can see below  jn:members()  works like a charm, no  local:unbox()  needed.
Btw, did post another nice JSONiq script yesterday with DataPower specific extension functions:
https://www.ibm.com/developerworks/community/blogs/HermannSW/entry/websphere_datapower_firmware_release_6_0_1_now_available


$ cat transpose.2.xq
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";

declare option output:method "json";
declare option jsoniq-version "0.4.42";

declare function local:transpose($m as array()) as array()
{
  [  for $i in 1 to jn:size($m(1))  return [ jn:members($m)($i) ]  ]
};

local:transpose(.)
$
$ cat m.json
[
  [1,2,3],
  [4,5,6]
]
$
$ coproc2 transpose.2.xq m.json http://firestar:2226 ; echo

[
  [

    1,
    4
  ],
  [
    2,
    5
  ],
  [
    3,
    6
  ]
]
$


Hermann.
Reply all
Reply to author
Forward
0 new messages