Skip to content

Collecting an Iterators.PartitionIterator{String} should return a vector of SubStrings and not a Vector{Vector{Char}} #45768

@BambOoxX

Description

@BambOoxX

This subject was initially discussed in https://discourse.julialang.org/t/efficient-way-to-split-string-at-specific-index/83115/17 regarding a way to split a string into equal-length substrings.

The solutions discussed involved (for a string composed of 10 blocks of 8 strings)

  • using a comprehension split8(str::String) = [str[i+1:8] for i in 0:8:length(str)]
  • using a comprehension and SubStrings split8(str::String) = [SubString(str,i+1,i+8) for i in 0:8:length(str)-1]
  • using Iterators.partition and join split8(str::String) = join.(Iterators.partition(str, 8))
  • using Iterators.partition and String split8(str::String) = String.(Iterators.partition(str, 8))

With this configuration

Julia Version 1.7.3
Commit 742b9ab (2022-05-06 12:58 UTC)
Platform Info:
OS: Windows (x86_64-w64-mingw32)
CPU: 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-12.0.1 (ORCJIT, tigerlake)
Environment:
JULIA_EDITOR = code
JULIA_NUM_THREADS = 1

it seems that the first solution is the most efficient. However, Iterators.partition seems to be the correct tool for that. Also as noted by DNF in the original discussion, given its description

partition(collection, n)

Iterate over a collection n elements at a time.

Examples
≡≡≡≡≡≡≡≡≡≡

julia> collect(Iterators.partition([1,2,3,4,5], 2))
3-element Vector{SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}}:
[1, 2]
[3, 4]
[5]

one could assume that collecting the iterator should return a vector of SubString.

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueIndicates a good issue for first-time contributors to JuliaiterationInvolves iteration or the iteration protocolstrings"Strings!"

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions