Julia is the equivalent of the function R paste ()

Is there a function in Julia that behaves like a R paste() function? In particular, if we give the function two vectors, it will return one vector with the elementary concatenation of two input vectors.

I looked around and can not find the answer for this in the docs or otherwise. An older article by John Miles White suggests that the Julia join() function is the closest analogue, but it seems to work only on pairs of strings, not elementary on row vectors.

Currently, I just use the function below, which iterates over the elements that call join() , but I wonder if there is a better approach.

 x = ["aa", "bb", "cc"] y = ["dd", "ee", "ff"] function mypaste(v1, v2) n = length(v1) res = Array{ASCIIString}(n) for i = 1:n res[i] = join([v1[i], v2[i]]) end return res end mypaste(x, y) 

Running mypaste() gives us the result below if required.

 3-element Array{ASCIIString,1}: "aadd" "bbee" "ccff" 

Is there a good alternative? Do I really not understand the join() function?

+5
source share
3 answers

I don’t think I would use join at all. Join is used to merge strings into a single collection; you after concatenating strings in two different collections. Therefore, when you can easily (and efficiently) create the temporary collections that you need to join with zip , you can avoid this by using a function or multiplying string :

 julia> map(string, x, y) 3-element Array{ASCIIString,1}: "aadd" "bbee" "ccff" julia> map(*, x, y) 3-element Array{ASCIIString,1}: "aadd" "bbee" "ccff" 

Even better (but perhaps too smart in half), there is an operator for multiplying by television broadcasting by element .* :

 julia> x .* y 3-element Array{ASCIIString,1}: "aadd" "bbee" "ccff" 
+8
source

You can use list comprehension and zip to get pairs:

 julia> x = ["aa", "bb", "cc"]; julia> y = ["dd", "ee", "ff"]; julia> [join(i) for i=zip(x,y)] 3-element Array{ByteString,1}: "aadd" "bbee" "ccff" 
+4
source

map . Single line map(join,zip(x,y)) value map(join,zip(x,y)) . As in the following example, which also adds z :

 julia> x = ["aa","bb","cc"]; julia> y = ["dd","ee","ff"]; julia> z = ["gg","hh","ii"]; julia> map(join,zip(x,y,z)) 3-element Array{Any,1}: "aaddgg" "bbeehh" "ccffii" 

(see @DSM answer for a list comprehension)

+4
source

Source: https://habr.com/ru/post/1246099/


All Articles