From e28262f5ab8af9c843049e8ac2a31cd28a7f2a5c Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Thu, 19 Dec 2019 12:58:11 -0500 Subject: [PATCH] Add array/fill This function has similar semantics to buffer/fill. --- CHANGELOG.md | 3 ++- src/core/array.c | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 74e37072..53e38cbb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/src/core/array.c b/src/core/array.c index 2ef9c863..7fab80e5 100644 --- a/src/core/array.c +++ b/src/core/array.c @@ -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"