public inbox for kawa@sourceware.org
 help / color / mirror / Atom feed
* multi-dimensional array reading and (pretty-)printing
@ 2016-01-31 21:17 Per Bothner
  0 siblings, 0 replies; only message in thread
From: Per Bothner @ 2016-01-31 21:17 UTC (permalink / raw)
  To: Kawa mailing list

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/

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2016-01-31 21:17 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-31 21:17 multi-dimensional array reading and (pretty-)printing Per Bothner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).