gpt4 book ai didi

Clojure:删除嵌套数据结构中所有 nil 项的最简单方法是什么?

转载 作者:行者123 更新时间:2023-12-02 04:51:21 24 4
gpt4 key购买 nike

假设我们有这个嵌套向量:

(def coll [nil [["this" nil "this"] nil] nil "this"])

你会如何设计一个remove-nil函数来让所有的nil都消失?

(remove-nil coll)
;> [[["this" "this"]] "this"]

最佳答案

(clojure.walk/postwalk #(if (coll? %) (into (empty %) (remove nil? %)) %) coll)
;=> [[["this" "this"]] "this"]

关于Clojure:删除嵌套数据结构中所有 nil 项的最简单方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18792130/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com