Commit c4ca265a
c4ca265a3fa8Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified
Description
Implement Range#bsearch in find-minimum mode
Integer Range#bsearch had no codegen arm, so (1..100).bsearch { |x| x >= 42 } failed to resolve. Add a binary search over the range's
[first, last - excl] interval that returns the smallest member for
which the block is truthy, or nil when none qualifies. The result is
typed as a nullable int so nil renders correctly.
Only the find-minimum form (block returns a boolean) is handled, matching the absence of a find-any Array#bsearch.
Fixes #1235
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- c4ca265a3fa8
- Parent
- a4d975b0680c
spinel_analyze.rbmodifiedView file
@@ -5534,6 +5534,13 @@
55345534
end55355535
return "tuple:int,int"55365536
end5537+
# Integer Range#bsearch returns a member of the range or nil when5538+
# the block never finds one, so it is a nullable int.5539+
if mname == "bsearch" && @nd_block[nid] >= 05540+
if recv >= 0 && infer_type(recv) == "range"5541+
return "int?"5542+
end5543+
end55375544
if mname == "partition"55385545
if recv >= 055395546
rt = infer_type(recv)spinel_codegen.rbmodifiedView file
Loading diff…
test/range_bsearch.rbaddedView file
Loading diff…
test/range_bsearch.rb.expectedaddedView file
Loading diff…