-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Feature request: @fill macro for expressions that need to be evaluated for every entry #38845
Comments
#16769 is very related, it motivates me to revive it as I also need that once in a while. I will update that PR with a function |
Yes, this functionality would be great. But why call it P.S.: Why does it need to be a macro at all? |
@thofma By the time One problem with |
I think you were trying to explain this above @mcabbott , but what would a macro version be able to do that a higher-order function version, taking a thunk (e.g. |
As @thchr suggested(If I understand your words correctly), an array version julia> function mapfill(f, inds...)
map(CartesianIndices(inds)) do idx
f(idx.I...)
end
end
mapfill (generic function with 1 methods)
julia> mapfill((i, j)->Int[], 2, 3)
2×3 Array{Array{Int64,1},2}:
[] [] []
[] [] [] |
That wasn't so clear, sorry. I agree that a version which takes a function would be pretty natural, and would serve the same role as the macro proposal. But it seems awkward to make this a method of Another possibility is to make a method of |
The point of my proposal is mostly that it's quite convenient to have a short syntax for this. Of course you can do the same with higher order functions, or list comprehensions as I wrote in my post above. I personally think |
I think the
fill
function could use an accompanying@fill
macro, because I relatively often want to fill an array of a specific size with the result of an expression evaluated for every entry.This came up recently in a Discourse post, where someone was suprised
fill(Int[], 5)
gives a vector with five times the same empty Int Vector.One use-case I often encounter is in MakieLayout, where I'd like to generate a matrix of LAxis objects. They are obviously not all supposed to be the same. So a common thing to write is:
I propose the syntax:
Here is a preliminary implementation:
The text was updated successfully, but these errors were encountered: