将 clojure 中的两个列表压缩为连接字符串列表 [英] Zip two lists in clojure into list of concatenated strings

查看:22
本文介绍了将 clojure 中的两个列表压缩为连接字符串列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

而不是压缩映射两个列表来获取:

Instead of zip-mapping two lists to get:

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

我想将第一个列表的第一个元素与第二个列表的第一个元素连接起来,依此类推:

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

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

或以相反的顺序.

推荐答案

您可以使用 map 做到这一点.map 可以接受多个集合,它从每个集合中取出下一个元素并将它们传递给作为第一个参数传递的函数(当其中一个集合用完时停止).所以你可以传入一个带有 n 个参数和 n 个集合的函数.

You can do that with map. map can take multiple collections, it takes the next element from each collection and passes them into the function passed as the first argument (stopping when one of the collections runs out). So you can pass in a function that takes n arguments, and n collections.

表达式

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

将首先用a"和c"调用str,然后用b"和d",然后用c"和e".结果将是

will call str first with "a" and "c", then with "b" and "d", then with "c" and "e". The result will be

("ac" "bd" "ce")

由于 str 可以接受可变数量的参数,因此它可以用于任意数量的集合.传入四个集合,比如

Since str can takes a variable number of arguments it can be used with any number of collections. Passing in four collections, like

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

将评估为

("adgj" "behk" "cfil")

这篇关于将 clojure 中的两个列表压缩为连接字符串列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆