SultanC examples
Small complete programs, one idea per file, in Arabic and in English. Every example passes sultanc check.
Algorithms
- Binary search in a sorted vector
algorithms/binary_search.sn - Euclid's algorithm written by hand, checked against the standard library
algorithms/gcd.sn - Insertion sort in place through a mutable reference
algorithms/insertion_sort.sn - Multiplies two matrices stored in vectors
algorithms/matrix_multiply.sn - Sieve of Eratosthenes
algorithms/sieve.sn
Basics
- Integer arithmetic
basics/arithmetic.sn - Module-level constants are evaluated before main runs
basics/constants.sn - Prints a greeting and a computed number
basics/hello.sn - Fixed-width integers, hexadecimal literals and explicit casts with `as`,
basics/integer_types.sn
Characters
Checksums
Collections
- A bit set stores many yes/no flags in little memory
collections/bit_set.sn - A double-ended queue
collections/deque.sn - A B-tree map keeps its keys sorted, whatever the insertion order
collections/ordered_map.sn - A binary heap ordered by a comparison function
collections/priority_queue.sn - A hash set keeps each value once
collections/set.sn - A slice (`[] T`) is a view of a sequence that functions can take and return
collections/slices.sn - Sorting numbers and text, then binary-searching the sorted vector
collections/sorting.sn - Creating a vector, appending to it and reading from it
collections/vector.sn - Counting words with a text-keyed hash map
collections/word_count.sn
Compression
Compile time
- `compile_error` rejects a bad compile-time argument while compiling
comptime/compile_error.sn - `comptime` runs code while compiling; the program only sees the result
comptime/compile_time_values.sn - Decorators wrap a function with another ordinary function
comptime/decorators.sn - Type facts computed by the compiler for the selected target
comptime/reflection.sn - A `comptime` parameter makes one specialized copy of the function per value
comptime/specialization.sn
Control flow
- Conditions with if and else
control_flow/conditions.sn - A C-style `for` loop with `continue` and `break`
control_flow/for_loop.sn - Loops with while
control_flow/loops.sn - `match` on integers, ranges, characters, strings and booleans
control_flow/match_patterns.sn
Cryptography
- ChaCha20-Poly1305
crypto/encryption.sn - SHA-512 and HMAC-SHA-256, printed as hexadecimal
crypto/hashes.sn - SHA-256 of a byte vector, printed as hexadecimal
crypto/sha256.sn - Ed25519
crypto/signatures.sn
Data
- Types with variants, and matching on them
data/enums.sn - Nested records and access to their fields
data/records.sn - An algebraic data type whose variants carry records, destructured in `match`
data/shapes.sn - Tuples group values without naming a record; `match` takes them apart
data/tuples.sn - `type` gives an existing type a new name; `alias` gives a function one
data/type_aliases.sn
Encoding
Environment
Errors
- `assert` stops the program with exit code 1 when a condition is false
errors/assertions.sn - std.error
errors/error_values.sn - Phase 3 of the error system (docs/en/reference/errors.md), optional
errors/languages.sn - Phase 2 of the error system (docs/en/reference/errors.md)
errors/propagation.sn - Fallible operations return `result`; each error case is a typed value
errors/results.sn
FFI
Formatting
- Display and Debug for your own types
fmt/display.sn - std.fmt
fmt/formatting.sn - A table with Arabic names
fmt/table.sn
Data formats
- Reading CSV records and fields, including a quoted field with a comma
formats/csv.sn - Parsing a JSON document, reading a field, and writing it back out
formats/json.sn - Reading values from a TOML document, including a nested table
formats/toml.sn
Files
- Creating nested directories, listing and walking them, then removing the tree
fs/directories.sn - Writing a file, reading it back, then removing it
fs/files.sn
Functions
- Anonymous functions capture surrounding values and can be returned
functions/closures.sn - Functions passed as values
functions/higher_order.sn - Calling a function through a value
functions/indirect_call.sn - A function that calls itself
functions/recursion.sn
Generics
- A generic function that works with any type
generics/identity.sn - A generic ADT and generic functions instantiated with several types
generics/maybe.sn - A generic stack record backed by a vector
generics/stack.sn
Identifiers
Input and output
- Buffering many small writes and flushing them in one go
io/buffered_output.sn - A logger writes to standard error and drops messages below its level;
io/logging.sn - Reading a line from standard input
io/read_input.sn
Math
Memory
Networking
Numbers
Ownership
Processes
- Command-line arguments
process/arguments.sn - Running another program and waiting for its exit status
process/child_process.sn - `exit` ends the program at once with the given status (here 3)
process/exit_code.sn
Programs
Randomness
Strings
- Building text piece by piece with a text builder
strings/builder.sn - Searching, splitting, joining, trimming and replacing text
strings/search_and_replace.sn - Strings in "" quotes with escapes, and in «» quotes
strings/text.sn - Text is UTF-8
strings/unicode.sn