Skip to content

halve silently fails on zip collections #68

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

Open
perrutquist opened this issue Jun 24, 2022 · 2 comments
Open

halve silently fails on zip collections #68

perrutquist opened this issue Jun 24, 2022 · 2 comments

Comments

@perrutquist
Copy link

The current implementation of halve(zip(xs...)) essentially amounts to zip(map(halve, xs)...). This causes a problem when halve splits collections differently. Each half is then a zip of different-length collections, and since zip stops iterating at the end of the shortest collection, some elements are silently left out.

@MasonProtter
Copy link
Member

@perrutquist Can you check which version you're using? This is what I see currently:

julia> using SplittablesBase: halve

julia> halve(zip([1,2], [1,2,3]))
ERROR: ArgumentError: `halve(zip(...))` requires collections with identical `size`
Stacktrace:
 [1] shape
   @ ~/.julia/packages/SplittablesBase/FeYNd/src/implementations.jl:264 [inlined]
 [2] checkshape
   @ ~/.julia/packages/SplittablesBase/FeYNd/src/implementations.jl:258 [inlined]
 [3] halve(xs::Base.Iterators.Zip{Tuple{Vector{Int64}, Vector{Int64}}})
   @ SplittablesBase.Implementations ~/.julia/packages/SplittablesBase/FeYNd/src/implementations.jl:270
 [4] top-level scope
   @ REPL[5]:1

and it seems this was fixed back in 2020 by this PR: https://github.com/JuliaFolds/SplittablesBase.jl/pull/28/files

If you have an example on a recent version of SplittablesBase.jl where this still fails though, please let me know so we can think up a fix.

@perrutquist
Copy link
Author

perrutquist commented Dec 13, 2022

Here's an example of what I mean. Let Vector{Float64} be a stand-in for a user type, for which I define a halve method that cuts it "roughly in half".

(@v1.8) pkg> status SplittablesBase
Status `~/.julia/environments/v1.8/Project.toml`
  [171d559e] SplittablesBase v0.1.15

julia> import SplittablesBase: halve

julia> function halve(xs::Vector{Float64})
           mid = ceil(Int, length(xs)/2)
           left = @view xs[firstindex(xs):firstindex(xs)-1+mid]
           right = @view xs[firstindex(xs)+mid:end]
           return (left, right)
       end
halve (generic function with 20 methods)

julia> a = collect(1:5); b = float.(a);

julia> halve(zip(a,b))
(zip([1, 2], [1.0, 2.0, 3.0]), zip([3, 4, 5], [4.0, 5.0]))

julia> collect.(ans)
([(1, 1.0), (2, 2.0)], [(3, 4.0), (4, 5.0)])

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

No branches or pull requests

2 participants