3

Folks, I have the below function but I'm getting a java.lang.ClassCastException: java.lang.Boolean cannot be cast to clojure.lang.IFn" error when I run. Any thoughts on how to fix it?

(defn tickets [price] (fn [price] ((and proms/med-to-high?
                                           (partial checks/price-matches? price)))))

I am trying to use the above function in the below function. What I want to do is to check if a ticket is at a specific price and if not, print the ticket was not

(defn right-tickets?
 "Returns true for right ticket.Else, print something"
  [price]
  (if (tickets? price) true (do (println "Ticket not at right price") false)))

Any suggestion is highly appreciated

2 Answers 2

3
((and proms/med-to-high?
 (partial checks/price-matches? price))

There is your problem. You evaluate the and expression, and then you try to treat the resulting boolean like a function. Try removing the outer parentheses from that expression.

Sign up to request clarification or add additional context in comments.

1 Comment

Made the correction but did not seem to fix the issue
0

Aside from the error that @user100464 points out,

  • there is too much global state
  • you are mixing computation with reporting.

But first, you call function tickets?, having defined one with the different name of tickets. Let's go for the latter.

In tickets, proms/med-to-high? appears to be global data. I guess from the wording that it applies to some event, which also has some ticket prices attached. We can represent the event data as a map, say

{:prom :low, :prices #{10.50 15.00 25.00}} 

Then your tickets function can be

(defn tickets [event price]
  (and (#{:medium :high} (:prom event)) ((:prices event) price)))

For instances,

(tickets {:prom :low, :prices #{10.50 15.00 25.00}} 15.0)
;nil

(tickets {:prom :medium, :prices #{10.50 15.00 25.00}} 10.0)
;nil

(tickets {:prom :medium, :prices #{10.50 15.00 25.00}} 15.0)
;15.0

But if I am prepared to spend £20 on a ticket, then one at £10 is fine. So I want to see the range of qualifying prices:

(defn tickets [event price]
  (and
    (#{:medium :high} (:prom event))
    (seq (filter (partial >= price) (:prices event)))))

For instances,

(tickets {:prom :medium, :prices #{10.50 15.00 25.00}} 10.0)
;nil

(tickets {:prom :medium, :prices #{10.50 15.00 25.00}} 20.0)
;(10.5 15.0)

It might be that the prices were a map, giving the number of tickets at each price ... and so on.

The ticket message function can be

(def ticket-message {nil "No tickets at the price"})

Which, applied to nil, yields the message, but applied to anything else, yields nil.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.