本文介绍了Clojure 获取嵌套映射值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我习惯于在我的应用程序中使用嵌套数组或设置映射.我试着像这样在 Clojure 中设置一个:
(def gridSettings{:宽度 50:身高50:ground {:variations 25}:water {:variations 25}})
我想知道您是否知道检索嵌套值的好方法?我试着写
(:variations (:ground gridSettings))
这是可行的,但它是背单词而且相当麻烦,特别是如果我添加几个级别.
解决方案get-in
就是这样做的:
(进入 gridSettings [:ground :variations])
来自文档字符串:
<块引用>clojure.core/get-in([m ks] [m ks 未找到])返回嵌套关联结构中的值,其中 ks 是一个键序列.如果键返回 nil不存在,或未找到的值(如果提供).
So I'm used to having a nested array or map of settings in my applications. I tried setting one up in Clojure like this:
(def gridSettings
{:width 50
:height 50
:ground {:variations 25}
:water {:variations 25}
})
And I wondered if you know of a good way of retrieving a nested value? I tried writing
(:variations (:ground gridSettings))
Which works, but it's backwords and rather cumbersome, especially if I add a few levels.
解决方案That's what get-in
does:
(get-in gridSettings [:ground :variations])
From the docstring:
clojure.core/get-in ([m ks] [m ks not-found]) Returns the value in a nested associative structure, where ks is a sequence of keys. Returns nil if the key is not present, or the not-found value if supplied.
这篇关于Clojure 获取嵌套映射值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论