String splatting inside comprehensions

The following excerpt (taken from a julia-1.10.2 REPL) illustrates my question:

julia> src = ("Ab","Cd")
("Ab", "Cd")

julia> [ "Ab"..., "Cd"... ]
4-element Vector{Char}:
 'A': ASCII/Unicode U+0041 (category Lu: Letter, uppercase)
 'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
 'C': ASCII/Unicode U+0043 (category Lu: Letter, uppercase)
 'd': ASCII/Unicode U+0064 (category Ll: Letter, lowercase)

julia> [ i... for i in src ]
ERROR: syntax: "..." expression outside call around REPL[114]:1
 [1] top-level scope
   @ REPL[114]:1

Why can I manually splat Strings inside the Vector comprehension, but not so programatically? The error message indicates the lack of a call, but in the manual example, calls were lacking too.

How can I accomplish what was done manually, in a program?

1 Like

When you write [ "Ab"..., "Cd"... ] that’s syntax sugar for a call to the vect function. You can check this by executing Meta.@lower [ "Ab"..., "Cd"... ]. The two arguments are splatted in this call.

When you write [ 2i for i in src ] the 2i is not the argument of a function, it’s the body of the anonymous function to call for each i, so you can’t splat in that context.

For the case at hand you can do vcat(collect.(src)...), or probably more efficient: collect(Iterators.flatten(src)).


Or collect(join(src))


This one is both compact and efficient:


It only works for strings of equal lengths though…


Here’s another possibility. You have a lot of options.

reduce((spl,e)->vcat(spl,e...),src, init=Char[])
vcat([[e...] for e in src]...)
[[[e...] for e in src]...;]

and also:

reduce(vcat, [i...] for i in src) 


[j for i in src for j in i] 	

Or even collect(prod(src)) since string concatenation is defined via *

1 Like

Great explanation! Thank you!!

1 Like

Thank you for the concise and readable answer!!

Thank you for adding to the possibilities!

This is neat because it uses a comprehension, thus aligning with the original post. It also illustrates a flattening mechanism without using a function!