TypeScript
HIR projection with file emission and end-to-end floors.
Part of the HIR lane. Every panel below is compiler output.
How to read it#
The largest expansion among the host languages, because Faber's typed numerics and tensors have no TypeScript counterpart and arrive as generated runtime scaffolding.
Typed tensors#
Builds two shaped matrices, multiplies them, and reduces the product to a scalar. Exercises shape-bearing types and a reduction.
Faber source
faber format --locale en — English reader surfacemain {
const list<f32> flat_a ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
const list<f32> flat_b ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0]
const tensor<f32, []> seed ← vacua
const tensor<f32, [2, 3]> a ← seed.strue(flat_a, [2, 3])
const tensor<f32, [3, 4]> b ← seed.strue(flat_b, [3, 4])
const tensor<f32, [2, 4]> product ← a.matmul(b)
const f32 mean ← product.media()
print mean
}faber format --locale la — canonical Faberincipit {
fixum lista<f32> flat_a ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
fixum lista<f32> flat_b ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0]
fixum tensor<f32, []> seed ← vacua
fixum tensor<f32, [2, 3]> a ← seed.strue(flat_a, [2, 3])
fixum tensor<f32, [3, 4]> b ← seed.strue(flat_b, [3, 4])
fixum tensor<f32, [2, 4]> product ← a.matmul(b)
fixum f32 mean ← product.media()
nota mean
}faber format --locale th-TH — Thaiเริ่ม {
คงที่ รายการ<f32> flat_a ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
คงที่ รายการ<f32> flat_b ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0]
คงที่ เทนเซอร์<f32, []> seed ← เซตว่าง
คงที่ เทนเซอร์<f32, [2, 3]> a ← seed.strue(flat_a, [2, 3])
คงที่ เทนเซอร์<f32, [3, 4]> b ← seed.strue(flat_b, [3, 4])
คงที่ เทนเซอร์<f32, [2, 4]> product ← a.matmul(b)
คงที่ f32 mean ← product.media()
บันทึก mean
}faber format --locale zh-Hans — Simplified Chinese入口 {
常量 列表<f32> flat_a ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
常量 列表<f32> flat_b ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0]
常量 张量<f32, []> seed ← 空集
常量 张量<f32, [2, 3]> a ← seed.strue(flat_a, [2, 3])
常量 张量<f32, [3, 4]> b ← seed.strue(flat_b, [3, 4])
常量 张量<f32, [2, 4]> product ← a.matmul(b)
常量 f32 mean ← product.media()
显示 mean
}faber format --locale zh-Hant — Traditional Chinese入口 {
定值 列表<f32> flat_a ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
定值 列表<f32> flat_b ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0]
定值 張量<f32, []> seed ← 空集
定值 張量<f32, [2, 3]> a ← seed.strue(flat_a, [2, 3])
定值 張量<f32, [3, 4]> b ← seed.strue(flat_b, [3, 4])
定值 張量<f32, [2, 4]> product ← a.matmul(b)
定值 f32 mean ← product.media()
註記 mean
}faber format --locale vi — Vietnamesebắt_đầu {
hằng danh_sách<f32> flat_a ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
hằng danh_sách<f32> flat_b ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0]
hằng ten_xo<f32, []> seed ← tập_rỗng
hằng ten_xo<f32, [2, 3]> a ← seed.strue(flat_a, [2, 3])
hằng ten_xo<f32, [3, 4]> b ← seed.strue(flat_b, [3, 4])
hằng ten_xo<f32, [2, 4]> product ← a.matmul(b)
hằng f32 mean ← product.media()
ghi_chú mean
}faber format --locale ar — Arabicبداية {
ثابت قائمة<f32> flat_a ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
ثابت قائمة<f32> flat_b ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0]
ثابت موتر<f32, []> seed ← فارغ
ثابت موتر<f32, [2, 3]> a ← seed.strue(flat_a, [2, 3])
ثابت موتر<f32, [3, 4]> b ← seed.strue(flat_b, [3, 4])
ثابت موتر<f32, [2, 4]> product ← a.matmul(b)
ثابت f32 mean ← product.media()
اعرض mean
}faber format --locale hi — Hindiआरंभ {
स्थिर सूची<f32> flat_a ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
स्थिर सूची<f32> flat_b ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0]
स्थिर टेंसर<f32, []> seed ← खाली
स्थिर टेंसर<f32, [2, 3]> a ← seed.strue(flat_a, [2, 3])
स्थिर टेंसर<f32, [3, 4]> b ← seed.strue(flat_b, [3, 4])
स्थिर टेंसर<f32, [2, 4]> product ← a.matmul(b)
स्थिर f32 mean ← product.media()
दिखाओ mean
}TypeScript — 10 lines in, 294 out (29.4×)
// Generated by radix - do not edit
type FaberDisplayHint =
"unknown"
| "textus"
| "ascii"
| "numerus"
| "fractus"
| "bivalens"
| "nihil"
| "vacuum"
| "valor"
| "instans"
| "regex"
| { kind: "nullable"; inner: FaberDisplayHint }
| { kind: "lista"; element: FaberDisplayHint }
| { kind: "tensor"; element: FaberDisplayHint }
| { kind: "sparsa"; element: FaberDisplayHint }
| { kind: "map"; key: FaberDisplayHint; value: FaberDisplayHint }
| { kind: "set"; element: FaberDisplayHint }
;
function __faberDisplay(value: any, hint: FaberDisplayHint = "unknown"): string {
if (typeof hint === "object") {
if (hint.kind === "nullable") {
return value === null || value === undefined ? "nihil" : __faberDisplay(value, hint.inner);
}
if (hint.kind === "lista") { return __faberDisplayList(value, hint.element); }
if (hint.kind === "tensor") { return __faberDisplayTensor(value, hint.element); }
if (hint.kind === "sparsa") { return __faberDisplaySparsa(value, hint.element); }
if (hint.kind === "map") { return __faberDisplayMap(value, hint.key, hint.value); }
return __faberDisplayList(value, hint.element);
}
if (value === null || value === undefined) {
return hint === "vacuum" ? "vacuum" : "nihil";
}
switch (hint) {
case "bivalens": return value ? "verum" : "falsum";
case "fractus": return __faberDisplayFractus(value);
case "nihil": return "nihil";
case "vacuum": return "vacuum";
case "valor": return __faberDisplayValor(value);
case "regex": return String(value);
case "textus":
case "ascii":
case "instans":
case "numerus": return String(value);
default: return __faberDisplayValor(value);
}
}
function __faberDisplayFractus(value: any): string {
const n = Number(value);
if (!Number.isFinite(n)) { return String(value); }
return Number.isInteger(n) ? `${n}.0` : String(n);
}
function __faberDisplayList(value: any, element: FaberDisplayHint): string {
if (value === null || value === undefined) { return "nihil"; }
return `[${Array.from(value as Array<any>).map((item) => __faberDisplay(item, element)).join(", ")}]`;
}
function __faberDisplayTensor(value: any, element: FaberDisplayHint): string {
if (value === null || value === undefined) { return "nihil"; }
if (typeof value.planata === "function") { return __faberDisplayList(value.planata(), element); }
if (Array.isArray(value.data)) { return __faberDisplayList(value.data, element); }
return __faberDisplayValor(value);
}
function __faberDisplaySparsa(value: any, element: FaberDisplayHint): string {
if (value === null || value === undefined) { return "nihil"; }
if (typeof value.densata === "function") { return __faberDisplayTensor(value.densata(), element); }
return __faberDisplayValor(value);
}
function __faberDisplayMap(value: any, keyHint: FaberDisplayHint, valueHint: FaberDisplayHint): string {
if (value === null || value === undefined) { return "nihil"; }
const entries = value instanceof Map ? Array.from(value.entries()) : Object.entries(value);
return `{${entries.map(([key, item]) => `${JSON.stringify(__faberDisplay(key, keyHint))}: ${__faberDisplay(item, valueHint)}`).join(", ")}}`;
}
function __faberDisplayValor(value: any): string {
if (value !== null && typeof value === "object" && typeof value.__faberValorTag === "string") { return __faberDisplayTaggedValor(value); }
if (value === null || value === undefined) { return "nihil"; }
if (typeof value === "boolean") { return value ? "verum" : "falsum"; }
if (typeof value === "number") { return String(value); }
if (typeof value === "string") { return value; }
if (Array.isArray(value)) { return __faberDisplayList(value, "valor"); }
if (typeof value.text === "function") { return String(value.text()); }
if (typeof value.toString === "function" && value.toString !== Object.prototype.toString) { return String(value); }
return __faberDisplayMap(value, "textus", "valor");
}
function __faberDisplayTaggedValor(value: any): string {
const payload = value.__faberValorPayload;
switch (value.__faberValorTag) {
case "Nihil": return "nihil";
case "Bivalens": return payload ? "verum" : "falsum";
case "Numerus": return String(payload);
case "Fractus": return __faberDisplayFractus(payload);
case "Textus": return String(payload);
case "Instans": return String(payload);
case "Octeti": return `<${Array.isArray(payload) ? payload.length : 0} bytes>`;
case "Lista": return __faberDisplayList(payload, "valor");
case "Tabula": return __faberDisplayMap(payload, "textus", "valor");
default: return __faberDisplayValor(payload);
}
}
class FaberTensor<T> {
data: T[];
shape: number[];
constructor(data: T[], shape: number[]) {
this.data = data.slice();
this.shape = shape.slice();
}
private static elementCount(shape: number[]): number {
if (shape.some((dim) => dim < 0)) {
throw new Error("tensor shape dimension must be non-negative");
}
return shape.reduce((total, dim) => total * dim, 1);
}
private static coordinates(flat: number, shape: number[]): number[] {
const coords = new Array(shape.length).fill(0);
for (let axis = shape.length - 1; axis >= 0; axis--) {
const dim = shape[axis];
coords[axis] = dim === 0 ? 0 : flat % dim;
flat = dim === 0 ? 0 : Math.floor(flat / dim);
}
return coords;
}
private static offset(shape: number[], indices: number[]): number | null {
if (indices.some((idx) => idx < 0)) {
throw new Error("tensor accipe invalid index");
}
if (indices.length !== shape.length) {
return null;
}
let offset = 0;
let stride = 1;
for (let axis = shape.length - 1; axis >= 0; axis--) {
const idx = indices[axis];
const dim = shape[axis];
if (idx >= dim) {
return null;
}
offset += idx * stride;
stride *= dim;
}
return offset;
}
private static broadcastShape(left: number[], right: number[]): number[] {
const rank = Math.max(left.length, right.length);
const shape = new Array(rank);
for (let i = 0; i < rank; i++) {
const l = left[left.length - rank + i] ?? 1;
const r = right[right.length - rank + i] ?? 1;
if (l !== r && l !== 1 && r !== 1) {
throw new Error("tensor broadcast shape mismatch");
}
shape[i] = Math.max(l, r);
}
return shape;
}
private static broadcastOffset(sourceShape: number[], resultCoords: number[]): number | null {
const offset = resultCoords.length - sourceShape.length;
const coords = sourceShape.map((dim, axis) => dim === 1 ? 0 : resultCoords[axis + offset]);
return FaberTensor.offset(sourceShape, coords);
}
private static flatten(source: unknown): any[] {
if (!Array.isArray(source)) {
return [source];
}
return source.flatMap((value) => FaberTensor.flatten(value));
}
static empty<T>(shape: number[] = []): FaberTensor<T> {
return new FaberTensor<T>([], shape);
}
static fromArray<T>(source: unknown, shape: number[], convert: (value: any) => T = (value) => value as T): FaberTensor<T> {
const data = FaberTensor.flatten(source).map(convert);
if (FaberTensor.elementCount(shape) !== data.length) {
throw new Error("tensor conversio element count does not match shape");
}
return new FaberTensor<T>(data, shape);
}
crea(fill: T, shape: number[]): FaberTensor<T> {
return new FaberTensor<T>(new Array(FaberTensor.elementCount(shape)).fill(fill), shape);
}
strue(data: T[], shape: number[]): FaberTensor<T> {
if (FaberTensor.elementCount(shape) !== data.length) {
throw new Error("tensor structa element count does not match shape");
}
return new FaberTensor<T>(data, shape);
}
longitudo(): number {
return this.shape.length;
}
magnitudines(): Array<number> {
return this.shape.slice();
}
forma(shape: number[]): FaberTensor<T> {
if (FaberTensor.elementCount(shape) !== this.data.length) {
throw new Error("tensor forma (reshape) element count mismatch");
}
return new FaberTensor<T>(this.data, shape);
}
accipe(indices: number[]): T | null {
const offset = FaberTensor.offset(this.shape, indices);
return offset == null ? null : this.data[offset];
}
ponde(indices: number[], value: T): void {
const offset = FaberTensor.offset(this.shape, indices);
if (offset == null) {
throw new Error("tensor ponde invalid index");
}
this.data[offset] = value;
}
reple(value: T): void {
this.data.fill(value);
}
planata(): T[] {
return this.data.slice();
}
sectio(start: number, end: number): FaberTensor<T> {
if (start < 0 || end < 0 || end < start) {
throw new Error("tensor sectio invalid slice bounds");
}
const inner = FaberTensor.elementCount(this.shape.slice(1));
const shape = [end - start, ...this.shape.slice(1)];
return new FaberTensor<T>(this.data.slice(start * inner, end * inner), shape);
}
materialize(): FaberTensor<T> {
return new FaberTensor<T>(this.data, this.shape);
}
private elementwise(other: FaberTensor<T>, op: (left: any, right: any) => any): FaberTensor<T> {
const shape = FaberTensor.broadcastShape(this.shape, other.shape);
const data = new Array(FaberTensor.elementCount(shape));
for (let i = 0; i < data.length; i++) {
const coords = FaberTensor.coordinates(i, shape);
const left = FaberTensor.broadcastOffset(this.shape, coords);
const right = FaberTensor.broadcastOffset(other.shape, coords);
data[i] = op(this.data[left ?? 0], other.data[right ?? 0]);
}
return new FaberTensor<T>(data as T[], shape);
}
addita(other: FaberTensor<T>): FaberTensor<T> {
return this.elementwise(other, (left, right) => left + right);
}
subtrahe(other: FaberTensor<T>): FaberTensor<T> {
return this.elementwise(other, (left, right) => left - right);
}
multiplica(other: FaberTensor<T>): FaberTensor<T> {
return this.elementwise(other, (left, right) => left * right);
}
summa(): T {
return this.data.reduce((total: any, value: any) => total + value, 0) as T;
}
media(): number {
return this.data.length === 0 ? 0 : this.data.reduce((total: any, value: any) => total + value, 0) / this.data.length;
}
matmul(other: FaberTensor<T>): FaberTensor<T> {
if (this.shape.length !== 2 || other.shape.length !== 2) {
throw new Error("matmul requires rank-2 tensor");
}
const [m, k1] = this.shape;
const [k2, n] = other.shape;
if (k1 !== k2) {
throw new Error("matmul inner dimension mismatch");
}
const data = new Array(m * n).fill(0);
for (let row = 0; row < m; row++) {
for (let column = 0; column < n; column++) {
let total: any = 0;
for (let k = 0; k < k1; k++) {
total += (this.accipe([row, k]) as any) * (other.accipe([k, column]) as any);
}
data[row * n + column] = total;
}
}
return new FaberTensor<T>(data as T[], [m, n]);
}
}
(() => {
{
const flat_a: Array<number> = [1, 2, 3, 4, 5, 6];
const flat_b: Array<number> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
const seed: FaberTensor<number> = FaberTensor.empty<number>([]);
const a: FaberTensor<number> = seed.strue(flat_a, [2, 3]);
const b: FaberTensor<number> = seed.strue(flat_b, [3, 4]);
const product: FaberTensor<number> = a.matmul(b);
const mean: number = product.media();
console.log(__faberDisplay(mean, "fractus"));
}})();The error channel#
A function that may fail, and a caller that catches. Shows how the ⇥ channel becomes each target's own error idiom.
Faber source
faber format --locale en — English reader surfacefn divide(int a, int b) → int ⇥ string {
if b ≡ 0 {
throw "division by zero"
}
return a / b
}
main {
do {
print divide(10, 2)
}
catch err {
warn err
}
}faber format --locale la — canonical Faberfunctio divide(numerus a, numerus b) → numerus ⇥ textus {
si b ≡ 0 {
iace "division by zero"
}
redde a / b
}
incipit {
fac {
nota divide(10, 2)
}
cape err {
mone err
}
}faber format --locale th-TH — Thaiฟังก์ชัน divide(จำนวน a, จำนวน b) → จำนวน ⇥ ข้อความ {
ถ้า b ≡ 0 {
โยน "division by zero"
}
คืน a / b
}
เริ่ม {
ทำ {
บันทึก divide(10, 2)
}
จับ err {
เตือน err
}
}faber format --locale zh-Hans — Simplified Chinese函数 divide(整数 a, 整数 b) → 整数 ⇥ 文本 {
如果 b ≡ 0 {
抛错 "division by zero"
}
返回 a / b
}
入口 {
执行 {
显示 divide(10, 2)
}
捕获 err {
警告 err
}
}faber format --locale zh-Hant — Traditional Chinese函式 divide(整數 a, 整數 b) → 整數 ⇥ 文字 {
若 b ≡ 0 {
拋出 "division by zero"
}
傳回 a / b
}
入口 {
執行 {
註記 divide(10, 2)
}
捕捉 err {
警告 err
}
}faber format --locale vi — Vietnamesehàm divide(số a, số b) → số ⇥ văn_bản {
nếu b ≡ 0 {
ném "division by zero"
}
trả a / b
}
bắt_đầu {
làm {
ghi_chú divide(10, 2)
}
bắt err {
cảnh_báo err
}
}faber format --locale ar — Arabicدالة divide(عدد a, عدد b) → عدد ⇥ نص {
إذا b ≡ 0 {
ارم "division by zero"
}
أعد a / b
}
بداية {
افعل {
اعرض divide(10, 2)
}
التقط err {
نبه err
}
}faber format --locale hi — Hindiफलन divide(संख्या a, संख्या b) → संख्या ⇥ पाठ {
यदि b ≡ 0 {
इधरफेंको "division by zero"
}
लौटाओ a / b
}
आरंभ {
करो {
दिखाओ divide(10, 2)
}
पकड़ो err {
चेताओ err
}
}TypeScript — 13 lines in, 127 out (9.8×)
// Generated by radix - do not edit
type FaberResult<T, E> = { ok: true; value: T } | { ok: false; error: E };
type FaberDisplayHint =
"unknown"
| "textus"
| "ascii"
| "numerus"
| "fractus"
| "bivalens"
| "nihil"
| "vacuum"
| "valor"
| "instans"
| "regex"
| { kind: "nullable"; inner: FaberDisplayHint }
| { kind: "lista"; element: FaberDisplayHint }
| { kind: "tensor"; element: FaberDisplayHint }
| { kind: "sparsa"; element: FaberDisplayHint }
| { kind: "map"; key: FaberDisplayHint; value: FaberDisplayHint }
| { kind: "set"; element: FaberDisplayHint }
;
function __faberDisplay(value: any, hint: FaberDisplayHint = "unknown"): string {
if (typeof hint === "object") {
if (hint.kind === "nullable") {
return value === null || value === undefined ? "nihil" : __faberDisplay(value, hint.inner);
}
if (hint.kind === "lista") { return __faberDisplayList(value, hint.element); }
if (hint.kind === "tensor") { return __faberDisplayTensor(value, hint.element); }
if (hint.kind === "sparsa") { return __faberDisplaySparsa(value, hint.element); }
if (hint.kind === "map") { return __faberDisplayMap(value, hint.key, hint.value); }
return __faberDisplayList(value, hint.element);
}
if (value === null || value === undefined) {
return hint === "vacuum" ? "vacuum" : "nihil";
}
switch (hint) {
case "bivalens": return value ? "verum" : "falsum";
case "fractus": return __faberDisplayFractus(value);
case "nihil": return "nihil";
case "vacuum": return "vacuum";
case "valor": return __faberDisplayValor(value);
case "regex": return String(value);
case "textus":
case "ascii":
case "instans":
case "numerus": return String(value);
default: return __faberDisplayValor(value);
}
}
function __faberDisplayFractus(value: any): string {
const n = Number(value);
if (!Number.isFinite(n)) { return String(value); }
return Number.isInteger(n) ? `${n}.0` : String(n);
}
function __faberDisplayList(value: any, element: FaberDisplayHint): string {
if (value === null || value === undefined) { return "nihil"; }
return `[${Array.from(value as Array<any>).map((item) => __faberDisplay(item, element)).join(", ")}]`;
}
function __faberDisplayTensor(value: any, element: FaberDisplayHint): string {
if (value === null || value === undefined) { return "nihil"; }
if (typeof value.planata === "function") { return __faberDisplayList(value.planata(), element); }
if (Array.isArray(value.data)) { return __faberDisplayList(value.data, element); }
return __faberDisplayValor(value);
}
function __faberDisplaySparsa(value: any, element: FaberDisplayHint): string {
if (value === null || value === undefined) { return "nihil"; }
if (typeof value.densata === "function") { return __faberDisplayTensor(value.densata(), element); }
return __faberDisplayValor(value);
}
function __faberDisplayMap(value: any, keyHint: FaberDisplayHint, valueHint: FaberDisplayHint): string {
if (value === null || value === undefined) { return "nihil"; }
const entries = value instanceof Map ? Array.from(value.entries()) : Object.entries(value);
return `{${entries.map(([key, item]) => `${JSON.stringify(__faberDisplay(key, keyHint))}: ${__faberDisplay(item, valueHint)}`).join(", ")}}`;
}
function __faberDisplayValor(value: any): string {
if (value !== null && typeof value === "object" && typeof value.__faberValorTag === "string") { return __faberDisplayTaggedValor(value); }
if (value === null || value === undefined) { return "nihil"; }
if (typeof value === "boolean") { return value ? "verum" : "falsum"; }
if (typeof value === "number") { return String(value); }
if (typeof value === "string") { return value; }
if (Array.isArray(value)) { return __faberDisplayList(value, "valor"); }
if (typeof value.text === "function") { return String(value.text()); }
if (typeof value.toString === "function" && value.toString !== Object.prototype.toString) { return String(value); }
return __faberDisplayMap(value, "textus", "valor");
}
function __faberDisplayTaggedValor(value: any): string {
const payload = value.__faberValorPayload;
switch (value.__faberValorTag) {
case "Nihil": return "nihil";
case "Bivalens": return payload ? "verum" : "falsum";
case "Numerus": return String(payload);
case "Fractus": return __faberDisplayFractus(payload);
case "Textus": return String(payload);
case "Instans": return String(payload);
case "Octeti": return `<${Array.isArray(payload) ? payload.length : 0} bytes>`;
case "Lista": return __faberDisplayList(payload, "valor");
case "Tabula": return __faberDisplayMap(payload, "textus", "valor");
default: return __faberDisplayValor(payload);
}
}
function divide(a: number, b: number): FaberResult<number, string> {
if (((b as number) === (0 as number))) {
return { ok: false as const, error: "division by zero" };
}
return { ok: true as const, value: (a / b) };
}
(() => {
{
const t24: FaberResult<any, string> = (() => { console.log(__faberDisplay(divide(10, 2), "numerus"));
return { ok: true as const, value: undefined }; })();
if (!t24.ok) {
const err = t24.error;
{
console.warn(__faberDisplay(err, "textus"));
}}
}})();Collections and iteration#
A list folded to a total with itera ex. The plainest possible read on how loops lower.
Faber source
faber format --locale en — English reader surfacefn summa(list<int> numeri) → int {
var int total ← 0
for from numeri const n {
total ← total + n
}
return total
}
main {
const list<int> valores ← [1, 2, 3, 4, 5]
print summa(valores)
}faber format --locale la — canonical Faberfunctio summa(lista<numerus> numeri) → numerus {
varia numerus total ← 0
itera ex numeri fixum n {
total ← total + n
}
redde total
}
incipit {
fixum lista<numerus> valores ← [1, 2, 3, 4, 5]
nota summa(valores)
}faber format --locale th-TH — Thaiฟังก์ชัน summa(รายการ<จำนวน> numeri) → จำนวน {
แปร จำนวน total ← 0
วน ออก numeri คงที่ n {
total ← total + n
}
คืน total
}
เริ่ม {
คงที่ รายการ<จำนวน> valores ← [1, 2, 3, 4, 5]
บันทึก summa(valores)
}faber format --locale zh-Hans — Simplified Chinese函数 summa(列表<整数> numeri) → 整数 {
变量 整数 total ← 0
遍历 取自 numeri 常量 n {
total ← total + n
}
返回 total
}
入口 {
常量 列表<整数> valores ← [1, 2, 3, 4, 5]
显示 summa(valores)
}faber format --locale zh-Hant — Traditional Chinese函式 summa(列表<整數> numeri) → 整數 {
變值 整數 total ← 0
遍歷 取自 numeri 定值 n {
total ← total + n
}
傳回 total
}
入口 {
定值 列表<整數> valores ← [1, 2, 3, 4, 5]
註記 summa(valores)
}faber format --locale vi — Vietnamesehàm summa(danh_sách<số> numeri) → số {
biến số total ← 0
lặp từ numeri hằng n {
total ← total + n
}
trả total
}
bắt_đầu {
hằng danh_sách<số> valores ← [1, 2, 3, 4, 5]
ghi_chú summa(valores)
}faber format --locale ar — Arabicدالة summa(قائمة<عدد> numeri) → عدد {
متغير عدد total ← 0
كرر من numeri ثابت n {
total ← total + n
}
أعد total
}
بداية {
ثابت قائمة<عدد> valores ← [1, 2, 3, 4, 5]
اعرض summa(valores)
}faber format --locale hi — Hindiफलन summa(सूची<संख्या> numeri) → संख्या {
चर संख्या total ← 0
दोहराओ सेवन numeri स्थिर n {
total ← total + n
}
लौटाओ total
}
आरंभ {
स्थिर सूची<संख्या> valores ← [1, 2, 3, 4, 5]
दिखाओ summa(valores)
}TypeScript — 12 lines in, 121 out (10.1×)
// Generated by radix - do not edit
type FaberDisplayHint =
"unknown"
| "textus"
| "ascii"
| "numerus"
| "fractus"
| "bivalens"
| "nihil"
| "vacuum"
| "valor"
| "instans"
| "regex"
| { kind: "nullable"; inner: FaberDisplayHint }
| { kind: "lista"; element: FaberDisplayHint }
| { kind: "tensor"; element: FaberDisplayHint }
| { kind: "sparsa"; element: FaberDisplayHint }
| { kind: "map"; key: FaberDisplayHint; value: FaberDisplayHint }
| { kind: "set"; element: FaberDisplayHint }
;
function __faberDisplay(value: any, hint: FaberDisplayHint = "unknown"): string {
if (typeof hint === "object") {
if (hint.kind === "nullable") {
return value === null || value === undefined ? "nihil" : __faberDisplay(value, hint.inner);
}
if (hint.kind === "lista") { return __faberDisplayList(value, hint.element); }
if (hint.kind === "tensor") { return __faberDisplayTensor(value, hint.element); }
if (hint.kind === "sparsa") { return __faberDisplaySparsa(value, hint.element); }
if (hint.kind === "map") { return __faberDisplayMap(value, hint.key, hint.value); }
return __faberDisplayList(value, hint.element);
}
if (value === null || value === undefined) {
return hint === "vacuum" ? "vacuum" : "nihil";
}
switch (hint) {
case "bivalens": return value ? "verum" : "falsum";
case "fractus": return __faberDisplayFractus(value);
case "nihil": return "nihil";
case "vacuum": return "vacuum";
case "valor": return __faberDisplayValor(value);
case "regex": return String(value);
case "textus":
case "ascii":
case "instans":
case "numerus": return String(value);
default: return __faberDisplayValor(value);
}
}
function __faberDisplayFractus(value: any): string {
const n = Number(value);
if (!Number.isFinite(n)) { return String(value); }
return Number.isInteger(n) ? `${n}.0` : String(n);
}
function __faberDisplayList(value: any, element: FaberDisplayHint): string {
if (value === null || value === undefined) { return "nihil"; }
return `[${Array.from(value as Array<any>).map((item) => __faberDisplay(item, element)).join(", ")}]`;
}
function __faberDisplayTensor(value: any, element: FaberDisplayHint): string {
if (value === null || value === undefined) { return "nihil"; }
if (typeof value.planata === "function") { return __faberDisplayList(value.planata(), element); }
if (Array.isArray(value.data)) { return __faberDisplayList(value.data, element); }
return __faberDisplayValor(value);
}
function __faberDisplaySparsa(value: any, element: FaberDisplayHint): string {
if (value === null || value === undefined) { return "nihil"; }
if (typeof value.densata === "function") { return __faberDisplayTensor(value.densata(), element); }
return __faberDisplayValor(value);
}
function __faberDisplayMap(value: any, keyHint: FaberDisplayHint, valueHint: FaberDisplayHint): string {
if (value === null || value === undefined) { return "nihil"; }
const entries = value instanceof Map ? Array.from(value.entries()) : Object.entries(value);
return `{${entries.map(([key, item]) => `${JSON.stringify(__faberDisplay(key, keyHint))}: ${__faberDisplay(item, valueHint)}`).join(", ")}}`;
}
function __faberDisplayValor(value: any): string {
if (value !== null && typeof value === "object" && typeof value.__faberValorTag === "string") { return __faberDisplayTaggedValor(value); }
if (value === null || value === undefined) { return "nihil"; }
if (typeof value === "boolean") { return value ? "verum" : "falsum"; }
if (typeof value === "number") { return String(value); }
if (typeof value === "string") { return value; }
if (Array.isArray(value)) { return __faberDisplayList(value, "valor"); }
if (typeof value.text === "function") { return String(value.text()); }
if (typeof value.toString === "function" && value.toString !== Object.prototype.toString) { return String(value); }
return __faberDisplayMap(value, "textus", "valor");
}
function __faberDisplayTaggedValor(value: any): string {
const payload = value.__faberValorPayload;
switch (value.__faberValorTag) {
case "Nihil": return "nihil";
case "Bivalens": return payload ? "verum" : "falsum";
case "Numerus": return String(payload);
case "Fractus": return __faberDisplayFractus(payload);
case "Textus": return String(payload);
case "Instans": return String(payload);
case "Octeti": return `<${Array.isArray(payload) ? payload.length : 0} bytes>`;
case "Lista": return __faberDisplayList(payload, "valor");
case "Tabula": return __faberDisplayMap(payload, "textus", "valor");
default: return __faberDisplayValor(payload);
}
}
function summa(numeri: Array<number>): number {
let total: number = 0;
for (const n of numeri) {
total = (total + n);
}
return total;
}
(() => {
{
const valores: Array<number> = [1, 2, 3, 4, 5];
console.log(__faberDisplay(summa(valores), "numerus"));
}})();---