Clojure 获取嵌套映射值

编程入门 行业动态 更新时间:2024-10-19 14:32:15
本文介绍了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:关键词]

本文发布于:2023-03-27 08:47:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/686647.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   Clojure

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!