Category talk:Wren-fmt: Difference between revisions

→‎Source code: Added support for Unicode number formats and printing polynomials.
(→‎Source code: More changes.)
(→‎Source code: Added support for Unicode number formats and printing polynomials.)
Line 107:
return "%(n)%(suffix)"
}
 
// Converts an integer to its unicode superscript equivalent.
static superscript(n) {
if (!(n is Num && n.isInteger)) Fiber.abort("Argument must be an integer.")
var ss = {
"0": "⁰", "1": "¹", "2": "²", "3": "³", "4": "⁴", "5": "⁵",
"6": "⁶", "7": "⁷", "8": "⁸", "9": "⁹", "-": "⁻"
}
return n.toString.map { |d| ss[d] }.join()
}
 
// Converts an integer to its unicode subscript equivalent.
static subscript(n) {
if (!(n is Num && n.isInteger)) Fiber.abort("Argument must be an integer.")
var ss = {
"0": "₀", "1": "₁", "2": "₂", "3": "₃", "4": "₄", "5": "₅",
"6": "₆", "7": "₇", "8": "₈", "9": "₉", "-": "₋"
}
return n.toString.map { |d| ss[d] }.join()
}
 
// Converts a numerator and denominator to their unicode fraction equivalent.
// If there is none, returns a string of the form "n/d".
static fraction(n, d) {
if (!(n is Num && n.isInteger && n > 0)) Fiber.abort("n must be a positive integer.")
if (!(d is Num && d.isInteger && d > 0)) Fiber.abort("d must be a positive integer.")
var fracs = {
"1/4": "¼", "1/2": "½", "3/4": "¾", "1/7": "⅐", "1/9": "⅑", "1/10": "⅒",
"1/3": "⅓", "2/3": "⅔", "1/5": "⅕", "2/5": "⅖", "3/5": "⅗", "4/5": "⅘",
"1/6": "⅙", "5/6": "⅚", "1/8": "⅛", "3/8": "⅜", "5/8": "⅝", "7/8": "⅞"
}
var frac = "%(n)/%(d)"
return fracs.containsKey(frac) ? fracs[frac] : frac
}
 
// Returns the unicode infinity symbol.
static infinity { "∞" }
}
 
Line 919 ⟶ 956:
static mprint(m, w) { mprint(m, w, precision, "|", false) }
static mprint(m) { mprint(m, 0, precision, "|", false) }
 
// Formats a polynomial as a string.
// Polynomials are represented by an ordered list of coefficients
// from the term with the highest degree down to the constant term.
// Unless there is only one term, terms with zero coefficents are suppressed.
// Unless it's the constant term, the '1' in coefficients of exactly ± 1 is also suppressed.
// 'fmt' is applied to each coefficient, 'symbol' is the exponentiation
// symbol (e.g. "^") and 'variable' (e.g. "x") is the variable name.
// If symbol = "", unicode superscript characters are used for the degree.
static spwrite(fmt, coefs, symbol, variable) {
if (!(fmt is String)) Fiber.abort("First argument must be a string.")
if (!(coefs is List) || coefs.count == 0 || !(coefs[0] is Num)) {
Fiber.abort("Second argument must be a non-empty ordered list of numbers.")
}
if (!(symbol is String)) Fiber.abort("Third argument must be a string.")
if (!(variable is String)) Fiber.abort("Fourth argument must be a string.")
var degree = coefs.count - 1
if (degree == 0 || coefs.all { |c| c == 0 }) return Fmt.swrite(fmt, coefs[0])
var p = ""
for (i in 0..degree) {
var coef = coefs[i]
var pow = degree - i
if (coef == 0) continue
if (coef > 0) {
p = p + " + " + ((coef == 1 && pow > 0) ? "": Fmt.swrite(fmt, coef))
} else {
p = p + " - " + ((coef == -1 && pow > 0) ? "": Fmt.swrite(fmt, -coef))
}
if (pow > 1) {
if (symbol != "") {
p = p + Fmt.swrite("$s$s$d", variable, symbol, pow)
} else {
p = p + Fmt.swrite("$s$s", variable, Conv.superscript(pow))
}
} else if (pow == 1) {
p = p + variable
}
}
p = p.startsWith(" + ") ? p[3..-1] : "-" + p[3..-1]
return p
}
 
// Prints a polynomial without (pwrite) or with (pprint) a following \n to stdout.
static pwrite(fmt, coefs, symbol, variable) {
System.write(spwrite(fmt, coefs, symbol, variable))
}
 
static pprint(fmt, coefs, symbol, variable) {
System.print(spwrite(fmt, coefs, symbol, variable))
}
}</syntaxhighlight>
9,482

edits