5

在“Clojure in Action”(第 63 页)中处理以下示例:

(defn basic-item-total [price quantity] 
    (* price quantity))

(defn with-line-item-conditions [f price quantity] 
    {:pre [(> price 0) (> quantity 0)]
     :post [(> % 1)]} 
    (apply f price quantity))

评估 REPL:

(with-line-item-conditions basic-item-total 20 1)

导致引发以下异常:

Don't know how to create ISeq from: java.lang.Long
  [Thrown class java.lang.IllegalArgumentException]

在评估应用程序之后似乎正在引发异常。

4

1 回答 1

8

的最后一个参数apply应该是一系列参数。在您的情况下,用法可能看起来更像这样:

(defn with-line-item-conditions [f price quantity] 
    {:pre [(> price 0) (> quantity 0)]
     :post [(> % 1)]} 
    (apply f [price quantity]))

apply在处理参数列表时很有用。在您的情况下,您可以简单地调用该函数:

(defn with-line-item-conditions [f price quantity] 
    {:pre [(> price 0) (> quantity 0)]
     :post [(> % 1)]} 
    (f price quantity))
于 2012-08-24T05:25:28.723 回答