Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(stdlib): Add Array.tryInit #2209

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

spotandjake
Copy link
Member

This pr adds an Array.tryInit function to make this a bit easier. Array.tryInit: (length: Number, fn: (index: Number) => Result<a, b>) => Result<Array<A>, b>. This makes building arrays from result functions quite a bit easier as there was no good alternative before. I do not love the tryInit name but I couldn't think of anything better.

Notes:

  • I do not love the tryInit name, if anyone has a better one let me know
  • If we like the feature and name I am happy to add it to List and Array.Immutable
    • happy todo that in this pr or a separate one
    • Array.tryInit is the most important as with something like a list you can just build it manually.
  • I also noticed we didn't have tests for Array.make and Array.init so I added those in this pr but I am happy to move those to a separate pr if wanted.

index += 1n
},
Err(e) => {
for (let mut j = 0n; j < i; j += 4n) {
Copy link
Member Author

@spotandjake spotandjake Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We incRef when we add the items so we should probably decRef all the items we incRef'd. I would appreciate if someone could verify that I am doing this right.

I also think we might need to manually deallocate (not just decref) the allocated array if i'm not mistaken?

@ospencer
Copy link
Member

This feels a little niche. Can you give some examples of use cases of where this would be useful?

@spotandjake
Copy link
Member Author

This feels a little niche. Can you give some examples of use cases of where this would be useful?

The scenario I needed this for was parsing wasm, I already know how many types will be in the section so it makes sense to initialize the array ahead of time, but I need to parse each type separately like:

let typeCount = Stream.GetAndAdvance.uLeb128(stream)
Array.init((_) => {
  parseWasmType(stream) // This could fail so we want to escape
}, typeCount)

The only other ideal way I could think todo this was either use a list or create an array of options (which has other issues). I know the instruction itself seems niche and my use case was niche but I feel like the operation makes sense especially during streaming operations where you know the count but not if the data is correct.

@ospencer
Copy link
Member

I see. Let me sit with it for a bit.

@spotandjake spotandjake force-pushed the spotandjake-array-tryInit branch from 6a5b65f to 54c6dd7 Compare January 14, 2025 19:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants