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

EachRow/EachCol types (see #32310) #663

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

EachRow/EachCol types (see #32310) #663

wants to merge 1 commit into from

Conversation

simonbyrne
Copy link
Contributor

@simonbyrne simonbyrne commented Oct 8, 2019

Obviously still needs JuliaLang/julia#32310 to be merged, but this is just to show we can monkey-patch the aliases to support older versions of Julia.

cc: @nalimilan

@@ -1903,6 +1903,17 @@ if v"0.7.0" <= VERSION < v"1.3.0-alpha.8"
Base.mod(i::Integer, r::AbstractUnitRange{<:Integer}) = mod(i-first(r), length(r)) + first(r)
end

# https://github.com/JuliaLang/julia/pull/32310
if v"0.7.0" <= VERSION
const EachRow{A,I} = Union{Base.Generator{I,(typeof(eachrow(ones(Int32,2,2)).f).name.wrapper){A}}, Base.Generator{I,(typeof(eachrow(ones(Int32,2,2)).f).name.wrapper){A}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const EachRow{A,I} = Union{Base.Generator{I,(typeof(eachrow(ones(Int32,2,2)).f).name.wrapper){A}}, Base.Generator{I,(typeof(eachrow(ones(Int32,2,2)).f).name.wrapper){A}}
const EachRow{A,I} = Union{Base.Generator{I,(typeof(eachrow(ones(Int32,2,2)).f).name.wrapper){A}}, Base.Generator{I,(typeof(eachrow(ones(Int32,2,2)).f).name.wrapper){A}}}

@oschulz
Copy link

oschulz commented May 12, 2022

Could we also define Compat.AbstractSlices as part of this to "backport" Base.AbstractSlices (see JuliaLang/julia#32310 (comment))? This will allow packages like JuliennedArrays.jl, ArraysOfArrays.jl to extend that type without depending on non-LTS Julia versions.

@oschulz
Copy link

oschulz commented May 12, 2022

We could also add

const EachSlice{A,I} = Base.Generator{I,<:(typeof(eachslice(ones(Int32,2,2), dims = 1).f).name.wrapper){A}}
Base.parent(x::EachSlice) = x.f.A

to add support for "legacy" eachslice, in addition to "legacy" eachrow and eachcol. Seems to work fine:

julia> parent(eachslice(rand(3,4,5,6), dims = 3))
3×4×5×6 Array{Float64, 4}:
#...

@martinholters
Copy link
Member

With JuliaLang/julia#32310 finally merged, this could now be picked up again if there is still interest.

@oschulz
Copy link

oschulz commented Jun 10, 2022

I think we should definitely do this. Ideally with AbstractSlices and EachSlice as well?

@nalimilan
Copy link
Member

Adding AbstractSlices in the spirit of this PR isn't possible AFAICT as on Julia < 1.9 eachrow and eachcol return Generator objects, which cannot be made to inherit from AbstractSlices. What can be done is adding unexported eachrow, eachcol and eachslice functions that would be copies of what has been added at JuliaLang/julia#32310, i.e returning EachSlice <: AbstractSlices objects.

Probably the best would be to implement both approaches. The former is useful for packages that want to use a special method when users call f(eachcol(x)) on any Julia version. The latter is useful if they want to dispatch on AbstractSlices, and/or call Compat.eachslice themselves.

@oschulz
Copy link

oschulz commented Jun 12, 2022

Adding AbstractSlices in the spirit of this PR isn't possible AFAICT

Ah - I don't want to add it to have eachrow and eachcol as subtypes - that's not possible, I agree. But I think it's important to add it in Compat so that packages like ArraysOfArrays.jl and JuliennedArrays.jl can make their types subtypes of AbstractSlices while still supporting Julia LTS. And so we can do something like

@static if isdefined(Base, :Slices)
    const SlicedArrayLike = AbstractSlices
else
    const SlicedArrayLike = Union{EachRow,EachCol,EachSlice,AbstractSlices}
end

in ArrayInterfaceCore (prototyping this here). That will then allow code that can take advantage of slices array to dispatch on SlicedArrayLike, which will cover the legacy eachrow and eachcol, as well as the Julia v1.9 Slices and subtypes of AbstractSlices.

For that to work, we need an AbstractSlices that's available from Julia v1.6 on, via Compat.

@mcabbott
Copy link
Contributor

1.9alpha is out. What's the plan here?

@oschulz
Copy link

oschulz commented Nov 12, 2023

Bump - is there support for adding this to Compat?

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.

None yet

5 participants