[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: [ANN] printable.lua
- From: Dirk Laurie <dirk.laurie@...>
- Date: Mon, 7 Nov 2011 22:43:04 +0200
A printable table is one that has a `__tostring` metamethod. In a
well-designed object-oriented application, such a metamethod would be
provided as a matter of course. In many cases, though, it is simply
not worth the effort.
For those cases, I've written a one-size-fits-all function that can be
used for that purpose. It uses four reserved keys: `_sep`, `_fmt`,
`_kv` and `_post`, with inheritance via the `__index` metamethod to
achieve flexibility.
Sample:
ptab = require "printable"
S=ptab{msg='hello',1,2,{3,4}}
print(S)
--> {1,2,{3,4},msg="hello"}
A = {{1,2,3},{4,5,6}}
latexmatrix = {{_sep=' \\\\\n',_post='%'},' & '}
print (ptab(A,latexmatrix))
--> 1 & 2 & 3 \\
--> 4 & 5 & 6
<https://sites.google.com/a/aims.ac.za/experimental-mathematics/lua>
Dirk