From b9f0f14e31a8cbe62a2dd1a4e1485461a4384c34 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Wed, 18 Dec 2019 13:02:50 -0500 Subject: [PATCH] Add array/new-filled Similar function signature to buffer/new-filled. --- src/core/array.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/core/array.c b/src/core/array.c index 2e2a6692..2ef9c863 100644 --- a/src/core/array.c +++ b/src/core/array.c @@ -125,6 +125,18 @@ static Janet cfun_array_new(int32_t argc, Janet *argv) { return janet_wrap_array(array); } +static Janet cfun_array_new_filled(int32_t argc, Janet *argv) { + janet_arity(argc, 1, 2); + int32_t count = janet_getinteger(argv, 0); + Janet x = (argc == 2) ? argv[1] : janet_wrap_nil(); + JanetArray *array = janet_array(count); + for (int32_t i = 0; i < count; i++) { + array->data[i] = x; + } + array->count = count; + return janet_wrap_array(array); +} + static Janet cfun_array_pop(int32_t argc, Janet *argv) { janet_fixarity(argc, 1); JanetArray *array = janet_getarray(argv, 0); @@ -243,6 +255,11 @@ static const JanetReg array_cfuns[] = { "Creates a new empty array with a pre-allocated capacity. The same as " "(array) but can be more efficient if the maximum size of an array is known.") }, + { + "array/new-filled", cfun_array_new_filled, + JDOC("(array/new 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/pop", cfun_array_pop, JDOC("(array/pop arr)\n\n"