Jump to content

Category talk:Wren-fmt: Difference between revisions

→‎Source code: Added support for formatted printing of complex numbers and matrices.
(→‎Source code: Bug fix.)
(→‎Source code: Added support for formatted printing of complex numbers and matrices.)
Line 1:
===Source code===
 
<lang ecmascript>/* Module "fmt.wren" */
 
Line 454 ⟶ 455:
}
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 ⟶ 474:
static g(w, n) { g(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 gz(w, n) { gz(w, n, precision) }
Line 491 ⟶ 504:
(fn == "g") ? g(w, v, p) :
(fn == "h") ? h(w, v, p) :
(fn == "z") ? z(w, v, p) :
(fn == "dz") ? dz(w, v) :
(fn == "bz") ? bz(w, v) :
Line 516 ⟶ 530:
// 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'
// 'p' is needed for 'e', 'E', 'f', 'g', 'h', 'z', 'fz', 'gz', 'hz', 'fc', 'gc' or 'hc' but is ignored otherwise.
// or 'hc' but is ignored otherwise.
// The resulting strings are then joined together using the separator 'sep'.
// having first applied the 'q' method, with parameter 'cc', to each of them.
Line 540 ⟶ 555:
// Applies a 'short' formatting method to each element of a two-dimensional
// list or sequence 'm'.
// A Matrix or CMatrix 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) {
ifvar s = (m.type.toString == "Matrix") m = m.toList
if (s == "Matrix" || s == "CMatrix") m = m.toList
var nr = m.count
if (nr == 0) return ""
Line 570 ⟶ 586:
// $[flag][width][.precision][letter] of which all bracketed items except [letter] are optional.
// 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, X or Xz.
// If present, the flag (there can only be one) must be one of the following:
// + always prints a + or - sign ('dp' method)
Line 582 ⟶ 598:
// 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
// 'e', 'E', 'f', 'g', 'h' or 'hz' 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 one of the arguments is a sequence (other than a string) this method senses it
Line 639 ⟶ 655:
var fn = ""
var ds = ""
if ("abcdeEfghikmnoqrstxXabcdeEfghikmnoqrstxXz".codePoints.contains(cp)) { // format letter
fn = Conv.itoc(cp)
} else if (cp == 42) { // star
Line 674 ⟶ 690:
 
if (fn == "") {
if (!"abcdeEfghikmnoqrstxXabcdeEfghikmnoqrstxXz".codePoints.contains(cp)) {
Fiber.abort("Unrecognized character in format string.")
}
Line 762 ⟶ 778:
// 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, "sz" otherwisefor ('p'complex isnumbers,"s" ignored for latter)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"
(a.count > 0 && (a[0].type.toString == "Complex")) ? "z" : "s"
System.print(Fmt.v(fn, w, a, p, " ", bb, ""))
}
Line 779 ⟶ 796:
// 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 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 settings for the other parameters are:
// 'fn' = "f" for numbers, "sz" otherwisefor ('p'complex isnumbers, ignored"s" for latter)otherwise
// ('p' is ignored for latter) 'sep' = " ", 'cc' = "", 'ss' = "\n".
static mprint(m, w, p, bb) {
ifvar s = (m.type.toString == "Matrix") m = m.toList
varif fn(s == (m.count"Matrix" >|| 0s &&== "CMatrix") m[0].count >= 0 && (m[0][0] is Num)) ? "f" : "s".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"))
}
9,485

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.