连接映射中的所有字段
在 clojure 中,我希望能够为列表中的每个映射连接映射中的所有字段(带有分隔符)。
对于以下结果,我希望能够获得:
(def h '({:key1 "one" :key2 "descone"} {:key1 "two" :key2 "destwo"}))
我想要的结果:
({one,descone} {two,destwo})
我执行了以下操作,但无法获得正确的结果
(def h '({:key1 "one" :key2"descone"} {:key1 "two" :key2"destwo"}))
(~@(apply interpose "," (map (juxt :key1 :key2) h))))
而我得到以下结果:
one,desconetwo,destwo
编辑
场景如下:我们使用jdbc从postgres获取所有记录。返回的记录如下:({:col1 "one" :col2 "descone"} {:col1 "two" :col2 "destwo"})。现在,我们需要用分隔符连接所有列,并将其作为主键,并将其插入到 postgres 中的新表中。
In clojure, I want to be able to concatenate all the fields(with a separator) in a map for each map in a list.
For the following result I want to be able to get:
(def h '({:key1 "one" :key2 "descone"} {:key1 "two" :key2 "destwo"}))
The result I want:
({one,descone} {two,destwo})
I did the following but I could not get the correct result
(def h '({:key1 "one" :key2"descone"} {:key1 "two" :key2"destwo"}))
(~@(apply interpose "," (map (juxt :key1 :key2) h))))
And I am getting the following instead:
one,desconetwo,destwo
Edit
The scenario is as follows: We use jdbc to make get all the records from postgres. The records are returned like this: ({:col1 "one" :col2 "descone"} {:col1 "two" :col2 "destwo"}). Now, we need to concatenate all the columns with a separator and have this as primary key and insert it back into a new table in postgres.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您想返回一个字符串,因为您谈论逗号作为分隔符。我进一步假设,当您说“所有字段”时,您的意思是“每个键值对的所有值”,并且每个值都是一个字符串。在这种情况下,以下内容将为您提供您想要的内容。
代码所做的是首先获取每个映射的值并用逗号分隔符将它们连接起来,然后用左大括号和右大括号将每个映射括起来。然后它获取每个这样的字符串并用空格字符将它们连接起来,然后用左括号和右括号将整个结果括起来。
编辑:将编辑与问题相匹配。
由于您想要生成一个由数据库行的所有值组成的主键值,因此我假设您想要返回的是字符串序列。其中每个字符串都是每个映射的所有值按特定顺序并带有分隔符的串联。假设这是正确的,这是一个修改后的答案。
I'm assuming you want to return a string since you talk about a comma as a separator. I further assume that when you say "all the fields" you mean "all the values of each key-value pair", and that each value is a string. In that case, the following gives you what you wanted.
What the code is doing is first taking the values of each map and concatenating them with a comma separator and then enclosing each map with an open and close brace. Then it is taking each such string and concatenating them with a space character and then enclosing the whole result with an open and close paren.
EDIT: To match the edit to the question.
Since you want to generate a primary key value that consists of all the value of a database row, what I'm assuming you want to return is a sequence of strings. Where each string is a concatenation, with a separator, of all the values of each map in a specific order. Assuming that is correct, here is a modified answer.