-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
stdlib: add Array.init_matrix #12455
Conversation
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.
I think it's a good addition to the Array module. I'm not 100% happy with the documentation, but have nothing better to suggest at this point.
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.
I agree that the function is reasonable and the implementation is fine.
cc @nojb, who can tell us whether we should indeed add |
I'm in favour of adding this, but I'd prefer an implementation that went all in on either efficiency or elegance, e.g. let init_matrix sx sy f =
init sx @@ fun x ->
init sy @@ fun y ->
f x y (or perhaps one day Also, a documentation nit:
If |
I also found the implementation a bit odd, until I realized that it is adapted from |
ce1689f
to
c1d95e1
Compare
Indeed I would have written the elegant version but I figured that maybe Regarding the doc nitpick (that part was copy-pasted from |
I looked in LexiFi's codebase, there are a handful of occurrences of this function. So I would be in favour of adding them. |
I thus added Regarding the edge case |
I approved this already. If we want to accept this PR, we need a second approver -- because it is stdlib. In the past I have felt uneasy about being invited to second-approve because it is hard to say "no". So now I am hesitating to name names. |
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.
LGTM
This conforms to the documented behavior, and aligns `make_matrix` with the new `init_matrix`.
This aligns with the implementation of `init_matrix`, where this short-circuit is not just an optimization, but a required test.
1607623
to
c62b3db
Compare
The CI has a failure of memory-model/forbidden.ml, which is unrelated. |
This PR adds
Array.init_matrix
to the standard library, with the obvious semantics. It fills a gap because:Array.make
andArray.init
andArray.make_matrix
;Array.make_matrix
, it reduces the risk of someone falling into the beginner’s trap of initializing an array with pointers to a shared mutable value.I included a test.
Remaining questions:
Float.Array
, but apparently there is noFloat.Array.make_matrix
. Is this intentional?f
take place; should it? (I incline to think it should not, but this contrasts with the current docstring ofArray.init
.)@since NEXT_OCAML_RELEASE
.