Clojure中的数字参数的验证 [英] Validation of Numeric Arguments in Clojure

查看:144
本文介绍了Clojure中的数字参数的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Clojure的功能:

I have a clojure function:

(defn f [arg1 arg2]
  ...)

我想测试,如果 ARG1 ARG2 的数字(仅适用于数字类型应该传递 - 不是数字格式化字符串)。有,当然,方法可以做到这一大堆,但我想这样做的惯用越好。建议?

I would like to test if arg1 and arg2 are numeric (only numeric types should pass - not numerically formatted strings). There are, of course, a whole bunch of ways to do this, but I'd like to do it as idiomatically as possible. Suggestions?

编辑:我知道关于:pre 。对是否就是处理这将是AP preciated适当/必要途径发表任何评论。

I know about :pre. Any comment on whether or not that is an appropriate/necessary way to handle this would be appreciated.

推荐答案

pre-条件下能做到这一点:

Pre-conditions can do that:

(defn test [arg1 arg2]
  {:pre [(number? arg1) (number? arg2)]}
  (+ arg1 arg2))

(test 1 2)
=> 3

(test 1 "2")
=> Assert failed: (number? arg2)

请参阅 http://clojure.org/special_forms#toc9 获取文档。

这篇关于Clojure中的数字参数的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆