Commit 1d9a49df
1d9a49df5d92Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
feat: add Array#sample for all array types
Returns a random element using rand() % length. Works for int_array, str_array, and float_array.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 1d9a49df5d92
- Parent
- 71523c1d272c
spinel_codegen.rbmodifiedView file
@@ -1694,6 +1694,18 @@
16941694
if mname == "keys"16951695
return "str_array"16961696
end1697+
if mname == "sample"1698+
if recv >= 01699+
rt = infer_type(recv)1700+
if rt == "str_array"1701+
return "string"1702+
end1703+
if rt == "float_array"1704+
return "float"1705+
end1706+
end1707+
return "int"1708+
end16971709
if mname == "values"16981710
if recv >= 016991711
rt = infer_type(recv)@@ -11936,6 +11948,10 @@
1193611948
1193711949
def compile_array_method_expr(nid, mname, rc, recv_type)1193811950
# Common array methods (all array types)11951+
if mname == "sample"11952+
pfx = array_c_prefix(recv_type)11953+
return "sp_" + pfx + "_get(" + rc + ", rand() % sp_" + pfx + "_length(" + rc + "))"11954+
end1193911955
if mname == "any?" && @nd_block[nid] < 01194011956
pfx = array_c_prefix(recv_type)1194111957
return "(sp_" + pfx + "_length(" + rc + ") > 0)"