Stream: helpdesk (published)

Topic: Broadcasting over string


view this post on Zulip Adam non-jedi Beckmeyer (May 13 2021 at 14:30):

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 Chars? I know I've done this before...

view this post on Zulip Moorits Muru (May 13 2021 at 15:20):

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)

view this post on Zulip Mason Protter (May 13 2021 at 15:52):

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)

view this post on Zulip Adam non-jedi Beckmeyer (May 13 2021 at 16:51):

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.

view this post on Zulip Adam non-jedi Beckmeyer (May 13 2021 at 16:51):

But yes. I am trying to avoid additional allocations without having to explicitly write out the loop.


Last updated: Dec 28 2024 at 04:38 UTC