brain
tamnd's digital brain — notes, problems, research
43815 notes
The first program in Zig is small.
@compileError stops compilation with a custom error message.
@panic stops the program immediately with a message.
Zig code should be explicit, simple, and easy to inspect. The goal is not cleverness. The goal is code that another programmer can read, verify, and maintain.
Contributing to Zig means helping the language, compiler, standard library, documentation, tests, or tooling improve.
A lexer is the first stage of many programming language tools.
A cross-platform Zig program should not pretend that every operating system behaves the same way. Windows, Linux, macOS, WebAssembly, and embedded targets have different...
A compiler translates one form of code into another form.
Benchmarking means measuring how fast code runs.
A TCP server is a program that waits for clients to connect.
An event loop is code that waits for events, then runs the right piece of work for each event.
ABI means Application Binary Interface.
Memory debugging means finding mistakes in how a program uses memory.
Zig has built-in support for tests.
Programs often need to work with time.
@floatCast converts one floating-point value to another floating-point type.
This appendix lists useful Zig projects to read after you know the basics.
A programming language is never only its syntax.
A database stores data so it can be saved, searched, updated, and loaded again later.
A portable API is an interface that works across more than one platform.
A virtual machine is a program that runs another program.
Branch prediction is a CPU optimization.
A network protocol is a rulebook for how programs talk over a network.
await means: wait until an asynchronous operation has finished, then continue with its result.
Zig can call C, but C can also call Zig.
GDB and LLDB are debuggers.
Zig can build the same program in different optimization modes.
Parsing means turning text into data.
A tree is a collection made of nodes.
An allocation-friendly API makes memory behavior clear to the caller.
@truncate converts an integer to a smaller integer type by keeping only the low bits.
Zig is designed to work closely with C. You can call C from Zig, call Zig from C, compile C code with Zig, and link Zig programs against existing C libraries.
LLVM is a compiler infrastructure project.
A memory allocator is code that gives memory to the rest of a program.
Cross-target debugging means debugging a program built for a different machine, operating system, or CPU architecture than the one you are sitting at.
A parser reads text and turns it into structure.
Copying data is sometimes necessary, but unnecessary copying is one of the easiest ways to waste time and memory.
Endianness means the order used to store the bytes of a multi-byte value.
Async code lets a program start an operation now and receive the result later.
A mixed Zig and C project contains source files from both languages.
An assertion is a check that must be true while the program runs.
Linking is the step where the compiler connects your program with the code it depends on.
Formatting means turning values into text.
A ring buffer is a fixed-size queue that reuses its storage.
Memory lifetime means:
@intCast converts one integer value to another integer type.
comptime is one of Zig’s strongest features, but it should not be used everywhere.
When Zig compiles a program, it can build the program in different modes.
Code generation is the compiler stage that turns analyzed program meaning into target code.
A thread pool is a group of worker threads that wait for jobs.
ARM is a CPU architecture family used in phones, tablets, laptops, routers, Raspberry Pi boards, microcontrollers, servers, and many embedded devices. When you write Zig for...
A game engine core is the small central layer that runs the game.
Allocations are one of the most common causes of slow programs.
A binary file format stores data as bytes with a specific structure.
A condition variable lets one thread sleep until another thread says that something has changed.
Importing a C header lets Zig understand a C API. Linking gives the final program the actual compiled code.
A stack trace shows how your program reached a failure.
Cross compilation means building a program for a different machine than the one you are using.
A file lives inside a directory.
A bit set is a compact collection of yes-or-no values.
A memory leak happens when a program allocates memory and then loses the ability to free it.
@memcpy copies bytes from one memory region to another.
Metaprogramming means writing code that helps create, inspect, or specialize other code.
Most memory in Zig is ordinary memory.
A clear explanation of finding products sold only in the first quarter of 2019 using GROUP BY with date range conditions.
A clear explanation of finding the lexicographically smallest subsequence with all distinct characters using a greedy stack approach.
A clear explanation of finding the minimum total Manhattan distance to assign bikes to workers using bitmask dynamic programming.
A clear explanation of finding the longest string that divides both strings using the GCD of their lengths.
A clear explanation of sorting matrix cells by Chebyshev distance from a given center cell using BFS.
A clear explanation of adding two non-positive integers represented as arrays of digits.
A clear explanation of finding the complement of a number by XORing with a bitmask of the same bit length.
A clear explanation of finding the fraction of players retained the day after their first login using self-join and window functions.
A clear explanation of finding the shortest string containing both input strings as subsequences using LCS dynamic programming.
A clear explanation of finding the maximum number of rows that can be made all-equal by flipping columns, using row pattern normalization.
A clear explanation of determining if a source can reach a target in a very large grid with blocked cells using BFS with a cell count limit.
A clear explanation of finding all words that follow a two-word sequence in a text string.
A clear explanation of finding the best seller(s) by total price using GROUP BY, SUM, and a subquery for the maximum.
A clear explanation of checking divisibility of binary prefixes by 5 using running remainder tracking.
A clear explanation of greedily assigning bikes to workers based on Manhattan distance, prioritizing by distance then worker then bike index.
A clear explanation of determining if a robot stays in a bounded circle by checking position and direction after one instruction cycle.
A clear explanation of converting a BST to a greater sum tree by accumulating values in reverse inorder traversal.
A clear explanation of maximizing the sightseeing score by tracking the best left value seen so far in a single pass.
A clear explanation of coloring the border of a connected component in a grid using BFS.
A clear explanation of counting land cells unreachable from the grid border using BFS from boundary land cells.
A clear explanation of minimizing total travel cost for two-city scheduling using a greedy refund approach after sending everyone to city A.
A clear explanation of computing total quantity sold per product using GROUP BY and SUM aggregation.
A clear explanation of finding characters that appear in all words using minimum frequency counts.
A clear explanation of converting a non-negative integer to its base negative-two representation.
A clear explanation of finding the maximum sum of two numbers less than k using a two-pointer approach on a sorted array.
A clear explanation of pruning tree nodes where all root-to-leaf paths through them have sum less than a limit, using post-order DFS.
A clear explanation of eliminating adjacent duplicate character pairs from a string using a stack.
A clear explanation of finding actor-director pairs with at least three collaborations using GROUP BY and HAVING.
A clear explanation of generating all strings from a brace expansion expression using recursive parsing and set union/concatenation.
A clear explanation of finding the shortest path from top-left to bottom-right in a binary matrix using BFS.
A clear explanation of validating a string by repeatedly removing 'abc' substrings using a stack.
A clear explanation of minimizing total rounding error when rounding prices to meet a target sum using a greedy approach.
A clear explanation of reconstructing a binary tree from a depth-encoded preorder traversal string using a stack.
A clear explanation of checking if a number becomes a different valid number when rotated 180 degrees.
A clear explanation of finding buyers who bought an iPhone but not an iPad using JOIN and NOT IN filtering.
A clear explanation of duplicating zeros in-place in an array without using extra space by working backwards.