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

More iteration documentation explanations. #44540

Open
nathanrboyer opened this issue Mar 9, 2022 · 2 comments
Open

More iteration documentation explanations. #44540

nathanrboyer opened this issue Mar 9, 2022 · 2 comments
Labels
doc This change adds or pertains to documentation

Comments

@nathanrboyer
Copy link
Contributor

https://docs.julialang.org/en/v1/manual/arrays/#Iteration
The documentation here needs a section on iterating over slices of an array.

My first instinct coming from Matlab is to write loops like this:

for i in 1:length(A[:,1])
end

If I come across size elsewhere in the documentation, then I might switch to the better:

for i in 1:size(A,1)
end

However, neither of the above should really be used. pairs and axes are usually suggested on Discourse instead, but these function names are not intuitive to search for or use in this context. It would be nice to explain when to use the many different indexing functions available in Julia and their interaction with CartesianIndices.

Another topic that should be addressed in this section is the correct syntax order for accessing arrays in column order. In the examples below, the loop order of nested! and comprehension are natural to me, but the loop order of nested_compact! seems backwards. Since it is not obvious how the loops unwrap, it should be documented.

function nested!(A)
    for k in axes(A,3) 
       for j in axes(A,2) 
           for i in axes(A,1)
              A[i,j,k] = A[i,j,k] - 1.0
           end
       end
    end
 end

function nested_compact!(A)
    for k in axes(A,3), j in axes(A,2), i in axes(A,1)
        A[i,j,k] = A[i,j,k] - 1.0
    end
end

function comprehension(A)
    B = [A[i,j,k] + 1.0 for i in axes(A,1), j in axes(A,2), k in axes(A,3)]
end
@timholy
Copy link
Sponsor Member

timholy commented Mar 10, 2022

Care to submit a pull request? You can just click "Fork" and hit "." to open a browser instance of VSCode.

@nathanrboyer
Copy link
Contributor Author

Hopefully someday. I have DataFrames documentation that I need to finish first, and I still don't totally understand how pairs and CartesianIndices are supposed to be used.

@ViralBShah ViralBShah added the doc This change adds or pertains to documentation label Mar 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc This change adds or pertains to documentation
Projects
None yet
Development

No branches or pull requests

3 participants