This is the mail archive of the
kawa@sourceware.org
mailing list for the Kawa project.
multi-dimensional array reading and (pretty-)printing
- From: Per Bothner <per at bothner dot com>
- To: Kawa mailing list <kawa at sourceware dot org>
- Date: Sun, 31 Jan 2016 13:16:46 -0800
- Subject: multi-dimensional array reading and (pretty-)printing
- Authentication-results: sourceware.org; auth=none
I checked in support for extended reader syntax for multi-dimensional arrays,
using Guile extensions (https://www.gnu.org/software/guile/manual/html_node/Array-Syntax.html).
For example, you can write: #2f32@1:2@1:3((4 5 6) (0.5 1.25 9))
That creates a rank-2 array whose dimensions are 2*3 array and
the index lower-bounds are 1 rather than the default 0. This
uses a (32-bit) 'float' array because of the "f32".
Use "a" for a generic array.
On write, the dimension info (i.e. "@1:2@1:3") is written when it's "needed"
i.e. a lower-bound is non-zero or a dimension is 0.
(The syntax of 0-rank arrays follows Common Lisp, rather than Guile,
which (incorrectly IMO) adds an extra set a parentheses.)
The new format-array procedure takes an array and an optional format.
The result is a string which when printed displays the array in a
nice 2-dimensional layout, similar to what APL does. For example
the above array becomes:
â#2f32@1:2@1:3
â4.0â 5.0â6.0â
ââââââââââââââ
â0.5â1.25â9.0â
ââââââââââââââ
Note this uses Unicode "box-drawing" characters, which might cause problems
for some.
The optional format is used to format individual array elements. For example:
(format-array #2f32((4 5 6) (0.5 1.25 9)) "~,2f")
â#2f32:2:3ââââââ
â4.00â5.00â6.00â
ââââââââââââââââ
â0.50â1.25â9.00â
ââââââââââââââââ
A more complex example:
#2a((334 4545 #2f32((5 6))) (78987 "abc\ndefg\nhi" #2A((1 2) (3 14))))
â#2a:2:3ââââââââââââââ
â 334â4545â#2f32:1:2â
â â ââ5.0â6.0ââ
â â âââââââââââ
ââââââââââââââââââââââ
â78987âabc â#2aâââ â
â âdefgââ1â 2â â
â âhi âââââââ â
â â ââ3â14â â
â â âââââââ â
ââââââââââââââââââââââ
There are more examples in testsuite/arr-test.scm.
Finally, the REPL by default uses format-array, though not when using write,
and only when at column 0. We might want to tweak this
choice. In particular, people with old crummy terminals,
missing fonts, or encoding problems might have problems.
Let me know if format-array by-default causes problems for you.
These changes are a pre-cursor to more extensive array changes.
They will make it easier to test and document the latter.
--
--Per Bothner
per@bothner.com http://per.bothner.com/