-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Allow function negation -f to work like !f
#55920
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
base: master
Are you sure you want to change the base?
Conversation
|
I'm not necessarily opposed to this but I'll raise some downsides. (1) Defining functionality for functions that doesn't work on general callables means we can't use it in functions that take generic callable argument. (2) Related, (3) I don't like special case syntax: I think it would be better if instead of adding |
| This method requires at least Julia 1.12. | ||
| """ | ||
| -(f::Function) = (-) ∘ f | ||
| -(f::ComposedFunction{typeof(-)}) = f.inner #allows -(-f) === f |
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 method seems fit for an independent PR. I think it'd be merged easier than the feature addition.
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.
Happy to remove, it's just copied over from the !!f case. Where it allows this slightly odd result:
julia> (!!log)(2)
0.6931471805599453
julia> !!log(2)
ERROR: MethodError: no method matching !(::Float64)although I doubt that ever matters in practice.
|
Do the same for |
|
personally speaking I don't think I'm a fan of this. the ("wat" potential) : (cuteness of syntax gained) ratio is too high |
|
Point (2) above is an interesting deviation from how The only callable array I can think of is from my package, which uses (ab)uses round brackets to mean another kind of indexing. In this case, the two meanings would agree, since indexing the negated array julia> A = AxisKeys.KeyedArray([10, 20, 30], char='a':'c');
julia> A('b') == A[2] == 20 # 2nd element can be accessed by its label 'b'
true
julia> -A('b') == ((-)∘A)('b') == -20
trueThe closest related PR is probably #39042, which makes I can't picture why defining Maybe it would be worth thinking up the worst "wat" which this change would allow? |
|
From the point of view of generic programming, Likewise, we should not have two separate meanings for unary -x == -1 * x
-x == (-) ∘ x # If `x` is a `Function`. |
We have
!f === (!)∘fto negate any boolean function. Was there ever discussion of making-f === (-)∘ftoo? Both are small conveniences to avoid definingx -> op(x). This one would allow for example:This will not change the fact that
-f.(xs)means-(f.(xs)), when usually fused@. -f(xs)would be better. (Unless you write(-f).(xs)by hand.)Needs tests obviously, but RFC?