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

Iterators.partition does not work on substrings #51771

Closed
chrstphrbrns opened this issue Oct 19, 2023 · 1 comment · Fixed by #51773
Closed

Iterators.partition does not work on substrings #51771

chrstphrbrns opened this issue Oct 19, 2023 · 1 comment · Fixed by #51773
Labels
iteration Involves iteration or the iteration protocol

Comments

@chrstphrbrns
Copy link
Contributor

julia> lstrip("01111", '0')
"1111"

julia> collect(Iterators.partition("1111", 2))
2-element Vector{SubString{String}}:
 "11"
 "11"

julia> collect(Iterators.partition(lstrip("01111", '0'), 2))
ERROR: TypeError: in typeassert, expected SubString{SubString{String}}, got a value of type SubString{String}
Stacktrace:
 [1] convert(::Type{SubString{SubString{String}}}, s::SubString{String})
   @ Base ./strings/substring.jl:57
 [2] setindex!(A::Vector{SubString{SubString{String}}}, x::SubString{String}, i1::Int64)
   @ Base ./array.jl:1019
 [3] copyto!(dest::Vector{SubString{SubString{String}}}, src::Base.Iterators.PartitionIterator{SubString{String}})
   @ Base ./abstractarray.jl:941
 [4] _collect(cont::UnitRange{Int64}, itr::Base.Iterators.PartitionIterator{SubString{String}}, ::Base.HasEltype, isz::Base.HasLength)
   @ Base ./array.jl:763
 [5] collect(itr::Base.Iterators.PartitionIterator{SubString{String}})
   @ Base ./array.jl:757
 [6] top-level scope
   @ REPL[76]:1

julia> collect(Iterators.partition(string(lstrip("01111", '0')), 2))
2-element Vector{SubString{String}}:
 "11"
 "11"
@KristofferC
Copy link
Member

KristofferC commented Oct 19, 2023

#51773 should fix this. The issue is that eltype here lied about what element that iterating it actually produce:

julia> itr = Iterators.partition(lstrip("01111", '0'), 2)
Base.Iterators.PartitionIterator{SubString{String}}("1111", 2)

julia> eltype(itr)
SubString{SubString{String}}

julia> foreach(x -> @show(typeof(x)), itr)
typeof(x) = SubString{String}
typeof(x) = SubString{String}

@brenhinkeller brenhinkeller added the iteration Involves iteration or the iteration protocol label Oct 20, 2023
KristofferC added a commit that referenced this issue Oct 23, 2023
Fixes #51771

The convert method that asserts in #51771 is arguably still faulty
though.
KristofferC added a commit that referenced this issue Oct 24, 2023
Fixes #51771

The convert method that asserts in #51771 is arguably still faulty
though.

(cherry picked from commit cf00550)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
iteration Involves iteration or the iteration protocol
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants