Lock two lists in clojure to a list of concatenated strings

Instead of zip displaying two lists:

(zipmap ["a","b","c"] ["c","d","e"]) = {"c" "e", "b" "d", "a" "c"} 

I want to combine the first element of the first list with the first element of the second list and so on to get:

 ("ce","bd","ac") 

or in reverse order.

+6
source share
1 answer

You can do this with map . map can accept several collections, it takes the next element from each collection and passes them to the function passed as the first argument (stop when one of the sets ends). So you can pass a function that takes n arguments and n collections.

Expression

 (map str ["a" "b" "c"] ["c" "d" "e"]) 

first, str will be str with "a" and "c", then with "b" and "d", then with "c" and "e". Result will be

 ("ac" "bd" "ce") 

Since str can take a variable number of arguments, it can be used with any number of collections. Transmission in four collections, such as

 (map str ["a" "b" "c"] ["d" "e" "f"] ["g" "h" "i"] ["j" "k" "l"]) 

will be rated as

 ("adgj" "behk" "cfil") 
+12
source

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


All Articles