Category talk:Wren-fmt: Difference between revisions

→‎Source code: tprint and cprint can now use separators other than a single space. Fixed some other minor issues.
m (Change to error message.)
(→‎Source code: tprint and cprint can now use separators other than a single space. Fixed some other minor issues.)
Line 895:
 
// Prints (with a following \n) a sequence 'a' to stdout in tabular form
// with a maximum of 'rowSize' elements per row. 'fmt' is applied individually
// to each element and formatted elements are separated by a single space'sep'.
static tprint(fmt, a, rowSize, sep) {
if (!(fmt is String)) Fiber.abort("First argument must be a string.")
if (!(a is Sequence)) Fiber.abort("Second argument must be a sequence.")
Line 903:
Fiber.abort("Third argument must be a positive integer.")
}
fmtvar ac = fmt + " "a.count
var count = 0
for (e in a) {
Fmt.write(fmt, e)
count = count + 1
if (count % rowSize == 0) System.print(|| count == ac) {
if (count % rowSize != 0) System.print()
} else System.write(sep)
}
if (count % rowSize != 0) System.print()
}
 
// Prints (with a following \n) a sequence 'a' to stdout in columnar form
// with a maximum of 'colSize' elements per column. 'fmt' is applied individually
// to each element and formatted elements are separated by a single space'sep'.
static cprint(fmt, a, colSize, sep) {
if (!(fmt is String)) Fiber.abort("First argument must be a string.")
if (!(a is Sequence)) Fiber.abort("Second argument must be a sequence.")
Line 922 ⟶ 923:
Fiber.abort("Third argument must be a positive integer.")
}
fmtif =(!(a fmtis +List)) "a = "a.toList
var ac = a.count
for (i in 0...colSize) {
var j = i
while (j < a.counttrue) {
Fmt.write(fmt, a[j])
j = j + colSize
if (j >= ac) break
System.write(sep)
}
System.print()
}
}
 
// Convenience versions of the above methods which use a single space for the separator.
static tprint(fmt, a, rowSize) { tprint(fmt, a, rowSize, " ") }
static cprint(fmt, a, colSize) { cprint(fmt, a, colSize, " ") }
 
// Prints (with a following \n) an array 'a' to stdout using a typical layout.
Line 941 ⟶ 949:
// The 'rns' parameter, if true, leaves a space for the sign of a real number but only prints minus.
static aprint(a, w, p, bb, rns) {
if (!(a is Sequence)) Fiber.abort("Second argument must be a sequence.")
if (s!(a ==is "Matrix" || s == "CMatrix"List)) ma = ma.toList
var fn = (a.count > 0 && (a[0] is Num)) ? "f" :
(a.count > 0 && (a[0].type.toString == "Complex")) ? "z" : "s"
Line 963 ⟶ 973:
// The rns' parameter, if true, leaves a space for the sign of a real number but only prints minus.
static mprint(m, w, p, bb, rns) {
varif s(!(m is List)) m = m.type.toStringtoList
if (s == "Matrix" || s == "CMatrix") m = m.toList
var fn = (m.count > 0 && m[0].count > 0 && (m[0][0] is Num)) ? "f" :
(m.count > 0 && m[0].count > 0 && (m[0][0].type.toString == "Complex")) ? "z" : "s"
9,476

edits