feat(compiler): add Enum.find_index/2 support for integer ranges#112
Merged
feat(compiler): add Enum.find_index/2 support for integer ranges#112
Conversation
Compile Enum.find_index/2 on integer ranges (plain and stepped) to
efficient tail-recursive WASM loops with early termination.
Enum.find_index(start..stop, fn i -> pred(i) end)
Enum.find_index(start..stop//step, fn i -> pred(i) end)
Returns the 0-based index of the first element where the predicate
is truthy, or -1 if no element matches (integer sentinel for nil).
Supports:
- Plain ranges (step=1)
- Positive stepped ranges
- Negative stepped ranges
- Dynamic step values (runtime sign check)
The generated helper function shape:
__find_index_N__(i, stop, idx) =
if i > stop do -1
else if pred(i) do idx
else __find_index_N__(i + step, stop, idx + 1)
This gets TCO-optimized into an efficient WASM loop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add
Enum.find_index/2compilation support for integer ranges (plain and stepped) to the Firebird Elixir-to-WASM compiler.What it does
Compiles
Enum.find_index/2on integer ranges into efficient tail-recursive WASM loops with early termination:Returns the 0-based index of the first element where the predicate is truthy, or
-1if no element matches (integer sentinel for nil in WASM).Implementation
Generates a tail-recursive helper function:
This gets TCO-optimized into an efficient WASM loop. Follows the same pattern as the existing
Enum.find/2,Enum.any?/2, andEnum.all?/2implementations.Supports
Tests
10 new end-to-end tests covering: