Commit 3804f3e7

3804f3e757bdMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified

Description

feat: add Array#zip with block

Iterates two arrays in parallel, yielding pairs to the block. Both block parameters inherit the receiver's element type.

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Details

Commit
3804f3e757bd
Parent
7f17edd888ed

spinel_codegen.rbmodifiedView file

@@ -9463,6 +9463,15 @@
94639463 else
94649464 types.push("int")
94659465 end
9466+ elsif mname == "zip"
9467+ # Both params get element type from receiver
9468+ if recv_type == "str_array"
9469+ types.push("string")
9470+ elsif recv_type == "float_array"
9471+ types.push("float")
9472+ else
9473+ types.push("int")
9474+ end
94669475 elsif mname == "each_with_index"
94679476 if bk == 0
94689477 # Element
@@ -15295,6 +15304,35 @@
1529515304 end
1529615305 end
1529715306
15307+ if mname == "zip"
15308+ if @nd_block[nid] >= 0 && recv >= 0
15309+ old = @in_loop
15310+ @in_loop = 1
15311+ rt = infer_type(recv)
15312+ rc = compile_expr(recv)
15313+ arg = compile_arg0(nid)
15314+ bp1 = get_block_param(nid, 0)
15315+ bp2 = get_block_param(nid, 1)
15316+ if bp1 == ""
15317+ bp1 = "_a"
15318+ end
15319+ if bp2 == ""
15320+ bp2 = "_b"
15321+ end
15322+ pfx = array_c_prefix(rt)
15323+ tmp_i = new_temp
15324+ emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_" + pfx + "_length(" + rc + "); " + tmp_i + "++) {")
15325+ emit(" lv_" + bp1 + " = sp_" + pfx + "_get(" + rc + ", " + tmp_i + ");")
15326+ emit(" lv_" + bp2 + " = sp_" + pfx + "_get(" + arg + ", " + tmp_i + ");")
15327+ @indent = @indent + 1
15328+ compile_stmts_body(@nd_body[@nd_block[nid]])
15329+ @indent = @indent - 1
15330+ emit(" }")
15331+ @in_loop = old
15332+ return 1
15333+ end
15334+ end
15335+
1529815336 if mname == "step"
1529915337 if @nd_block[nid] >= 0 && recv >= 0
1530015338 old = @in_loop