如何将序列/值集合指定为clojure中函数的一个值?

时间:2016-08-22 09:33:12

标签: clojure clojure.spec

我试图以一种两个字符串的序列作为函数的第一个参数来规定一个函数。

这就是我的尝试:

(ns yoostan-lib.test
  (:require [clojure.spec :as s]
            [clojure.spec.gen :as gen]))

(s/def ::two-strings (s/cat :s1 string?
                            :s2 string?))

;; (gen/sample (s/gen ::two-strings) 3)
;; (("" "") ("7" "J") ("Tx1" "oQ"))

(s/fdef print-two-strings
        :args (s/cat :ss ::two-strings)
        :ret string?)

(defn print-two-strings
  [ss & rst]
  (with-out-str (clojure.pprint/pprint {:ss ss
                                        :rst rst})))

;; this is what I want
;; (print-two-strings '("oeu" "oeu"))
;; => "{:ss (\"oeu\" \"oeu\"), :rst nil}\n"

;; this is what I get instead
;; (s/exercise-fn `print-two-strings)
;; ([("" "") "{:ss \"\", :rst (\"\")}\n"] [("" "") "{:ss \"\", :rst (\"\")}\n"] [("90" "g") "{:ss \"90\", :rst (\"g\")}\n"]     [("IhE" "a6") "{:ss \"IhE\", :rst (\"a6\")}\n"] [("8P5" "70A") "{:ss \"8P5\", :rst (\"70A\")}\n"] [("738a" "41j4") "{:ss     \"738a\", :rst (\"41j4\")}\n"] [("M8" "4GD1") "{:ss \"M8\", :rst (\"4GD1\")}\n"] [("" "G") "{:ss \"\", :rst (\"G\")}\n"]     [("R" "8s43p") "{:ss \"R\", :rst (\"8s43p\")}\n"] [("C1e" "EY2AUE") "{:ss \"C1e\", :rst (\"EY2AUE\")}\n"])

要清楚。我遇到的问题是exercise-fn解释了我给它的fdef规范,意思是它可以传递我的函数两个参数,类型string?。我想要的是获得一个参数,由两个作为一个集合传递的字符串组成。

2 个答案:

答案 0 :(得分:2)

来自spec指南的Sequences部分:

  

当组合正则表达式操作时,它们描述单个序列。如果需要指定嵌套顺序集合,则必须使用对spec的显式调用来启动新的嵌套正则表达式上下文。

所以你可以像这样指定print-two-strings

(s/fdef print-two-strings
  :args (s/cat :ss (s/spec ::two-strings))
  :ret string?)

旁注:我发现您将参数垂直对齐fdef,而不是像spec指南那样使用双空格缩进。如果您正在使用CIDER,则可以将其配置为使用该宏的双空缩进,如文档here所述:

(put-clojure-indent 'clojure.spec/fdef 1)

或等同地:

(define-clojure-indent
  (clojure.spec/fdef 1))

这是我的Emacs配置中的example

答案 1 :(得分:2)

;; any of these will work, I'd probably use tuple here
(s/def ::two-strings (s/tuple string? string?))
(s/def ::two-strings (s/coll-of string? :count 2))
(s/def ::two-strings (s/coll-of string? :count 2 :into ())) ;; for lists in conformed value

(s/fdef print-two-strings
  :args (s/cat :ss ::two-strings :rst (s/? string?))
  :ret string?)

(pprint (s/exercise-fn `print-two-strings))

;;=> ([(["" ""] "") "{:ss [\"\" \"\"], :rst (\"\")}\n"]
 [(["H" "4"]) "{:ss [\"H\" \"4\"], :rst nil}\n"]
 [(["yZ" "7"] "OU") "{:ss [\"yZ\" \"7\"], :rst (\"OU\")}\n"]
 [(["" "FFt"]) "{:ss [\"\" \"FFt\"], :rst nil}\n"]
 [(["9" "Q0"]) "{:ss [\"9\" \"Q0\"], :rst nil}\n"]
 [(["o" "OuSA"]) "{:ss [\"o\" \"OuSA\"], :rst nil}\n"]
 [(["1JN" "bT"]) "{:ss [\"1JN\" \"bT\"], :rst nil}\n"]
 [(["IUY" ""]) "{:ss [\"IUY\" \"\"], :rst nil}\n"]
 [(["8G" "71H3r3d"]) "{:ss [\"8G\" \"71H3r3d\"], :rst nil}\n"]
 [(["qL" "zK3ZXA"] "9PV5X1")
  "{:ss [\"qL\" \"zK3ZXA\"], :rst (\"9PV5X1\")}\n"])