Error using the "apply" function in Clojure: "I don’t know how to create ISeq from: java.lang.Long"

Work on the following example in Clojure in Action (p. 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 score:

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

As a result, the following exceptions are thrown:

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

It seems that an exception is thrown after the application procedure is evaluated.

+5
source share
1 answer

The last argument applymust be a sequence of arguments . In your case, usage might look something like this:

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

applyuseful when you are working with a list of arguments. In your case, you can simply call the function:

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

All Articles