clojure - 使用 Spectre 从深度嵌套的数据结构中修剪不必要的条目
问题描述
我正在寻找使用 Clojure Spectre 来简化深度嵌套的数据结构。我想删除:
- 任何具有 nil 值的条目
- 任何具有空字符串值的条目
- 任何具有空地图值的条目
- 任何具有空顺序值的条目
- 删除上述情况后,任何具有空映射/顺序值的条目。
像这样的东西:
(do-something
{:a {:aa 1}
:b {:ba -1
:bb 2
:bc nil
:bd ""
:be []
:bf {}
:bg {:ga nil}
:bh [nil]
:bi [{}]
:bj [{:ja nil}]}
:c nil
:d ""
:e []
:f {}
:g {:ga nil}
:h [nil]
:i [{}]
:j [{:ja nil}]})
=>
{:a {:aa 1}
:b {:ba -1
:bb 2}}
我在香草 Clojure 中有一些东西:
(defn prunable?
[v]
(if (sequential? v)
(keep identity v)
(or (nil? v) (#{"" [] {}} v))))
(defn- remove-nil-values
[ticket]
(clojure.walk/postwalk
(fn [el]
(if (map? el)
(let [m (into {} (remove (comp prunable? second) el))]
(when (seq m)
m))
el))
ticket))
我想我需要某种东西,recursive-path
但我没有快速到达任何地方。非常感谢帮助。
解决方案
将不同版本的性能与 Spectre 实现进行比较:
@bm1729 普通版:
Evaluation count : 1060560 in 60 samples of 17676 calls.
Execution time mean : 57.083226 µs
Execution time std-deviation : 543.184398 ns
Execution time lower quantile : 56.559237 µs ( 2.5%)
Execution time upper quantile : 58.519433 µs (97.5%)
Overhead used : 7.023993 ns
Found 5 outliers in 60 samples (8.3333 %)
low-severe 3 (5.0000 %)
low-mild 2 (3.3333 %)
Variance from outliers : 1.6389 % Variance is slightly inflated by outliers
以下版本:
Evaluation count : 3621960 in 60 samples of 60366 calls.
Execution time mean : 16.606135 µs
Execution time std-deviation : 141.114975 ns
Execution time lower quantile : 16.481250 µs ( 2.5%)
Execution time upper quantile : 16.922734 µs (97.5%)
Overhead used : 7.023993 ns
Found 9 outliers in 60 samples (15.0000 %)
low-severe 6 (10.0000 %)
low-mild 3 (5.0000 %)
Variance from outliers : 1.6389 % Variance is slightly inflated by outliers
(defn prune [x]
(cond
(map? x) (not-empty
(reduce-kv
(fn [s k v]
(let [v' (prune v)]
(cond-> s
v' (assoc k v'))))
(empty x)
x))
(seqable? x) (not-empty
(into
(empty x)
(->> x (map prune) (filter identity))))
:else x))
测试用例:
(prune {:a {:aa 1}
:b {:ba -1
:bb 2
:bc nil
:bd ""
:be []
:bf {}
:bg {:ga nil}
:bh [nil]
:bi [{}]
:bj [{:ja nil}]}
:c nil
:d ""
:e []
:f {}
:g {:ga nil}
:h [nil]
:i [{}]
:j [{:ja nil}]})
;; => {:b {:bb 2, :ba -1}, :a {:aa 1}}
更新 - @bm1729 幽灵版本
Evaluation count : 3314820 in 60 samples of 55247 calls.
Execution time mean : 18.421613 µs
Execution time std-deviation : 591.106243 ns
Execution time lower quantile : 18.148204 µs ( 2.5%)
Execution time upper quantile : 20.674292 µs (97.5%)
Overhead used : 7.065044 ns
Found 8 outliers in 60 samples (13.3333 %)
low-severe 2 (3.3333 %)
low-mild 6 (10.0000 %)
Variance from outliers : 18.9883 % Variance is moderately inflated by outliers
推荐阅读
- sql - 如何查找跨 JSON 数组存在重复值的行
- c++ - execvp 做 ps 每次都会给出相同的结果
- lua - 在 Windows 上正确设置 LuaBinaries 文件?
- python - Python 硒网络驱动程序
- r - 尝试使用 R 整理 Excel 工作表
- kotlin - 在 Kotlin(不是 Android)中禁止特定方法的 Linting 规则
- python - 用 ord() 和 chr() 解密凯撒密码?
- c - C 标准的 FE_TONEAREST 舍入模式是否保证中间关系舍入为偶数?
- java - 我可以用一个全新的项目覆盖 Git 存储库吗?
- swift - 文本字段占位符显示不佳,SwitUI