sjp
sjp

Reputation: 392

Why are both expressions in this clojure if evaluated?

If I create a new project with

lein new quil foo

then reduce the src/foo/core.clj to

(ns foo.core
  (:require [quil.core :as q]
            [quil.middleware :as m]))

(defn draw [state]
  (if (< (rand) 0.5)
    (q/line 0 0 500 500)
    (q/line 0 500 500 0)))

(q/defsketch foo
  :title "Try this at home"
  :size [500 500]
  :draw draw
  :features [:keep-on-top]
  :middleware [m/fun-mode])

and evaluate the program in lein repl with (use 'foo.core), both lines are drawn (i.e. I get a big X). (if (< (rand) 0.5) true false) works as expected, so what am I missing?

Upvotes: 0

Views: 72

Answers (1)

amalloy
amalloy

Reputation: 91857

Presumably draw is called many times - it wouldn't be a very interactive framework if it only drew the screen once! Some of those times, you randomly choose to draw one line, and sometimes the other; and you never erase either. As a result, both of them are visible on the screen.

Upvotes: 7

Related Questions