gpt4 book ai didi

Clojure 规范强制嵌套映射值

转载 作者:行者123 更新时间:2023-12-03 00:06:47 25 4
gpt4 key购买 nike

我像这样强制映射值:

(require '[clojure.spec :as s])

(defn x-integer? [x]
(cond
(integer? x) x
(string? x) (try
(Integer/parseInt x)
(catch Exception e
:clojure.spec/invalid))
:else :clojure.spec/invalid))

(s/def ::port (s/conformer x-integer?))

(s/def ::config (s/keys :req [::port]))

(s/conform ::config {::port "12345"}) ;;=> #:my.ns{:port "12345"}

但是我不知道如何用下面的 map 做同样的事情:

(s/conform ::config {::nested-data {:port "12345"}}) ;;=> something like that maybe ? #:my.ns/nested-data{:port 12345}

应该如何定义::config?另外,最好使用 {::nested-data {::port "12345"}} 代替吗?

最佳答案

(s/def ::port (s/conformer x-integer?))
(s/def ::nested-data (s/keys :req-un [::port]))
(s/def ::config (s/keys :req [::nested-data]))
(s/conform ::config {::nested-data {:port "12345"}})
;;=> #:spec.examples.guide{:nested-data {:port 12345}}

关于Clojure 规范强制嵌套映射值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38426890/

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