-
-
Notifications
You must be signed in to change notification settings - Fork 116
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
base: main
Are you sure you want to change the base?
feat(stdlib): Add Array.tryInit
#2209
Conversation
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:
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. |
I see. Let me sit with it for a bit. |
6a5b65f
to
54c6dd7
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After thinking for awhile, I think this is worth it to have.
3a46de2
to
9bc5a58
Compare
9bc5a58
to
68627d0
Compare
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 thetryInit
name but I couldn't think of anything better.Notes:
tryInit
name, if anyone has a better one let me knowList
andArray.Immutable
Array.tryInit
is the most important as with something like a list you can just build it manually.Array.make
andArray.init
so I added those in this pr but I am happy to move those to a separate pr if wanted.