在 clojure 中 - 如何一一使用列表元素?

发布于 2025-01-03 12:21:59 字数 280 浏览 2 评论 0原文

我正在 Clojure 中使用 StructMap,并尝试通过值列表使用 struct 添加映射。 目前我的结构仅包含 3 个键,因此我使用以下内容来添加新结构:

(struct user-struct (first list-vals) (second list-vals) (nth list-vals 2))

但现在我想使用一个动态结构,用户将选择其中有多少个键。 因此需要使用某种循环来一一返回列表的元素。

有人知道我该怎么做吗?

谢谢。

i'm using a StructMap in Clojure, and trying to add map using struct by a list of values.
currently my struct includes only 3 keys, so i'm using the following to add new struct:

(struct user-struct (first list-vals) (second list-vals) (nth list-vals 2))

but now i want to use a dynamic struct which the user will choose how many keys will be in.
so in need to use some sort of loop which returns the list's element one by one.

does anybody has an idea how should i do it?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

没有伤那来痛 2025-01-10 12:21:59
(apply struct user-struct list-vals)

注意:列表元素计数应与结构字段计数相同

(apply struct user-struct list-vals)

NOTE: List elements count should be same as the struct fields count

总以为 2025-01-10 12:21:59

根据http://clojure.org/data_struct,StructMap 的大多数用途现在可以通过记录更好地服务。

您应该尝试从结构图切换到记录。

According to http://clojure.org/data_structure, most uses of StructMaps would now be better served by records.

You should try to switch from structmap to record.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文