本文介绍了在Clojure中使用Spectre移除嵌套值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个像这样的Clojure映射:

Suppose that I have a Clojure map like this:

(def mymap {:a [1 2 3] :b {:c [] :d [1 2 3]}})

我想删除一个函数-empties生成一个新映射,其中将删除(:b mymap)中具有空序列作为值的条目。因此(remove-empties mymap)将给出值:

I would like a function remove-empties that produces a new map in which entries from (:b mymap) that have an empty sequence as a value are removed. So (remove-empties mymap) would give the value:

{:a [1 2 3] :b {:d [1 2 3]}}

是否可以使用Spectre编写函数来实现此目的? / p>

Is there a way to write a function to do this using Specter?

推荐答案

(update my-map :b (fn [b]
                    (apply dissoc b
                           (map key (filter (comp empty? val) b)))))

这篇关于在Clojure中使用Spectre移除嵌套值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-30 09:12