Add pad and truncate array functions.
This commit is contained in:
parent
80a9abada6
commit
8128c0cf24
23
README.md
23
README.md
@ -444,8 +444,15 @@ fn decide(yes_no: bool) -> Dynamic {
|
||||
## Arrays
|
||||
|
||||
You can create arrays of values, and then access them with numeric indices.
|
||||
The functions `push`, `pop` and `shift` can be used to insert and remove elements
|
||||
to/from arrays.
|
||||
|
||||
The following standard functions operate on arrays:
|
||||
|
||||
* `push` - inserts an element at the end
|
||||
* `pop` - removes the last element and returns it (() if empty)
|
||||
* `shift` - removes the first element and returns it (() if empty)
|
||||
* `len` - returns the number of elements
|
||||
* `pad` - pads the array with an element until a specified length
|
||||
* `truncate` - cuts off the array at exactly a specified length (discarding all subsequent elements)
|
||||
|
||||
```rust
|
||||
let y = [1, 2, 3]; // 3 elements
|
||||
@ -463,9 +470,19 @@ first == 1;
|
||||
|
||||
let last = y.pop(); // remove the last element, 3 elements remaining
|
||||
last == 5;
|
||||
|
||||
print(y.len()); // prints 3
|
||||
|
||||
y.pad(10, "hello"); // pad the array up to 10 elements
|
||||
|
||||
print(y.len()); // prints 10
|
||||
|
||||
y.truncate(5); // truncate the array to 5 elements
|
||||
|
||||
print(y.len()); // prints 5
|
||||
```
|
||||
|
||||
`push` is only defined for standard built-in types. If you want to use `push` with
|
||||
`push` and `pad` are only defined for standard built-in types. If you want to use them with
|
||||
your own custom type, you need to define a specific override:
|
||||
|
||||
```rust
|
||||
|
@ -951,6 +951,14 @@ impl Engine {
|
||||
)
|
||||
}
|
||||
|
||||
macro_rules! reg_func3 {
|
||||
($engine:expr, $x:expr, $op:expr, $v:ty, $w:ty, $r:ty, $( $y:ty ),*) => (
|
||||
$(
|
||||
$engine.register_fn($x, $op as fn(x: $v, y: $w, z: $y)->$r);
|
||||
)*
|
||||
)
|
||||
}
|
||||
|
||||
fn add<T: Add>(x: T, y: T) -> <T as Add>::Output {
|
||||
x + y
|
||||
}
|
||||
@ -1095,13 +1103,24 @@ impl Engine {
|
||||
reg_func1!(engine, "debug", print_debug, String, Array, ());
|
||||
|
||||
// Register array functions
|
||||
fn push<T: Any + 'static>(list: &mut Array, item: T) {
|
||||
fn push<T: Any>(list: &mut Array, item: T) {
|
||||
list.push(Box::new(item));
|
||||
}
|
||||
fn pad<T: Any + Clone>(list: &mut Array, len: i64, item: T) {
|
||||
if len >= 0 {
|
||||
while list.len() < len as usize {
|
||||
push(list, item.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
reg_func2x!(engine, "push", push, &mut Array, (), i32, i64, u32, u64);
|
||||
reg_func2x!(engine, "push", push, &mut Array, (), f32, f64, bool);
|
||||
reg_func2x!(engine, "push", push, &mut Array, (), String, Array, ());
|
||||
reg_func3!(engine, "pad", pad, &mut Array, i64, (), i32, i64);
|
||||
reg_func3!(engine, "pad", pad, &mut Array, i64, (), u32, u64);
|
||||
reg_func3!(engine, "pad", pad, &mut Array, i64, (), f32, f64, bool);
|
||||
reg_func3!(engine, "pad", pad, &mut Array, i64, (), String, Array, ());
|
||||
|
||||
engine.register_dynamic_fn("pop", |list: &mut Array| list.pop().unwrap_or(Box::new(())));
|
||||
engine.register_dynamic_fn("shift", |list: &mut Array| {
|
||||
@ -1114,6 +1133,11 @@ impl Engine {
|
||||
engine.register_fn("len", |list: &mut Array| -> i64 {
|
||||
list.len().try_into().unwrap()
|
||||
});
|
||||
engine.register_fn("truncate", |list: &mut Array, len: i64| {
|
||||
if len >= 0 {
|
||||
list.truncate(len as usize);
|
||||
}
|
||||
});
|
||||
|
||||
// Register string concatenate functions
|
||||
fn prepend<T: Display>(x: T, y: String) -> String {
|
||||
|
Loading…
Reference in New Issue
Block a user