-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Implement zero!
for arrays
#19912
Implement zero!
for arrays
#19912
Conversation
""" | ||
zero!(x::AbstractArray) | ||
|
||
Sets all elements of array `x` to 0. Equivalent to `fill!(x, 0)`. |
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.
Typically imperative grammar is used when describing how functions act. So in this case it would be "Set all elements..." rather than "Sets all elements..." Should also probably be "the array" rather than just "array".
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.
This reads pretty clearly to me, tbh.
I'm not sure we need a separate function for this; it's pretty easy to type |
@stevengj That's a fair point, though for complete generality wouldn't it need to be something like |
In most practical applications for this I suspect you will know that you have numbers compatible with 0 and can do |
Or you will have some other scalar variable y and can do zero(y) It would be useful to survey the packages to see where filling with zero actually shows up |
I'm not saying its difficult to use |
The need for a lot of in-place operations is greatly reduced by the |
You mean |
Well, either one. Here I implemented |
julia> A = rand(2,2)
2×2 Array{Float64,2}:
ze r0.56361 0.904141
0.723398 0.87581
julia> zeros!(A)
2×2 Array{Float64,2}:
0.0 0.0
0.0 0.0 (It should be noted that both
Thanks for the input! |
I don't think we need to add this function. I agree with @johnmyleswhite that we should rather go in the direction of minimizing this kind of redundancy. |
I guess that settles this then. Thanks for the input! |
Implements
zero!
to set all elements of an array to 0.I was really surprised this wasn't a thing already, and since it is a very common thing to do I think it might be nice to have a special version of
fill!
for this purpose. I think it makes the code very readable and it is obvious what the function does.(I also really like in-place
!
functions 😄 )Comments?