Builtins¶
Desi provides built-in functions that are always available without any import. These are part of the language prelude.
Type Conversions¶
str(42) # "42"
str(3.14) # "3.14"
str(true) # "true"
int("42") # 42
float("3.14") # 3.14
bool(1) # true
bool(0) # false
Character & Encoding¶
chr(n)¶
Convert a Unicode codepoint (integer) to a single-character string:
ord(s)¶
Convert the first character of a string to its Unicode codepoint:
Roundtrip: ord(chr(n)) == n and chr(ord(s)) == s (for single-char strings).
Number Formatting¶
hex(n)¶
Format an integer as a hexadecimal string with 0x prefix:
oct(n)¶
Format an integer as an octal string with 0o prefix:
bin(n)¶
Format an integer as a binary string with 0b prefix:
Math¶
abs(n)¶
Return the absolute value of a number (works with both int and float):
round(n, digits=0)¶
Round a float to the specified number of decimal places:
pow(base, exp)¶
Integer exponentiation:
Collections¶
| Function | Description |
|---|---|
len(x) |
Length of string, list, dict, set, or tuple |
sum(items) |
Sum of a list of numbers |
min(items) |
Minimum value in a list |
max(items) |
Maximum value in a list |
any(items) |
true if any element is truthy |
all(items) |
true if all elements are truthy |
sorted(items) |
New sorted list (ascending) |
sorted(items, reverse=true) |
New sorted list (descending) |
reversed(items) |
Iterator in reverse order |
zip(a, b) |
Pair elements from two iterables |
enumerate(items) |
Iterator of (index, element) pairs |
Functional¶
| Function | Description |
|---|---|
map(fn, items) |
Apply function to each element |
filter(fn, items) |
Keep elements where function returns true |
reduce(fn, items, initial) |
Left fold |
foldl(fn, items, initial) |
Left fold (alias for reduce) |
foldr(fn, items, initial) |
Right fold |
I/O¶
| Function | Description |
|---|---|
print(...) |
Print values separated by space, ending with newline |
input(prompt) |
Read a line from stdin |
open(path, mode) |
Open a file |
Debugging¶
dbg(expr)¶
Print [file:line] expr = value and return the value unchanged:
type_of(value)¶
Get runtime type information:
todo()¶
Panic with "not implemented" — useful as a placeholder during development:
Inspired by Rust's todo!() macro. Calling todo() will print a message and exit with code 1.
unreachable()¶
Mark a code path that should never be reached. Panics immediately if called:
def classify(n: int) -> str:
if n > 0:
return "positive"
elif n < 0:
return "negative"
else:
return "zero"
def main() -> int:
let kind = classify(5)
if kind == "positive" or kind == "negative" or kind == "zero":
print("path: " + kind)
else:
unreachable()
0
Use unreachable() to document that a branch is impossible and catch logic errors early.
Identity & Hashing¶
| Function | Description |
|---|---|
hash(value) |
Hash of a value (FNV-1a of pointer) |
id(value) |
Unique identity integer for an object |
Testing¶
| Function | Description |
|---|---|
assert(condition) |
Panic if condition is false |
assert(condition, msg) |
Panic with message if false |
assert_eq(expected, actual) |
Panic if values differ |
assert_ne(a, b) |
Panic if values are equal |
See Also¶
- Math Module — Additional math functions (
sqrt,sin,cos, etc.) - Strings Module — String manipulation functions