File size: 2,443 Bytes
d086025 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | # Standard library
Modules are loaded with `import` and used through their name (for example
`math.sqrt(...)`). Built-in functions listed below need no import.
---
## Built-in functions
| Function | Purpose |
| --- | --- |
| `show(...)` | Print values without a trailing newline. |
| `showln(...)` | Print values as a line. |
| `format(template, ...args)` | Format values with `{}` placeholders and style options such as `{:.2f}`, `{:>6}`. |
| `typeof(value)` | Runtime type name, or class name for instances. |
| `str(v)`, `int(v)`, `float(v)`, `bool(v)` | Convert between types. |
| `len(collection)` | Length of a list, map or string. |
| `sleep(seconds)` | Pause execution. |
| `argv` | Command-line arguments passed after the script name. |
Methods available on values: `list.push(v)`, `list.map(f)`, `list.filter(f)`,
`list.reduce(f, init)`, `list.sort()`, `map.set(key, value)`,
`str.trim() / .upper() / .lower() / .split(sep) / .contains(sub) /
.replace(old, new)`.
---
## Modules
### `math`
Numeric and math utilities.
```dragon
import math
math.sqrt(144) // 12
math.abs(-7) // 7
math.floor(9.8) // 9
math.range(5) // [0, 1, 2, 3, 4]
math.range(1, 20) // 1..19
PI // constant, also available unqualified
```
### `json`
```dragon
import json
text = json.encode({ name: "Ember", level: 42 })
data = json.decode(text)
showln(data.name, data.level)
```
### `file` and `path`
Read, write and inspect files, and manipulate path strings (join, split,
extension, existence checks).
### `os`
Environment variables and system information.
### `time`
Dates, timers and sleeping.
### `net`
HTTP client and server utilities.
### `crypto`
Hashing (SHA-2, MD5), HMAC, PBKDF2 and AES-GCM encryption.
### `zip`
Create and extract ZIP archives.
### `xml`
Parse XML documents.
### `uuid`
Generate UUIDs.
### `pdf`, `font`
Render PDF documents and font/text output.
### `thread`
```dragon
import thread
thread.spawn(worker, [1]) // run worker(1) concurrently
```
### `ffi`, `struct`, `mem`
Native interop with C libraries: call into shared objects (`ffi`), describe C
data layouts (`struct`), and work with raw memory (`mem`).
---
## Importing your own modules
Scripts can include other `.dgn` files; when a script is compiled with
`dragon -c`, everything it imports is packaged into the produced executable, so
the result runs standalone on another machine.
|