Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 869
dtg Avatar asked Aug 24 '12 04:08

dtg


1 Answers

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))
like image 104
Beyamor Avatar answered Nov 15 '22 08:11

Beyamor