-
-
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
RFE: syntactic sugar f(x) .= 3+4x for vectorized function definition #30520
Comments
The standard way to do this kind of things in julia is to define functions for scalars, then vectorize: |
Thinking: "Limit argument types to Array{Any,N} by dot-before-equal-sign. Which syntactic sugar could be assigned to execute f(x::Scalar)? Sole "@" macro!" |
Defining "vectorized" functions (rather than using explicit "dot calls" That is, if you define a vectorized (elementwise) function See also #17302 — we used to have a special macro for defining vectorized methods, which was removed for precisely this reason. |
Thank you for the info. I did not previously have this clear of a picture. If the Julian style is to define scalar functions and promote, then I agree the The only gap is behavior of the
|
This is actually potentially useful behavior if foo(x) = sort!(myallocate(length(x)) .= x .+ 1) # several in-place operations on result of myallocate(n) (Changing this into a new kind of function definition would be breaking, and hence cannot happen in Julia 1.x.) |
With your other example, Given that we all agree that a "vectorized function definition" isn't really needed and that we have a defined meaning here, I think we can close this. |
Yep agree on closing; thank you for answering my questions! |
New to Julia, so please have patience if I am missing something in this suggestion.
Since the
.=
operator is used for in-place assignment, it does not currently make sense to define a function using this operator. That is to say, the statementf(x) .= 3+4x
would not have any meaning. The statement and variations such asf .= x -> 3+4x
indeed trigger strange errors.I think it would be elegant to hijack the
.=
operator when the LHS is a function definition or RHS is a lambda; my hijack proposal is to makef(x) .= 3+4x
and/orf .= x -> 3+4x
intof(x) = @. 3+4x
or something similar. This is less in-line with the behavior of.=
as an "in-place assignment" operator, and more in-line with the dot as a broadcast operator.The text was updated successfully, but these errors were encountered: