35. Esempi
Examples/ sui capitoli. 35.2–35.5 sono un snippet per traccia (agente, game, UI, property). Lo scaffolding è 35.6. I listing completi stanno in Examples/README.md.
Il corso core termina all'eval offline del prompt (Examples/Basics/prompt_eval.malda), non al gradebook. Dopo, segui Imparare MALDA — Dopo il percorso core.
35.0 First Look
Costrutti caratteristici, senza API key. Stesso programma di Examples/Basics/first_look.malda e del Quick Start in Indice. -> Review lega il prompt allo schema. Senza await la chiamata è un template reso; validate("Review", …) è lo stesso controllo che await eseguirebbe sul JSON del modello. Sintassi dello schema: 4.6.
schema Review {
summary: string;
issues: string[];
}
prompt codeReview(code, language) -> Review {
system: "You are an expert reviewer of {language}.",
user: "Review this {language} code:\n\n{code}"
}
var rendered = codeReview("function add(a, b) { return a + b; }", "javascript");
io.print(rendered.user);
var checked = validate("Review", {
"summary": "Looks fine",
"issues": []
});
if (checked.ok) {
io.print("schema ok: " + checked.data.summary);
} else {
io.print("schema failed: " + checked.error);
}
35.1 Catalogo Examples
Il metadata.json di ogni cartella può etichettare requires: offline (default), network, api-key o db. Hello World è Examples/Basics/hello_world.malda — capitolo 12, non un secondo listing qui. Lo starter completo è Examples/Basics/complete_starter_program.malda. Il mini progetto del corso è Examples/Basics/gradebook.malda. Il corso core termina a Examples/Basics/prompt_eval.malda.
| Cartella | Focus | Requires tipici | Capitolo |
|---|---|---|---|
Basics/ | Sintassi, controllo di flusso, funzioni, schema + validate | offline | 3–12, 4.6 |
Algorithms/ | Hanoi, search, sort, DP, RANSAC | offline | 8, 9 |
OOP/ | Classi e ereditarietà | offline | 11 |
Prompts/ | prompt, pipe RAG, evalPrompt | api-key (la maggior parte) | 10 |
Agents/, AI_LLM/ | Agenti, tool, client LLM | api-key | 19 |
Web/ | HttpServer, REST, ui.*, JS DOM | network / offline | 25–31 |
Games/ | game.* / three.* (target JS) | offline | 28 |
Workflows/ | workflow / step durevoli | offline | 23 |
Testing/ | malda test, property | offline | 36 |
Actors/ | spawn / send / on | offline | 18 |
ACP/, MCP/ | Agent / Model Context Protocol | network / api-key | 22, 21 |
Databases/, Graphs/, VectorDB/ | SQL, grafi, embedding | db / offline | 17, 15, 16 |
Memory/, Tools/, Plan/ | GraphMemory, tool su file, plan | api-key / offline | 20, 19 |
Assistant/, Arduino/ | Assistente + skill; sketch dispositivi | api-key / hardware | 34, 33 |
Altre cartelle (AI_Theory/, SpectreConsole/, RalphWiggum/, …) sono in Examples/README.md. Il kit reti neurali (math.matmul, percettrone / XOR / softmax, OnnxModel) è 14. Reti neurali. La convoluzione è 14.11.6. Una rete fatta di reti più piccole è 14.10.1. Pipe RAG: Examples/Prompts/rag_pipeline.malda — capitoli 10 e 16, non un dump qui.
35.2 Agente
Solo host: serve un client LLM. Stessa idea di Examples/Agents/. Linguaggio: 19. Orchestrazione di agenti.
var client = new OpenRouterClient();
var agent = new Agent(
"Assistant",
"helper",
"You are a helpful assistant.",
client
);
var response = agent.think("What is 2+2?");
io.print(response.content);
35.3 Game
Solo target JavaScript. Anteprima con malda play app.malda (reload live salvo --no-watch). Canvas / three.* / @shader() restano in 28. Stesso paddle di Examples/Games/game_bounce.malda:
var x = 100;
var speed = 220;
function update(dtMs) {
var step = (speed * dtMs) / 1000;
if (game.isKeyDown("arrowleft")) {
x = x - step;
}
if (game.isKeyDown("arrowright")) {
x = x + step;
}
}
function render() {
game.clear();
game.fillRect(x, 140, 40, 40, "#33cc66");
game.drawText("Use arrows", 10, 24, "#ffffff", "14px monospace");
}
game.createCanvas(640, 360, "#app");
game.setBackground("#202830");
game.start(update, render);
Crea il canvas prima di disegnare. Scala il movimento con dtMs. Carica malda-js-runtime.js prima dello script compilato. Biliardo GPU giocabile: 39.
35.4 UI guidata dal server
API del linguaggio: 26 (parti da 25). Sample offline in Examples/Web/: ui_event_loop.malda, ui_counter_dashboard.malda, ui_form_workflow.malda.
var tree = ui.column({
"children": [
ui.text({ "text": "Hello" }),
ui.button({ "id": "ok", "text": "OK" })
]
});
ui.mount("root", tree);
ui.render();
35.5 Property test
property intIdentity(x) {
return (x + 0) == x;
}
malda test --iterations 80 --seed 1337
malda test --format ci
malda test --write-regression --regression-dir ./artifacts/regressions
Guida completa: 36. Property testing. Sample: Examples/Testing/.
35.6 Scaffolding
Indice CLI: 2.0. Flusso full-stack: 31.
malda new webapi my-api
malda new fullstack my-app
malda new game my-game
malda new game my-scores --fullstack
malda new agent my-agent
malda new webapi my-api --name SalesPortal
malda new fullstack . --force
malda new webapi api-lite --no-tests
Gli starter generati includono le convenzioni di default di malda test e placeholder di config. Il passo successivo del template game è malda play app.malda. malda new game --fullstack è un'API punteggi compilata con --mode fullstack, non malda play. Il template agent è offline e cap-confined; il passo successivo è malda app.malda.
Vedi anche
- 4.6 Schema —
schema { }+validate - 11. Classi e oggetti — walkthrough OOP (
Examples/OOP/) - 17. Supporto database — SQLite, Postgres, SQL Server
- 19. Orchestrazione di agenti — agenti e tool
- 26. Componenti server Web UI —
ui.* - 28. Backend UI JavaScript nel browser —
game.*/three.* - 36. Property testing — shrinking e regressioni