Commit 9f59e4bd
9f59e4bd4906Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified
Description
Array#max_by / min_by
Type the block param (element) and result (an element) for max_by/min_by/find/ detect/sort_by/take_while/drop_while, and emit max_by/min_by as a loop tracking the element with the largest/smallest (int or float) block key.
make test: 188 pass / 6 fail / 650 error (was 187 / 6 / 651); str_array_max_min_by green, no regressions.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 9f59e4bd4906
- Parent
- 24a6b1d112e3
src/analyze.cmodifiedView file
@@ -435,6 +435,9 @@
435435
!strcmp(name, "filter") || !strcmp(name, "sort_by") ||436436
!strcmp(name, "take_while") || !strcmp(name, "drop_while"))437437
return rt;438+
if (!strcmp(name, "max_by") || !strcmp(name, "min_by") ||439+
!strcmp(name, "find") || !strcmp(name, "detect"))440+
return ty_array_elem(rt); /* returns an element */438441
}439442
if (!strcmp(name, "[]")) {440443
/* arr[range] / arr[start, len] -> a subarray; arr[i] -> an element */@@ -1723,9 +1726,12 @@
17231726
!strcmp(name, "find") || !strcmp(name, "detect") || !strcmp(name, "each_with_index") ||17241727
!strcmp(name, "sort_by") || !strcmp(name, "find_all") || !strcmp(name, "count")) && rt == TY_RANGE)17251728
pt = TY_INT;1726-
else if ((!strcmp(name, "each") || !strcmp(name, "map") ||1727-
!strcmp(name, "select") || !strcmp(name, "reject") ||1728-
!strcmp(name, "find") || !strcmp(name, "each_with_index")) &&1729+
else if ((!strcmp(name, "each") || !strcmp(name, "map") || !strcmp(name, "collect") ||1730+
!strcmp(name, "select") || !strcmp(name, "reject") || !strcmp(name, "filter") ||1731+
!strcmp(name, "find") || !strcmp(name, "detect") ||1732+
!strcmp(name, "max_by") || !strcmp(name, "min_by") || !strcmp(name, "sort_by") ||1733+
!strcmp(name, "take_while") || !strcmp(name, "drop_while") ||1734+
!strcmp(name, "each_with_index")) &&17291735
ty_is_array(rt))17301736
pt = ty_array_elem(rt);17311737
src/codegen.cmodifiedView file
Loading diff…