Category talk:Wren-fmt: Difference between revisions

Content added Content deleted
(→‎Source code: Bug fix.)
(→‎Source code: Added support for formatted printing of complex numbers and matrices.)
Line 1: Line 1:
===Source code===
===Source code===

<lang ecmascript>/* Module "fmt.wren" */
<lang ecmascript>/* Module "fmt.wren" */


Line 454: Line 455:
}
}
return f
return f
}

// Applies the 'f' format to each component, x and y, of a complex number 'n'
// before joining them together in the form x ± yi.
static z(w, n, p) {
if (n is Num) return f(w, n, p)
if (n.type.toString != "Complex") Fiber.abort("Argument must be a complex or real number.")
var real = f(w, n.real, p)
var sign = (n.imag >= 0) ? " + " : " - "
var imag = f(w, n.imag.abs, p)
return real + sign + imag + "i"
}
}


Line 462: Line 474:
static g(w, n) { g(w, n, precision) }
static g(w, n) { g(w, n, precision) }
static h(w, n) { h(w, n, precision) }
static h(w, n) { h(w, n, precision) }
static z(w, n) { z(w, n, precision) }
static fz(w, n) { fz(w, n, precision) }
static fz(w, n) { fz(w, n, precision) }
static gz(w, n) { gz(w, n, precision) }
static gz(w, n) { gz(w, n, precision) }
Line 491: Line 504:
(fn == "g") ? g(w, v, p) :
(fn == "g") ? g(w, v, p) :
(fn == "h") ? h(w, v, p) :
(fn == "h") ? h(w, v, p) :
(fn == "z") ? z(w, v, p) :
(fn == "dz") ? dz(w, v) :
(fn == "dz") ? dz(w, v) :
(fn == "bz") ? bz(w, v) :
(fn == "bz") ? bz(w, v) :
Line 516: Line 530:
// The method to be applied is specified (as a string) in 'fn'.
// The method to be applied is specified (as a string) in 'fn'.
// The parameters to be passed to the method are specified in 'w' and 'p'
// The parameters to be passed to the method are specified in 'w' and 'p'
// 'p' is needed for 'e', 'E', 'f', 'g', 'h', 'fz', 'gz', 'hz', 'fc', 'gc' or 'hc' but is ignored otherwise.
// 'p' is needed for 'e', 'E', 'f', 'g', 'h', 'z', 'fz', 'gz', 'hz', 'fc', 'gc'
// or 'hc' but is ignored otherwise.
// The resulting strings are then joined together using the separator 'sep'.
// The resulting strings are then joined together using the separator 'sep'.
// having first applied the 'q' method, with parameter 'cc', to each of them.
// having first applied the 'q' method, with parameter 'cc', to each of them.
Line 540: Line 555:
// Applies a 'short' formatting method to each element of a two-dimensional
// Applies a 'short' formatting method to each element of a two-dimensional
// list or sequence 'm'.
// list or sequence 'm'.
// A Matrix object is automatically converted to a 2D list of numbers.
// A Matrix or CMatrix object is automatically converted to a 2D list of numbers.
// The parameters: 'fn', 'w', 'p', 'sep', 'bb' and 'cc'
// The parameters: 'fn', 'w', 'p', 'sep', 'bb' and 'cc'
// are applied using the 'v' method to each row of 'm'.
// are applied using the 'v' method to each row of 'm'.
// The rows are then joined together using the separator 'ss'.
// The rows are then joined together using the separator 'ss'.
static v2(fn, w, m, p, sep, bb, cc, ss) {
static v2(fn, w, m, p, sep, bb, cc, ss) {
if (m.type.toString == "Matrix") m = m.toList
var s = m.type.toString
if (s == "Matrix" || s == "CMatrix") m = m.toList
var nr = m.count
var nr = m.count
if (nr == 0) return ""
if (nr == 0) return ""
Line 570: Line 586:
// $[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, x or X.
// a, b, c, d, e, E, f, g, h, i, k, m, n, o, q, r, s, t, x, X or z.
// 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 582: Line 598:
// It doesn't include any '#' flag prefix. If [width] is absent, a width of one is passed.
// It doesn't include any '#' flag prefix. If [width] is absent, a width of one is passed.
// If present, the precision is the number of decimal places to be passed to the appropriate
// If present, the precision is the number of decimal places to be passed to the appropriate
// 'e', 'E', 'f', 'g' or 'h' style method. If absent, the default precision is passed.
// 'e', 'E', 'f', 'g', 'h' or 'z' style method. If absent, the default precision is passed.
// Where any optional item is inappropriate to the method being used it is simply ignored.
// Where any optional item is inappropriate to the method being used it is simply ignored.
// Where one of the arguments is a sequence (other than a string) this method senses it
// Where one of the arguments is a sequence (other than a string) this method senses it
Line 639: Line 655:
var fn = ""
var fn = ""
var ds = ""
var ds = ""
if ("abcdeEfghikmnoqrstxX".codePoints.contains(cp)) { // format letter
if ("abcdeEfghikmnoqrstxXz".codePoints.contains(cp)) { // format letter
fn = Conv.itoc(cp)
fn = Conv.itoc(cp)
} else if (cp == 42) { // star
} else if (cp == 42) { // star
Line 674: Line 690:


if (fn == "") {
if (fn == "") {
if (!"abcdeEfghikmnoqrstxX".codePoints.contains(cp)) {
if (!"abcdeEfghikmnoqrstxXz".codePoints.contains(cp)) {
Fiber.abort("Unrecognized character in format string.")
Fiber.abort("Unrecognized character in format string.")
}
}
Line 762: Line 778:
// Prints (with a following \n) an array 'a' to stdout using a typical layout.
// 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.
// 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 parameters: 'w', 'p' and 'bb' are applied using the 'v' method to 'a'.
// The settings for the other parameters are:
// The settings for the other parameters are:
// 'fn' = "f" for numbers, "s" otherwise ('p' is ignored for latter)
// 'fn' = "f" for numbers, "z" for complex numbers,"s" otherwise
// 'sep' = " ", 'cc' = "".
// ('p' is ignored for latter) 'sep' = " ", 'cc' = "".
static aprint(a, w, p, bb) {
static aprint(a, w, p, bb) {
var fn = (a.count > 0 && (a[0] is Num)) ? "f" : "s"
var fn = (a.count > 0 && (a[0] is Num)) ? "f" :
(a.count > 0 && (a[0].type.toString == "Complex")) ? "z" : "s"
System.print(Fmt.v(fn, w, a, p, " ", bb, ""))
System.print(Fmt.v(fn, w, a, p, " ", bb, ""))
}
}
Line 779: Line 796:
// Prints (with a following \n) a matrix 'm' to stdout using a typical layout.
// 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' for this purpose is a two-dimensional list or sequence of objects.
// A Matrix object is automatically converted to a 2D list of numbers.
// A Matrix or CMatrix 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 parameters: 'w', 'p' and 'bb' are applied using the 'v2' method to 'm'.
// The settings for the other parameters are:
// The settings for the other parameters are:
// 'fn' = "f" for numbers, "s" otherwise ('p' is ignored for latter)
// 'fn' = "f" for numbers, "z" for complex numbers, "s" otherwise
// 'sep' = " ", 'cc' = "", 'ss' = "\n".
// ('p' is ignored for latter) 'sep' = " ", 'cc' = "", 'ss' = "\n".
static mprint(m, w, p, bb) {
static mprint(m, w, p, bb) {
if (m.type.toString == "Matrix") m = m.toList
var s = m.type.toString
var fn = (m.count > 0 && m[0].count > 0 && (m[0][0] is Num)) ? "f" : "s"
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"
System.print(Fmt.v2(fn, w, m, p, " ", bb, "", "\n"))
System.print(Fmt.v2(fn, w, m, p, " ", bb, "", "\n"))
}
}