连接映射中的所有字段

发布于 2025-01-17 19:29:32 字数 658 浏览 5 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无戏配角 2025-01-24 19:29:32

我假设您想返回一个字符串,因为您谈论逗号作为分隔符。我进一步假设,当您说“所有字段”时,您的意思是“每个键值对的所有值”,并且每个值都是一个字符串。在这种情况下,以下内容将为您提供您想要的内容。

user> (str "(" (clojure.string/join " " (map #(str "{" (clojure.string/join ","  (vals %)) "}") h)) ")")
"({one,descone} {two,destwo})"
user> 

代码所做的是首先获取每个映射的值并用逗号分隔符将它们连接起来,然后用左大括号和右大括号将每个映射括起来。然后它获取每个这样的字符串并用空格字符将它们连接起来,然后用左括号和右括号将整个结果括起来。

编辑:将编辑与问题相匹配。

由于您想要生成一个由数据库行的所有值组成的主键值,因此我假设您想要返回的是字符串序列。其中每个字符串都是每个映射的所有值按特定顺序并带有分隔符的串联。假设这是正确的,这是一个修改后的答案。

user> (map #(str "{" (clojure.string/join ","  ((juxt :key1 :key2) %)) "}") h)
("{one,descone}" "{two,destwo}")
user>

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.

user> (str "(" (clojure.string/join " " (map #(str "{" (clojure.string/join ","  (vals %)) "}") h)) ")")
"({one,descone} {two,destwo})"
user> 

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.

user> (map #(str "{" (clojure.string/join ","  ((juxt :key1 :key2) %)) "}") h)
("{one,descone}" "{two,destwo}")
user>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文