Commit a2698170
a269817036d1Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
feat: add Array#filter_map
Maps each element via block, keeping only truthy results. Equivalent to map + compact. Block return type determines the result array type (int_array, str_array, or float_array).
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- a269817036d1
- Parent
- 61abb29a3d1b
spinel_codegen.rbmodifiedView file
@@ -1756,6 +1756,27 @@
17561756
end17571757
return "int_array"17581758
end1759+
if mname == "filter_map"1760+
if recv >= 01761+
blk = @nd_block[nid]1762+
if blk >= 01763+
bbody = @nd_body[blk]1764+
if bbody >= 01765+
bbs = get_stmts(bbody)1766+
if bbs.length > 01767+
bret = infer_type(bbs.last)1768+
if bret == "string"1769+
return "str_array"1770+
end1771+
if bret == "float"1772+
return "float_array"1773+
end1774+
end1775+
end1776+
end1777+
end1778+
return "int_array"1779+
end17591780
if mname == "find" || mname == "detect"17601781
if recv >= 017611782
rt = infer_type(recv)@@ -9414,7 +9435,7 @@
94149435
end94159436
if mname == "times" || mname == "upto" || mname == "downto"94169437
types.push("int")9417-
elsif mname == "each" || mname == "each_pair" || mname == "map" || mname == "select" || mname == "filter" || mname == "reject" || mname == "find" || mname == "detect" || mname == "any?" || mname == "all?" || mname == "none?" || mname == "count" || mname == "min_by" || mname == "max_by" || mname == "sort_by" || mname == "flat_map"9438+
elsif mname == "each" || mname == "each_pair" || mname == "map" || mname == "select" || mname == "filter" || mname == "reject" || mname == "find" || mname == "detect" || mname == "any?" || mname == "all?" || mname == "none?" || mname == "count" || mname == "min_by" || mname == "max_by" || mname == "sort_by" || mname == "flat_map" || mname == "filter_map"94189439
# Element iteration: infer block param from collection type94199440
if recv_type == "str_array"94209441
types.push("string")@@ -12333,6 +12354,9 @@
1233312354
if (mname == "find" || mname == "detect") && @nd_block[nid] >= 01233412355
return compile_array_find_block(nid, rc, recv_type)1233512356
end12357+
if mname == "filter_map" && @nd_block[nid] >= 012358+
return compile_array_filter_map(nid, rc, recv_type)12359+
end1233612360
# Array methods1233712361
if recv_type == "int_array"1233812362
if mname == "length"@@ -16958,6 +16982,69 @@
1695816982
"0"1695916983
end1696016984
16985+
def compile_array_filter_map(nid, rc, recv_type)16986+
bp1 = get_block_param(nid, 0)16987+
if bp1 == ""16988+
bp1 = "_x"16989+
end16990+
elem_type = "int"16991+
if recv_type == "str_array"16992+
elem_type = "string"16993+
elsif recv_type == "float_array"16994+
elem_type = "float"16995+
end16996+
declare_var(bp1, elem_type)16997+
pfx_src = array_c_prefix(recv_type)16998+
# Determine result type from block return16999+
blk = @nd_block[nid]17000+
block_ret = "int"17001+
if blk >= 017002+
bbody = @nd_body[blk]17003+
if bbody >= 017004+
bbs = get_stmts(bbody)17005+
if bbs.length > 017006+
block_ret = infer_type(bbs.last)17007+
end17008+
end17009+
end17010+
if block_ret == "string"17011+
result_type = "str_array"17012+
elsif block_ret == "float"17013+
result_type = "float_array"17014+
else17015+
result_type = "int_array"17016+
end17017+
pfx_dst = array_c_prefix(result_type)17018+
@needs_gc = 117019+
tmp_arr = new_temp17020+
tmp_i = new_temp17021+
tmp_val = new_temp17022+
emit(" " + c_type(result_type) + " " + tmp_arr + " = sp_" + pfx_dst + "_new();")17023+
emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_" + pfx_src + "_length(" + rc + "); " + tmp_i + "++) {")17024+
emit(" lv_" + bp1 + " = sp_" + pfx_src + "_get(" + rc + ", " + tmp_i + ");")17025+
@indent = @indent + 117026+
bexpr = "0"17027+
if blk >= 017028+
bbody2 = @nd_body[blk]17029+
if bbody2 >= 017030+
bs = get_stmts(bbody2)17031+
if bs.length > 017032+
k = 017033+
while k < bs.length - 117034+
compile_stmt(bs[k])17035+
k = k + 117036+
end17037+
bexpr = compile_expr(bs.last)17038+
end17039+
end17040+
end17041+
emit(" " + c_type(block_ret) + " " + tmp_val + " = " + bexpr + ";")17042+
emit(" if (" + tmp_val + ") sp_" + pfx_dst + "_push(" + tmp_arr + ", " + tmp_val + ");")17043+
@indent = @indent - 117044+
emit(" }")17045+
tmp_arr17046+
end17047+
1696117048
def compile_array_find_block(nid, rc, recv_type)1696217049
bp1 = get_block_param(nid, 0)1696317050
if bp1 == ""