【问题标题】:Expression for "a member in set" with plumatic Schema使用 plumatic Sc​​hema 表达“集合中的成员”
【发布时间】:2017-09-26 17:34:34
【问题描述】:

我想编写一个模式,其中数据结构中的元素 e 可以是一组项目 S 中的任何成员; e ∈ S。在 Clojure Spec 中,这用如下集合表示:

(spec/def ::key-set #{:subprocess :leiningen :nrepl})
(gen/sample (spec/gen ::key-set))
; => (:nrepl :subprocess :subprocess :leiningen :subprocess :nrepl :subprocess :leiningen :nrepl :subprocess)

一组关键字。

然而,在 Schema 中,集合用于表示一组事物,而不是集合中的一个元素成员。那么如何在 Schema 中表达我想要一个集合中的一个成员呢?

【问题讨论】:

    标签: clojure plumatic-schema


    【解决方案1】:

    schema.core/enum 就是你要找的东西。

    user=> (schema.core/validate (schema.core/enum "a" "b" "c") "a")
    "a"
    
    => (schema.core/validate (schema.core/enum "a" "b" "c") "z")
    clojure.lang.ExceptionInfo: Value does not match schema: (not (#{"a" "b" "c"} "z"))
    

    【讨论】:

      猜你喜欢
      • 2017-09-23
      • 2016-07-16
      • 2017-09-29
      • 2016-09-28
      • 2017-09-25
      • 2014-08-14
      • 2016-09-17
      • 2015-04-16
      • 2013-06-08
      相关资源
      最近更新 更多