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

RFC: Add @view macro #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

lendle
Copy link

@lendle lendle commented Jul 21, 2014

This pull request adds a @view macro, which translates A[a:b, c:d] syntax to view(A, a:b, c:d).

Any symbol("end")s found in square brackets (unless they occur in nested square brackets) are converted to either size(A, d) or, if only one indexing object, length(A). The special case of 1:end is replaced by : instead of 1:size(A, d). For example:

  • @view(A[5:end-2]) => view(A, 5:length(A)-2)
  • @view(A[5:end, 3:b]) => view(A, 5:size(A,1), 3:b)
  • @view(A[1:end, 3:b]) => view(A, :, 3:b)
  • @view(A[c[3:end]]) => view(A, :, c[3:end])

@view only works on named arrays. @view(rand(10,10)[1:5, :]) will throw an error, because I don't think you'd want a view of an array that wasn't a named object very often.

I'm making some assumptions that I'm not 100% sure of, so please let me know if they could cause an issue:

  • symbol("end") can only occur in an expression or subexpression of an expression with head :ref
  • It always makes sense to replace symbol("end") with either a call to size or length
  • If there is a symbol("end") in a :ref expression that is a subexpression of another :ref, the symbol("end") applies to the innermost :ref expression.

I also have a bunch of tests but I imagine they wouldn't be considered good practice for testing macros, so any feedback on that would be appreciated!

@lindahua
Copy link
Contributor

@lendle This is great to have!

I think more tests to ensure correctness in more cases would be good too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants