Commit ad8d0452
ad8d0452437eMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
feat: add Array#take(n) and Array#drop(n)
take returns first n elements, drop skips first n elements. Both return new arrays and work for all array types.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- ad8d0452437e
- Parent
- 1d9a49df5d92
spinel_codegen.rbmodifiedView file
@@ -1739,6 +1739,12 @@
17391739
end17401740
return "int"17411741
end1742+
if mname == "take" || mname == "drop"1743+
if recv >= 01744+
return infer_type(recv)1745+
end1746+
return "int_array"1747+
end17421748
if mname == "sort"17431749
if recv >= 017441750
return infer_type(recv)@@ -11948,6 +11954,26 @@
1194811954
1194911955
def compile_array_method_expr(nid, mname, rc, recv_type)1195011956
# Common array methods (all array types)11957+
if mname == "take"11958+
pfx = array_c_prefix(recv_type)11959+
n = compile_arg0(nid)11960+
tmp = new_temp11961+
itmp = new_temp11962+
emit(" " + c_type(recv_type) + tmp + " = sp_" + pfx + "_new();")11963+
emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < " + n + " && " + itmp + " < sp_" + pfx + "_length(" + rc + "); " + itmp + "++)")11964+
emit(" sp_" + pfx + "_push(" + tmp + ", sp_" + pfx + "_get(" + rc + ", " + itmp + "));")11965+
return tmp11966+
end11967+
if mname == "drop"11968+
pfx = array_c_prefix(recv_type)11969+
n = compile_arg0(nid)11970+
tmp = new_temp11971+
itmp = new_temp11972+
emit(" " + c_type(recv_type) + tmp + " = sp_" + pfx + "_new();")11973+
emit(" for (mrb_int " + itmp + " = " + n + "; " + itmp + " < sp_" + pfx + "_length(" + rc + "); " + itmp + "++)")11974+
emit(" sp_" + pfx + "_push(" + tmp + ", sp_" + pfx + "_get(" + rc + ", " + itmp + "));")11975+
return tmp11976+
end1195111977
if mname == "sample"1195211978
pfx = array_c_prefix(recv_type)1195311979
return "sp_" + pfx + "_get(" + rc + ", rand() % sp_" + pfx + "_length(" + rc + "))"