代码之家  ›  专栏  ›  技术社区  ›  Kristian Salo

调用惰性序列计数时出现arityException

  •  0
  • Kristian Salo  · 技术社区  · 6 年前

    我已经编写了以下函数来确定两个字符串是否完全不同 differing-char-no 角色位置。

    (defn differ-by
        [id1 id2 differing-char-no]
        (let [zipped-ids (map vector (seq id1) (seq id2))
              differing-char-positions (filter #(not= %1 %2) zipped-ids)]
            (= differing-char-no (count differing-char-positions))))
    

    然而,函数产生一个 ArityException 当被召唤时。例外来自 count 函数调用,并说 Wrong number of args (1) passed to: ... .

    通过调试代码,我发现 differing-char-positions zipped-ids 是尚未实现的惰性序列。但是,评估 (count zipped-ids) 马上就好了,但是 (count differring-char-positions) 只有打过电话才能工作 (统计压缩ID) 一次,即之后 拉链入侵检测系统 已经实现。有人能解释这种行为吗?为什么这些懒惰的序列表现不同?

    1 回复  |  直到 6 年前
        1
  •  0
  •   akond    6 年前

    zipped-ids 生成向量序列,因此过滤函数必须接受单个参数:

    (filter (fn [[a b]] (not= a b)) (map vector (seq [1 2 3]) (seq '(:a :b :c))))

    但还有更好的方法:

    (filter (partial apply not=) (map vector (seq [1 2 3]) (seq '(1 :b :c))))

    推荐文章