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 end
17571757 return "int_array"
17581758 end
1759+ if mname == "filter_map"
1760+ if recv >= 0
1761+ blk = @nd_block[nid]
1762+ if blk >= 0
1763+ bbody = @nd_body[blk]
1764+ if bbody >= 0
1765+ bbs = get_stmts(bbody)
1766+ if bbs.length > 0
1767+ bret = infer_type(bbs.last)
1768+ if bret == "string"
1769+ return "str_array"
1770+ end
1771+ if bret == "float"
1772+ return "float_array"
1773+ end
1774+ end
1775+ end
1776+ end
1777+ end
1778+ return "int_array"
1779+ end
17591780 if mname == "find" || mname == "detect"
17601781 if recv >= 0
17611782 rt = infer_type(recv)
@@ -9414,7 +9435,7 @@
94149435 end
94159436 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 type
94199440 if recv_type == "str_array"
94209441 types.push("string")
@@ -12333,6 +12354,9 @@
1233312354 if (mname == "find" || mname == "detect") && @nd_block[nid] >= 0
1233412355 return compile_array_find_block(nid, rc, recv_type)
1233512356 end
12357+ if mname == "filter_map" && @nd_block[nid] >= 0
12358+ return compile_array_filter_map(nid, rc, recv_type)
12359+ end
1233612360 # Array methods
1233712361 if recv_type == "int_array"
1233812362 if mname == "length"
@@ -16958,6 +16982,69 @@
1695816982 "0"
1695916983 end
1696016984
16985+ def compile_array_filter_map(nid, rc, recv_type)
16986+ bp1 = get_block_param(nid, 0)
16987+ if bp1 == ""
16988+ bp1 = "_x"
16989+ end
16990+ elem_type = "int"
16991+ if recv_type == "str_array"
16992+ elem_type = "string"
16993+ elsif recv_type == "float_array"
16994+ elem_type = "float"
16995+ end
16996+ declare_var(bp1, elem_type)
16997+ pfx_src = array_c_prefix(recv_type)
16998+ # Determine result type from block return
16999+ blk = @nd_block[nid]
17000+ block_ret = "int"
17001+ if blk >= 0
17002+ bbody = @nd_body[blk]
17003+ if bbody >= 0
17004+ bbs = get_stmts(bbody)
17005+ if bbs.length > 0
17006+ block_ret = infer_type(bbs.last)
17007+ end
17008+ end
17009+ end
17010+ if block_ret == "string"
17011+ result_type = "str_array"
17012+ elsif block_ret == "float"
17013+ result_type = "float_array"
17014+ else
17015+ result_type = "int_array"
17016+ end
17017+ pfx_dst = array_c_prefix(result_type)
17018+ @needs_gc = 1
17019+ tmp_arr = new_temp
17020+ tmp_i = new_temp
17021+ tmp_val = new_temp
17022+ 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 + 1
17026+ bexpr = "0"
17027+ if blk >= 0
17028+ bbody2 = @nd_body[blk]
17029+ if bbody2 >= 0
17030+ bs = get_stmts(bbody2)
17031+ if bs.length > 0
17032+ k = 0
17033+ while k < bs.length - 1
17034+ compile_stmt(bs[k])
17035+ k = k + 1
17036+ end
17037+ bexpr = compile_expr(bs.last)
17038+ end
17039+ end
17040+ end
17041+ emit(" " + c_type(block_ret) + " " + tmp_val + " = " + bexpr + ";")
17042+ emit(" if (" + tmp_val + ") sp_" + pfx_dst + "_push(" + tmp_arr + ", " + tmp_val + ");")
17043+ @indent = @indent - 1
17044+ emit(" }")
17045+ tmp_arr
17046+ end
17047+
1696117048 def compile_array_find_block(nid, rc, recv_type)
1696217049 bp1 = get_block_param(nid, 0)
1696317050 if bp1 == ""