Sample 03 — Code, Algorithms, and Small Projects
Copyable code-fence patterns for filenames, emphasis, terminal frames, folds, typed virtual projects, and academic or code-style pseudocode.
Source: https://lapotron.com/article/sample-code-and-algorithms/
A code example should make its language, context, and important lines easy to find. This article develops a tiny statistics module while demonstrating code presentation and pseudocode. The examples are displayed as source; the public article does not execute them.
Copy the outer Markdown when you want the same presentation in your own article. Copy the rendered code when you want the program text itself.
Give a code block a useful filename
Put the language directly after the opening backticks. A title adds the filename to the compact toolbar, and {3-5} emphasizes the lines worth studying.
The empty-list condition makes the contract explicit. For a production library, also decide how to handle nonfinite inputs and numerical precision. This sample focuses on small finite numbers.
Try it: use the code toolbar to copy the example, toggle wrapping, or download its source. Displayed line numbers are not part of the copied program.
Show what changed
Insertion, deletion, and text markers describe an edit without requiring a separate diff viewer.
These are presentation markers: a deleted-looking line still exists in the source and in a complete copy of the block. Edit the program itself when applying the change. Here the previous constant is unused, so the example remains valid source.
Connect files explicitly in Studio
Fences are independent by default. Give related JavaScript or TypeScript examples the same project value and different filename titles to create an article-local virtual project.
In the Studio source editor, completion, hover signatures, diagnostics, definition navigation, references, and rename can use these virtual files. F12 goes to a definition, Shift+F12 finds references, F2 renames, and Ctrl/Cmd+. opens available code actions. Those are editing features, not controls attached to the published reader's code blocks.
Virtual project names do not create files on disk. Relative imports can connect examples in the same article; external package declarations are not downloaded. If you copy the program into a real project, supply the corresponding files and build environment there.
For an intentionally incomplete exercise, check="off" suppresses Studio semantic diagnostics for that fence. It does not execute the example or repair its missing pieces.
Fold an implementation detail
Folding makes a longer example easier to scan while retaining its full source. A range is inclusive and uses the block's own source line numbers.
3 collapsed lines
Try it: expand the folded loop, then copy the block. The complete implementation is retained even when the loop is folded. The example returns complete trailing windows only; a list shorter than the window produces an empty result.
Wrap long lines without changing the source
Use wrap for a configuration line that should fit the available reading width. Visual wrapping never inserts newlines into the copied string.
For a very short command or output, showLineNumbers=false removes unnecessary numbering.
Label terminal examples clearly
A terminal frame tells the reader that the block contains a command rather than a source file. This command validates this sample from the Glass project root; it is shown here without being run by the article.
npm run markdown:check -- content/lab/sample-code-and-algorithms.md --jsonKeep actual output separate from commands when the distinction matters. Do not present fabricated benchmark output as a measurement.
Explain an algorithm in academic form
Pseudocode lets the structure of an algorithm take priority over a programming language's syntax. The default academic style supports inline math. An enclosing algorithm provides a numbered label and cross-reference.
Algorithm 1 · Mean of a nonempty list
Algorithm 1 keeps the nonempty-input assumption visible. Each value contributes once, so the algorithm uses a number of additions proportional to the input length and a constant amount of extra storage, apart from the input itself.
Prefer code-style pseudocode for implementation notes
The same language accepts style="code". Its monospace presentation retains any $...$ notation literally rather than turning it into inline math.
Pseudocode is explanatory notation. It is not a runnable language, and the renderer does not verify the algorithm's correctness. State assumptions and check the reasoning just as you would for a prose description.
A small publishing checklist
- Choose a supported language and a descriptive filename.
- Explain the important line in the surrounding prose.
- Keep related virtual filenames unique within each project.
- Check copied source, not only the highlighted appearance.
- Use folds and wrapping to improve reading without hiding essential assumptions.
If the code needs an argument or a diagram around it, keep that explanation in the article instead of placing every sentence in another code block.