I know that you can treat a collection as a singular element by using Ref
, but I always forget how to go in the opposite direction. How do I broadcast over a string as a collection of Char
s? I know I've done this before...
I think collect
might be what you are looking for.
julia> collect("Julia")
5-element Vector{Char}:
'J': ASCII/Unicode U+004A (category Lu: Letter, uppercase)
'u': ASCII/Unicode U+0075 (category Ll: Letter, lowercase)
'l': ASCII/Unicode U+006C (category Ll: Letter, lowercase)
'i': ASCII/Unicode U+0069 (category Ll: Letter, lowercase)
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
If you don't want to collect an intermediate array, you can use a generator:
julia> g = (c for c in "hi")
Base.Generator{String, typeof(identity)}(identity, "hi")
julia> g .+ 1
2-element Vector{Char}:
'i': ASCII/Unicode U+0069 (category Ll: Letter, lowercase)
'j': ASCII/Unicode U+006A (category Ll: Letter, lowercase)
I should probably go PR to base the type/function Stefan suggests here because that seems like an awful workaround for a behavior that is often desirable.
But yes. I am trying to avoid additional allocations without having to explicitly write out the loop.
Last updated: Nov 06 2024 at 04:40 UTC