Commit d2d4f09c

d2d4f09ccf31Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified

Description

Array#inject/reduce with a symbol operator

Fold a typed array with an operator passed as a symbol, covering the inject(:+), inject(&:+) and inject(init, :+) forms. Int elements use the overflow-aware sp_int_* helpers, floats fold with the raw C operator, and strings concatenate. The block-fold form (inject { |a, e| ... }) is unchanged. analyze reports the element type as the result.

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
d2d4f09ccf31
Parent
cd284e6f8dd2

src/analyze.cmodifiedView file

@@ -539,6 +539,7 @@
539539 if (!strcmp(name, "length") || !strcmp(name, "size") ||
540540 !strcmp(name, "count") || !strcmp(name, "index")) return TY_INT;
541541 if (!strcmp(name, "sum")) return ty_array_elem(rt);
542+ if (!strcmp(name, "inject") || !strcmp(name, "reduce")) return ty_array_elem(rt);
542543 if (!strcmp(name, "tally") && argc == 0) {
543544 if (rt == TY_INT_ARRAY) return TY_INT_INT_HASH;
544545 if (rt == TY_STR_ARRAY) return TY_STR_INT_HASH;