这个函数的正确类型注释是什么?

时间:2015-12-16 16:13:06

标签: clojure clojure-core.typed

我有以下效用函数,应该是自解释的:

(ns my.utility-belt
  "Use this everywhere."
  (:use clojure.core.typed))

(ann  zipfn (All [c a b ...] [[a b ... b -> c] (Seqable a) * -> (Seqable c)]))
(defn zipfn
  "Applies f to the interleaved elements of colls. If 2 colls are given, then f
  will recieve 2 arguments, one from each coll. If 3 colls are given, then f
  will receive 3 arguments, and so on. Returns a flat sequence of the results of
  applying f to its args."
  [f & colls]
  (map (partial apply f) (partition (count colls) (apply interleave colls))))

(comment
  (let [c1 [1 2 3 4 5]
        c2 [5 4 3 2 1]]
    (assert (= (zipfn * c1 c2)
               '(5 8 9 8 5)))))

我提供的注释并不完全正确,但我不知道为什么。致电(check-ns)只会给我“Type Error (my/utility_belt.clj:12:51) Bad arguments to polymorphic function in apply in (apply interleave colls)

在这种情况下,正确的类型注释是什么?

1 个答案:

答案 0 :(得分:4)

问题是core.typed无法推断这是zipfn的实际类型。解决此问题的最快方法是告诉core.typed忽略zipfn:no-check的定义。

(ann ^:no-check zipfn (All [c a b ...] [[a b ... b -> c] (Seqable a) * -> (Seqable c)]))