-
Notifications
You must be signed in to change notification settings - Fork 14
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
Call-site autowrap
function or macro?
#142
Comments
I think there is something sound to this idea. |
Yeah, I feel like making users specify the |
functions.jl doesn't guess 😂 , it knows based on that static set of functions. If we can write this macro you propose and it is nice, i suspect we can write something similar to use internally that might simplify the code in functions.jl. |
One plus side to having this be a call-site function is that we don't need to define explicit wrappers with signatures that match the original function. Seems like a lot of those edge cases are because of inconsistencies with how |
Here's one attempt... function apply(f, args...; kw...)
names = ()
clean = map(args) do a
a isa NamedDimsArray || return a
names = dimnames(a)
parent(a) # edit -- this got lost somehow!
end
names === () && return f(args...; kw...)
dropped = ()
cleankw = map(kw.itr, Tuple(kw.data)) do s,v
s === :dims || return s => v
d = NamedDims.dim(names, v)
dropped = (dropped..., d...)
:dims => d
end
out = f(clean...; cleankw...)
if ndims(out) == length(names)
NamedDimsArray(out, names)
elseif ndims(out) == length(names) - length(dropped)
final = NamedDims.remaining_dimnames_after_dropping(names, dropped)
NamedDimsArray(out, final)
else
out
end
end
using NamedDims
nda = NamedDimsArray(rand(2,3), (:x, :y))
ndb = apply(sum, log, nda, init=100.0, dims=:x)
ndc = apply(dropdims, ndb, dims=:x)
@code_warntype apply(prod, nda.data, dims=2) # Any |
Thanks, I think there are two additions that we'll want to add.
|
Sorry 1. got lost somehow, between variants. I don't really see why this can't be made type-stable, it's all known right? Agree you could supply a type, although by the time you're writing How many functions would this be useful for? I suppose it's for random packages... here's one where "like dropdims" is the wrong guess:
|
The fact that FunctionWrappers.jl exists suggests that it's a harder problem than expected. While we can know all our input arguments we don't know what the result of
Yeah, a simple example is that some operations need to work on a |
We want to define all the wrapper functions though. So using things just works.
Providing something call-site would just be to make temporary work-arounds of missing overloads easier. |
#24 (or something like it) for LinearAlgebra would be nice to have. The lighter-weight this package is, the easier it is to get 3rd-party packages to depend on it, instead of the reverse. Maybe it would be worth doing this to Tracker.jl in order to drop Requires.jl?
I don't really understand FunctionWrappers.jl but it sounds like it's for harder cases. When |
Sorry, I wasn't trying to suggest that this would replace the existing explicit wrappers. I agree that an explicit wrapper should always trump an |
While I don't think this is a good long term solution, I think having a temporary call-site autowrap function or macro for function calls that don't work on
NameDimsArray
s (orKeyedArray
s in AxisKeys.jl) would help improve adoption and reduce friction. General logic would include:f
dims
to specify how the result should be rewrapped.:
meaning the result should be a scalar?dims
don't match the input (e.g., filtering)The text was updated successfully, but these errors were encountered: