The catalogue of examples
What ready-made flang programs are there, and how do you run them? They are all in the docs/examples/ directory of the repository: 461 .flang files. To run them yourself, clone the repository and install flang (Installation).
The number of files comes from the command git -c core.quotepath=false ls-files docs/examples | grep -c '\.flang$' → 461.
How to run them
Tests are written inside functions with the word пример (example). The command flang test runs the tests of one file or of every file in a directory:
flang test docs/examples/dsl/shipping.flang # one file
flang test docs/examples/leetcode/ # one set
The first command answers:
docs/examples/dsl/shipping.flang: примеров 8, прошло 8, не прошло 0
It reads "8 tests, 8 passed, 0 failed". For a directory the last line of the output counts the files and tests of the whole set. The time depends on the set: leetcode (82 files, 1010 tests) takes about half a minute, crypto about twenty minutes, because it computes AES and ECDSA test vectors. If a file fails the check, the command names it and the error code.
Sets by topic
| Directory | .flang files | About what |
|---|---|---|
leetcode/ | 82 | solutions to LeetCode problems, each in one file with no dependencies. The study of the set is 82 problems |
rosetta/ | 28 | Rosetta Code tasks, each with the Russian and with the English keywords. The study of the set is Rosetta Code |
web/ | 17 | HTTP: an orders service, the link shortener, a shop API of three services, a program for the browser |
io/ | 10 | input and output through effects: a binary file, bytes over the network, a child process, an HTTPS request, a temporary directory |
crypto/ | 9 | AES, ECDSA, RSA-PSS, X25519, X.509, a certificate revocation list, the first TLS message — on test vectors from the standards |
https/ | 7 | TLS: the handshake and records of RFC 8448, the system certificate store, a request to a real host |
db/ | 6 | talking to PostgreSQL (SCRAM included) and Redis, reading an SQLite file |
dsl/ | 14 | your own small language: discount rules, a calculator, an order state machine, shipping and access rules. The recipe is Your own language in 30 minutes |
frameworks/ | 6 | flang logic inside NestJS, React and Vue applications — see below |
library-api/ | 7 | the logic of a library REST service: lending, catalogue, fines. The only set made of several modules with its own directory layout; the layout rules are in Project layout |
service-on-processes/ | 4 | a service made of several processes that answers over the network |
behavior/ | 4 | business rules in two versions for the behavior table: access to a report, a discount on a total |
driver/ | 3 | device drivers made of always-terminating functions: a UART and MSI |
proof-probes/ | 3 | programs that test the proof kernel itself |
measure/ | 3 | termination by a declared measure (убывает, decreases): Euclid's algorithm, binary search, natural numbers |
host-boundary/ | 2 | flang computes a decision, a C program carries it out. Explained in The boundary between flang and the host |
errors/ | 2 | an error as a value: summing a column of numbers that arrived as strings; parsing numbers |
wal/ | 2 | a write-ahead log: reading, writing, recovery after a truncation |
numbers/ | 2 | Church numerals and the laws of rational numbers |
genetic/ | 2 | a genetic algorithm that evolves a given string |
pythagoras/ | 2 | what part of the Pythagorean theorem can be written in flang: Euclid's formula for triples |
surfaces/ | 2 | factorial with Chinese keywords and in Esperanto |
package/ | 2 | a package with a manifest. Explained in Modules and packages |
allocator/ | 1 | a memory allocator as a pure state machine |
monad/ | 1 | an order total written with the в монаде form |
money/ | 1 | money on the exact decimal type сотых |
paths/ | 1 | the shortest path in a network where some nodes are unreachable |
service/ | 1 | a call to a service |
Files for the documentation pages
These sets are the examples shown on the pages of this site. Each page example is a separate file, so you can run it without copying it from the browser.
| Directory | .flang files | For which page |
|---|---|---|
reference/ | 80 | Language reference |
proofs/ | 38 | the Proofs section |
guide/ | 33 | Which construct to use when, Command reference |
tour/ | 24 | the language tour: page examples and exercises with solutions |
playground/ | 24 | the example ladders of the playground: from simple to complex in every setup. The texts for the C and Python translators, the arguments and the formulas lie there too |
diagnostics/ | 18 | Diagnostics reference |
usage/ | 7 | the guides in the docs/guide/ directory |
first-proof/ | 5 | the five steps of Your first proof |
cli/ | 3 | Command reference |
tutorial.flang, operations.flang, import-check.flang | 3 | single files: first steps, operations, importing a module by name |
flang logic inside a JavaScript application
docs/examples/frameworks/ holds four applications. In each the logic is written in flang and generated into JavaScript with flang emit … --target js. The framework handles the network, the form state and rendering, and calls the functions of the generated module.
| Directory | Framework | flang source | What it computes |
|---|---|---|---|
nestjs-orders/ | NestJS | core/orders-api.flang | an orders REST service: parsing the request, required fields, routing by method and path, the response code (201, 200, 400, 404, 405) |
react-invoice/ | React | core/cart.flang, core/catalog.flang | a cart invoice in kopecks: the sum, discount tiers, the total, the invoice lines |
vue-roman/ | Vue | core/roman-numerals.flang | Roman numerals both ways for numbers from 1 to 3999 |
react-ts-pure/ | React and TypeScript | core/storefront.flang | a storefront with all its logic in flang: the cart, discounts, writing a sum in roubles, the reducer |
The generated module is in the printed/ directory of each example. It has no dependencies and runs in Node and in the browser. A flang list becomes an array, a record becomes an object. Function names are transliterations of the Russian ones: «В римские» → vRimskie, «Сумма корзины» → summaKorziny.
You can check a generated module without installing any packages:
cd docs/examples/frameworks/vue-roman && node --input-type=module -e \
'import("./printed/roman_numerals.js").then(m => console.log(m.vRimskie(2024), m.izRimskih("MCMLXXXIV")))'
MMXXIV 1984
The application itself is built with the commands from its package.json: npm install, npm run build. That needs packages from the network. The files in printed/ are not edited by hand; a command generates them again, for example flang emit docs/examples/frameworks/vue-roman/core/roman-numerals.flang --target js --out docs/examples/frameworks/vue-roman/printed.
Next
- What to read next — the reading order of the documentation
- Leetcode tasks — what is proved in them and what is not
- The link shortener — one program explained in full