348 lines
7.8 KiB
Markdown
348 lines
7.8 KiB
Markdown
---
|
|
Title: A Standard Library
|
|
Prev: Advanced Topics
|
|
Next: Complete Trait Reference
|
|
---
|
|
|
|
## Appendix A: Standard Library
|
|
|
|
This appendix provides a complete alphabetical reference of all standard library functions and operations.
|
|
|
|
### Standard Library Categories
|
|
|
|
**Array Operations**: at, concat, each, filter, length, map, reduce, reverse, shape, slice, transpose, window
|
|
|
|
**I/O Operations**: input, print, read, write
|
|
|
|
**String Operations**: concat, join, length, split, substr
|
|
|
|
**Type Conversion**: parse, to_i8, to_i16, to_i32, to_i64, to_u8, to_u16, to_u32, to_u64, to_f32, to_f64, to_str
|
|
|
|
**Utility Operations**: eval, lambda
|
|
|
|
### Alphabetical Reference
|
|
|
|
#### at
|
|
**Signature**: `(Selectable<T> Size -- T)`
|
|
**Trait**: Selectable<T>
|
|
**Description**: Access element at the given index.
|
|
**Example**:
|
|
```
|
|
[10 20 30] 1 at // Returns 20
|
|
```
|
|
**See Also**: slice, length
|
|
|
|
#### concat
|
|
**Signature**: `(Concatenable Concatenable -- Concatenable)`
|
|
**Trait**: Concatenable
|
|
**Description**: Concatenate two containers or strings.
|
|
**Example**:
|
|
```
|
|
[1 2 3] [4 5 6] concat // Returns [1 2 3 4 5 6]
|
|
"hello" " world" concat // Returns "hello world"
|
|
```
|
|
**See Also**: join, split
|
|
|
|
#### depth
|
|
**Signature**: `(-- Size)`
|
|
**Trait**: Stackable
|
|
**Description**: Push current stack depth onto the stack.
|
|
**Example**:
|
|
```
|
|
1 2 3 depth // Returns 1 2 3 3
|
|
```
|
|
**See Also**: pick, roll
|
|
|
|
#### drop
|
|
**Signature**: `(Self --)`
|
|
**Trait**: Stackable
|
|
**Description**: Remove and discard the top item from the stack.
|
|
**Example**:
|
|
```
|
|
5 10 drop // Returns 5
|
|
```
|
|
**See Also**: dup, swap
|
|
|
|
#### dup
|
|
**Signature**: `(Self -- Self Self)`
|
|
**Trait**: Stackable
|
|
**Description**: Duplicate the top item on the stack.
|
|
**Example**:
|
|
```
|
|
5 dup // Returns 5 5
|
|
```
|
|
**See Also**: drop, over
|
|
|
|
#### each
|
|
**Signature**: `(ArrayOf<T> TokenString --)`
|
|
**Description**: Apply function to each element (side effects).
|
|
**Example**:
|
|
```
|
|
[1 2 3] { print } each // Prints 1, 2, 3
|
|
```
|
|
**See Also**: map, filter
|
|
|
|
#### eval
|
|
**Signature**: `(TokenString --)`
|
|
**Trait**: Implementable
|
|
**Description**: Parse and execute TokenString as code.
|
|
**Example**:
|
|
```
|
|
"2 3 +" eval // Returns 5
|
|
```
|
|
**See Also**: lambda
|
|
|
|
#### filter
|
|
**Signature**: `(ArrayOf<T> TokenString -- ArrayOf<T>)`
|
|
**Description**: Keep only elements matching predicate.
|
|
**Example**:
|
|
```
|
|
[1 2 3 4 5] { 2 % 0 == } filter // Returns [2 4]
|
|
```
|
|
**See Also**: map, reduce
|
|
|
|
#### input
|
|
**Signature**: `(String -- String)`
|
|
**Description**: Print prompt and read line from stdin.
|
|
**Example**:
|
|
```
|
|
"Enter name: " input // Prompts and returns user input
|
|
```
|
|
**See Also**: print, read
|
|
|
|
#### join
|
|
**Signature**: `(ArrayOf<String> String -- String)`
|
|
**Description**: Join array of strings with delimiter.
|
|
**Example**:
|
|
```
|
|
["a" "b" "c"] "," join // Returns "a,b,c"
|
|
```
|
|
**See Also**: split, concat
|
|
|
|
#### lambda
|
|
**Signature**: `(TokenString -- Callable)`
|
|
**Trait**: Implementable
|
|
**Description**: Convert TokenString to callable code block.
|
|
**Example**:
|
|
```
|
|
{ dup * } lambda ::square swap
|
|
5 square eval // Returns 25
|
|
```
|
|
**See Also**: eval
|
|
|
|
#### length
|
|
**Signature**: `(Sized -- i64)`
|
|
**Trait**: Sized
|
|
**Description**: Get the number of elements in a container.
|
|
**Example**:
|
|
```
|
|
[1 2 3 4 5] length // Returns 5
|
|
"hello" length // Returns 5
|
|
```
|
|
**See Also**: at, slice
|
|
|
|
#### map
|
|
**Signature**: `(ArrayOf<T> TokenString -- ArrayOf<U>)`
|
|
**Description**: Transform each element with function.
|
|
**Example**:
|
|
```
|
|
[1 2 3 4] { 2 * } map // Returns [2 4 6 8]
|
|
```
|
|
**See Also**: filter, reduce, each
|
|
|
|
#### over
|
|
**Signature**: `(Self Self -- Self Self Self)`
|
|
**Trait**: Stackable
|
|
**Description**: Copy the second item to the top of the stack.
|
|
**Example**:
|
|
```
|
|
5 10 over // Returns 5 10 5
|
|
```
|
|
**See Also**: dup, swap, rot
|
|
|
|
#### parse
|
|
**Signature**: `(String -- Parseable)`
|
|
**Trait**: Parseable
|
|
**Description**: Parse string to value of inferred type.
|
|
**Example**:
|
|
```
|
|
"123" parse // Returns 123 (as appropriate numeric type)
|
|
```
|
|
**See Also**: to_str
|
|
|
|
#### pick
|
|
**Signature**: `(Size -- Self)`
|
|
**Trait**: Stackable
|
|
**Description**: Copy nth item to top (0 = top, 1 = second, etc.).
|
|
**Example**:
|
|
```
|
|
1 2 3 4 2 pick // Returns 1 2 3 4 2
|
|
```
|
|
**See Also**: roll, depth
|
|
|
|
#### print
|
|
**Signature**: `(Stringifiable --)`
|
|
**Description**: Print value to stdout.
|
|
**Example**:
|
|
```
|
|
"Hello" print // Prints: Hello
|
|
42 print // Prints: 42
|
|
```
|
|
**See Also**: input, to_str
|
|
|
|
#### read
|
|
**Signature**: `(String -- String)`
|
|
**Description**: Read file contents as string.
|
|
**Example**:
|
|
```
|
|
"file.txt" read // Returns file contents
|
|
```
|
|
**See Also**: write
|
|
|
|
#### reduce
|
|
**Signature**: `(ArrayOf<T> T TokenString -- T)`
|
|
**Description**: Fold array with accumulator function.
|
|
**Example**:
|
|
```
|
|
[1 2 3 4] 0 { + } reduce // Returns 10
|
|
```
|
|
**See Also**: map, filter
|
|
|
|
#### reverse
|
|
**Signature**: `(ArrayOf<T> -- ArrayOf<T>)`
|
|
**Description**: Reverse order of array elements.
|
|
**Example**:
|
|
```
|
|
[1 2 3] reverse // Returns [3 2 1]
|
|
```
|
|
**See Also**: transpose
|
|
|
|
#### roll
|
|
**Signature**: `(Size Size --)`
|
|
**Trait**: Stackable
|
|
**Description**: Rotate n items, times times.
|
|
**Example**:
|
|
```
|
|
1 2 3 4 3 1 roll // Rotates top 3 once: 1 3 4 2
|
|
```
|
|
**See Also**: rot, pick
|
|
|
|
#### rot
|
|
**Signature**: `(Self Self Self -- Self Self Self)`
|
|
**Trait**: Stackable
|
|
**Description**: Rotate the top three items.
|
|
**Example**:
|
|
```
|
|
1 2 3 rot // Returns 2 3 1
|
|
```
|
|
**See Also**: swap, roll
|
|
|
|
#### slice
|
|
**Signature**: `(Sliceable Size Size -- Sliceable)`
|
|
**Trait**: Sliceable
|
|
**Description**: Extract elements from start to end index.
|
|
**Example**:
|
|
```
|
|
[10 20 30 40] 1 3 slice // Returns [20 30]
|
|
```
|
|
**See Also**: at, length
|
|
|
|
#### split
|
|
**Signature**: `(String String -- ArrayOf<String>)`
|
|
**Trait**: String
|
|
**Description**: Split string by delimiter.
|
|
**Example**:
|
|
```
|
|
"a,b,c" "," split // Returns ["a" "b" "c"]
|
|
```
|
|
**See Also**: join, substr
|
|
|
|
#### substr
|
|
**Signature**: `(String Size Size -- String)`
|
|
**Trait**: String
|
|
**Description**: Extract substring from start to end index.
|
|
**Example**:
|
|
```
|
|
"hello" 1 3 substr // Returns "el"
|
|
```
|
|
**See Also**: slice, split
|
|
|
|
#### swap
|
|
**Signature**: `(Self Self -- Self Self)`
|
|
**Trait**: Stackable
|
|
**Description**: Swap the top two items on the stack.
|
|
**Example**:
|
|
```
|
|
5 10 swap // Returns 10 5
|
|
```
|
|
**See Also**: dup, rot
|
|
|
|
#### to_f32, to_f64
|
|
**Signature**: `(Convertible -- f32/f64)`
|
|
**Trait**: Convertible
|
|
**Description**: Convert value to 32-bit or 64-bit float.
|
|
**Example**:
|
|
```
|
|
42 to_f64 // Returns 42.0
|
|
```
|
|
**See Also**: to_i32, to_str
|
|
|
|
#### to_i8, to_i16, to_i32, to_i64
|
|
**Signature**: `(Convertible -- i8/i16/i32/i64)`
|
|
**Trait**: Convertible
|
|
**Description**: Convert value to signed integer type.
|
|
**Example**:
|
|
```
|
|
3.14 to_i32 // Returns 3 (truncates)
|
|
```
|
|
**See Also**: to_u32, to_f64
|
|
|
|
#### to_str
|
|
**Signature**: `(Stringifiable -- String)`
|
|
**Trait**: Stringifiable
|
|
**Description**: Convert value to string representation.
|
|
**Example**:
|
|
```
|
|
42 to_str // Returns "42"
|
|
```
|
|
**See Also**: parse, print
|
|
|
|
#### to_u8, to_u16, to_u32, to_u64
|
|
**Signature**: `(Convertible -- u8/u16/u32/u64)`
|
|
**Trait**: Convertible
|
|
**Description**: Convert value to unsigned integer type.
|
|
**Example**:
|
|
```
|
|
42 to_u32 // Returns 42 (as u32)
|
|
```
|
|
**See Also**: to_i32, to_f64
|
|
|
|
#### transpose
|
|
**Signature**: `(ArrayOf<ArrayOf<T>> -- ArrayOf<ArrayOf<T>>)`
|
|
**Description**: Transpose a 2D array (swap rows and columns).
|
|
**Example**:
|
|
```
|
|
[[1 2] [3 4]] transpose // Returns [[1 3] [2 4]]
|
|
```
|
|
**See Also**: reverse
|
|
|
|
#### window
|
|
**Signature**: `(ArrayOf<T> Size -- ArrayOf<ArrayOf<T>>)`
|
|
**Description**: Create sliding windows of given size.
|
|
**Example**:
|
|
```
|
|
[1 2 3 4] 2 window // Returns [[1 2] [2 3] [3 4]]
|
|
```
|
|
**See Also**: slice
|
|
|
|
#### write
|
|
**Signature**: `(String String --)`
|
|
**Description**: Write string to file.
|
|
**Example**:
|
|
```
|
|
"data" "file.txt" write // Writes "data" to file.txt
|
|
```
|
|
**See Also**: read
|
|
|
|
---
|