- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
clojure.spec.alpha
API 有一个名为 conformer
的宏有这样的描述:
Usage: (conformer f)
(conformer f unf)
takes a predicate function with the semantics of conform i.e. it should
return either a (possibly converted) value or :clojure.spec.alpha/invalid,
and returns a spec that uses it as a predicate/conformer. Optionally takes
a second fn that does unform of result of first
即使不是深奥,我也不清楚。
它有什么用? “unformer”(不应该是“unconformer”)是干什么用的?我想从返回的“一致值”中重新创建原始数据?
更新
经过15分钟的实验,似乎是要从一个“predicate”创建一个新的“spec”(这个“spec”有什么特别之处??)
我试过了
(require '[clojure.spec.alpha :as s :refer [valid? explain conform conformer]])
; ---
; Using purely clojure.spec.alpha:
; ---
(s/def ::vtx-x float?)
(s/def ::vtx-y float?)
(s/def ::vertex (s/keys :req [::vtx-x ::vtx-y]))
(type (s/get-spec ::vertex))
;=> clojure.spec.alpha$map_spec_impl$reify__1997
(conform ::vertex { ::vtx-x 1.0 ::vtx-y 2.0 })
;=> #:user{:vtx-x 1.0, :vtx-y 2.0}
(valid? ::vertex { ::vtx-x 1.0 ::vtx-y 2.0 })
;=> true
(conform ::vertex { ::vtx-x 1.0 })
;=> :clojure.spec.alpha/invalid
; ---
; Using my own special sauce predicate function, where the conformed
; value carries additional information ... maybe for a debugging system?
; ---
(defn my-vertex-conformer [v]
(when-let [ { x ::vtx-x y ::vtx-y } v ]
(if (and (float? x) (float? y))
[:comment "Vertex conforms!" :something (+ x y) :orig v]
; else
:clojure.spec.alpha/invalid)))
(defn my-vertex-unformer [conf-v] (get conf-v :orig))
(s/def ::my-vertex (conformer my-vertex-conformer my-vertex-unformer))
(type (s/get-spec ::my-vertex))
;=> clojure.spec.alpha$spec_impl$reify__2059
(conform ::my-vertex { ::vtx-x 1.0 ::vtx-y 2.0 })
;=> [:comment "Vertex conforms!" :something 3.0
;=> :orig #:user{:vtx-x 1.0, :vtx-y 2.0}]
(valid? ::my-vertex { ::vtx-x 1.0 ::vtx-y 2.0 })
;=> true
(conform ::my-vertex { ::vtx-x 1.0 })
;=> :clojure.spec.alpha/invalid
奖金,令人惊讶的是这里也不异常(exception),这是疏忽吗?
(conformer map?)
;=> #object[clojure.spec.alpha$spec_impl$reify__2059 0x770b843 "clojure.spec.alpha$spec_impl$reify__2059@770b843"]
(type (conformer map?))
;=> clojure.spec.alpha$spec_impl$reify__2059
最佳答案
What is it used for?
用于创建规范(当用于符合值时)可以返回与给定值不同的值。
(s/conform str 1)
=> 1 ;; (str 1) returned truthy value; input value unchanged
(s/conform (s/conformer str) 1)
=> "1" ;; returns (str 1)
What is the "unformer" (shouldn't that be an "unconformer") used for? I suppose to recreate the original data from the returned "conformed value"?
确切地说,unformer
函数可用于反转使用s/unform
的conformer 所做的任何更改。
(s/def ::str (s/conformer str #(Integer/parseInt %)))
(s/conform ::str 1)
=> "1"
(s/unform ::str "1")
=> 1
有机会简化您的示例规范:
(defn my-vertex-conformer [v]
(let [{x ::vtx-x y ::vtx-y} v] ;; don't need validation here
{:comment "Vertex conforms!" :something (+ x y) :orig v}))
(s/def ::my-vertex
(s/and ::vertex ;; because the validation is done by (s/and ::vertex ...)
(s/conformer my-vertex-conformer
:orig))) ;; keyword can be used as unform function
(->> {::vtx-x 1.0 ::vtx-y 2.0}
(s/conform ::my-vertex)
(s/unform ::my-vertex))
=> {::vtx-x 1.0 ::vtx-y 2.0}
Bonus, amazingly no exception here, is this an oversight?
(conformer map?)
不,这里没有任何问题,尽管将 map?
之类的 bool 谓词函数与 conformer
一起使用可能很不寻常:
(s/conform (s/conformer map?) {})
=> true
(s/conform (s/conformer map?) [])
=> false
(s/conformer map?)
是一个规范,它可以接受任何值并且如果它是一个映射则符合 true,否则为 false。
where does [spec] come from initially?
契约(Contract)的概念已经以各种形式出现了一段时间,例如https://docs.racket-lang.org/guide/contracts.html .另见 https://en.wikipedia.org/wiki/Design_by_contract .
关于Clojure 规范 "conformer"函数,它是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57871705/
我需要创建一个类型安全的弱引用数组 一个包含“类型安全”弱引用并且可以是数组元素的结构: public struct WeakRef: Hashable { public weak var r
假设我有这个 Swift 类: class Foo: NSObject, NSCoding { var stringMember: String var intMember: Int
谁能解释一下,下面的例子有什么问题?为什么会抛出 StackOverflowError 异常? (s/def ::tag keyword?) (s/def ::s string?) (s/def ::
啊啊!尝试使用我的神经网络进行计算时,我不断收到以下错误: > net.compute matrix.train1 (Intercept) survived pclass sexmale
我在下面的行中面临警告 此警告在更改以下行后出现 @interface BDSAppDelegate : UIResponder 到 @interface BDSAppDelegate : UIRe
我正在尝试实现 git 项目 XLPagerTabStrip . 根据项目,每个 Controller 必须: Every view controller provided by PagerTabSt
clojure.spec.alpha API 有一个名为 conformer 的宏有这样的描述: Usage: (conformer f) (conformer f unf) takes
iPhone开发中的“符合”是什么意思?这个词被广泛使用,但我找不到令人满意的定义。 假设我们有 3 个类别 - A、B 和 C 如果A继承B&如果A符合C 这是什么意思? 另外,为什么大多数类、协议
我最近刚刚遇到“弱一致性”这个术语(在 Stack Overflow 用户 retronym 对 How to set up implicit conversion to allow arithmet
我在我的一个 MVC 项目中使用 ConfORM Nhibernate。并且有一对多映射的问题。 IEnumerable domainEntities = this.GetDomain
如何对两个数组进行逐元素算术运算在第一个维度上是一致的,但还有一个额外的维度? 示例,将数组 a (3 x 3 x 2) 乘以数组 b (3 x 3): a a * b 要使其工作,您必须将数组 b
我是 swift 新手,并按照教程了解更多信息。 在下面的代码中,我定义了自定义运算符( Bool { for cardValue in valueArray { if lhs
我有来自 here 的 FAA 剖面图.每个包都包含相关图表的 .tif、描述图表的 .htm 文件和 .tfw 世界文件。 map 投影为朗伯共形圆锥投影。 我正在开发一个 C# 应用程序,它需要能
当我在脚本中调用下面的函数时,我会看到一个弹出对话框来执行一些说明。 这是我的示例代码: function updateStatus(instrxnID){ exporter.
我试图通过注入(inject)适应 URLSession 和 URLSessionDataTask 协议(protocol)的对象来测试我自己的类。我正在扩展 NSURLSession 和 NSURL
这个问题在这里已经有了答案: Fatal error: Dictionary does not conform to Decodable because Any does not conform t
我想在我的一个代理类中管理所有位置代码。由于该类(class)是从头开始构建的,而不是构建在 UIView 上的或继承 NSObjectProtocol 的类似类,它会抛出错误“不符合协议(proto
无法理解为什么我的类不符合 Codable请注意,在我的例子中,我不需要实现方法encode 和decode。 public class LCLAdvantagePlusJackpotCache: C
我是 IOS swift 开发新手。我曾经使用以前的 Xcode 6 beta。 我已经下载了 Xcode 6.0.1 但我无法让它工作 Xcode Version: 6.0.1 当我尝试运行示例时,
以下 Scala 声明是可以的: trait Base[B y) then x else y 这相当于将 Ordering(与 java.util.Comparator 相同)传递给函数。确实,宣言
我是一名优秀的程序员,十分优秀!