Category talk:Wren-fmt: Difference between revisions

Content added Content deleted
(→‎Source code: Further tinkerings including some formatting options which were missing before.)
(→‎Source code: Added some new methods mostly for use with the forthcoming Wren-matrix module.)
Line 534: Line 534:
// for some parameters.
// for some parameters.
static v(fn, w, seq, p, sep, bb) { v(fn, w, seq, p, sep, bb, "") }
static v(fn, w, seq, p, sep, bb) { v(fn, w, seq, p, sep, bb, "") }
static v(fn, w, seq, p, sep) { v(fn, w, seq, p, sep, "[]", "") }
static v(fn, w, seq, p) { v(fn, w, seq, p, ", ", "[]", "") }
static v(fn, w, seq, p) { v(fn, w, seq, p, ", ", "[]", "") }
static v(fn, w, seq) { v(fn, w, seq, precision, ", ", "[]", "") }
static v(fn, w, seq) { v(fn, w, seq, precision, ", ", "[]", "") }

// Applies a 'short' formatting method to each element of a two-dimensional
// list or sequence 'm'.
// A Matrix object is automatically converted to a 2D list of numbers.
// The parameters: 'fn', 'w', 'p', 'sep', 'bb' and 'cc'
// are applied using the 'v' method to each row of 'm'.
// The rows are then joined together using the separator 'ss'.
static v2(fn, w, m, p, sep, bb, cc, ss) {
if (m.type.toString == "Matrix") m = m.toList
var nr = m.count
if (nr == 0) return ""
var l = List.filled(nr, "")
var i = 0
for (row in m) {
l[i] = v(fn, w, row, p, sep, bb, cc)
i = i + 1
}
return l.join(ss)
}

// Convenience versions of the above method which use default values
// for some parameters.
static v2(fn, w, m, p, sep, bb, cc) { v(fn, w, m, p, sep, bb, cc, "\n") }
static v2(fn, w, m, p, sep, bb) { v(fn, w, m, p, sep, bb, "", "\n") }
static v2(fn, w, m, p, sep) { v(fn, w, m, p, sep, "|", "", "\n") }
static v2(fn, w, m, p) { v(fn, w, m, p, " ", "|", "", "\n") }
static v2(fn, w, m) { v(fn, w, m, precision, " ", "|", "", "\n") }


// Provides a 'sprintf' style method where the arguments are passed in a separate list and
// Provides a 'sprintf' style method where the arguments are passed in a separate list and
Line 542: Line 570:
// $[flag][width][.precision][letter] of which all bracketed items except [letter] are optional.
// $[flag][width][.precision][letter] of which all bracketed items except [letter] are optional.
// The letter must be one of the 'short' methods:
// The letter must be one of the 'short' methods:
// a, b, c, d, e, E, f, g, h, i, k, m, n, o, q, r, s, t, v, x or X.
// a, b, c, d, e, E, f, g, h, i, k, m, n, o, q, r, s, t, x or X.
// If present, the flag (there can only be one) must be one of the following:
// If present, the flag (there can only be one) must be one of the following:
// + always prints a + or - sign ('dp' method)
// + always prints a + or - sign ('dp' method)
Line 731: Line 759:
static print(fmt, a1) { System.print(slwrite(fmt, [a1])) }
static print(fmt, a1) { System.print(slwrite(fmt, [a1])) }
static lprint(fmt, a) { System.print(slwrite(fmt, a)) }
static lprint(fmt, a) { System.print(slwrite(fmt, a)) }

// Prints (with a following \n) an array 'a' to stdout using a typical layout.
// An 'array' for this purpose is a list or sequence of objects.
// The parameters: 'w', 'p' and 'bb' are applied using the 'v' method to 'a'.
// The settings for the other parameters are:
// 'fn' = "f" for numbers, "s" otherwise ('p' is ignored for latter)
// 'sep' = " ", 'cc' = "".
static aprint(a, w, p, bb) {
var fn = (a.count > 0 && (a[0] is Num)) ? "f" : "s"
System.print(Fmt.v(fn, w, a, p, " ", bb, ""))
}

// Convenience versions of the above method which use default values for
// some parameters.
static aprint(a, w, p) { aprint(a, w, p, "[]") }
static aprint(a, w) { aprint(a, w, precision, "[]") }
static aprint(a) { aprint(a, 0, precision, "[]") }

// Prints (with a following \n) a matrix 'm' to stdout using a typical layout.
// A 'matrix' for this purpose is a two-dimensional list or sequence of objects.
// A Matrix object is automatically converted to a 2D list of numbers.
// The parameters: 'w', 'p' and 'bb' are applied using the 'v2' method to 'm'.
// The settings for the other parameters are:
// 'fn' = "f" for numbers, "s" otherwise ('p' is ignored for latter)
// 'sep' = " ", 'cc' = "", 'ss' = "\n".
static mprint(m, w, p, bb) {
if (m.type.toString == "Matrix") m = m.toList
var fn = (m.count > 0 && m[0].count > 0 && (m[0][0] is Num)) ? "f" : "s"
System.print(Fmt.v2("f", w, m, p, " ", bb, "", "\n"))
}

// Convenience versions of the above method which use default values for
// some parameters.
static mprint(m, w, p) { mprint(m, w, p, "|") }
static mprint(m, w) { mprint(m, w, precision, "|") }
static mprint(m) { mprint(m, 0, precision, "|") }
}
}