1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-25 07:50:27 +00:00

Add array/fill

This function has similar semantics to buffer/fill.
This commit is contained in:
Calvin Rose 2019-12-19 12:58:11 -05:00
parent 94246f7574
commit e28262f5ab
2 changed files with 19 additions and 2 deletions

View File

@ -27,7 +27,8 @@ All notable changes to this project will be documented in this file.
- Add `janet_in` to C API.
- Add `truthy?`
- Add `os/environ`
- Add `buffer/fill`
- Add `buffer/fill` and `array/fill`
- Add `array/new-filled`
- Use `(doc)` with no arguments to see available bindings and dynamic bindings.
- `jpm` will use `CC` and `AR` environment variables when compiling programs.
- Add `comptime` macro for compile time evaluation.

View File

@ -137,6 +137,16 @@ static Janet cfun_array_new_filled(int32_t argc, Janet *argv) {
return janet_wrap_array(array);
}
static Janet cfun_array_fill(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 2);
JanetArray *array = janet_getarray(argv, 0);
Janet x = (argc == 2) ? argv[1] : janet_wrap_nil();
for (int32_t i = 0; i < array->count; i++) {
array->data[i] = x;
}
return argv[0];
}
static Janet cfun_array_pop(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
JanetArray *array = janet_getarray(argv, 0);
@ -257,9 +267,15 @@ static const JanetReg array_cfuns[] = {
},
{
"array/new-filled", cfun_array_new_filled,
JDOC("(array/new count &opt value)\n\n"
JDOC("(array/new-filled count &opt value)\n\n"
"Creates a new array of count elements, all set to value, which defaults to nil. Returns the new array.")
},
{
"array/fill", cfun_array_fill,
JDOC("(array/fill arr &opt value)\n\n"
"Replace all elements of an array with value (defaulting to nil) without changing the length of the array. "
"Returns the modified array.")
},
{
"array/pop", cfun_array_pop,
JDOC("(array/pop arr)\n\n"