dtg
dtg

Reputation: 1853

Error using "apply" function in Clojure: "Don't know how to create ISeq from: java.lang.Long"

Working 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))

Evaluating on the REPL:

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

Results in the following exception being thrown:

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

It appears the exception is being thrown after the apply procedure is evaluated.

Upvotes: 5

Views: 3293

Answers (1)

Beyamor
Beyamor

Reputation: 3378

The last argument to apply is supposed to be a sequence of arguments. In your case, the usage might look more like this:

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

apply is useful when you're 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))

Upvotes: 8

Related Questions