0

I'm writing some functions in Clojure to act as helpers. Right now, I'm writing functions that make using Java's random generator easier to use.

Since random-float and random-double will be nearly identical except for the exact Java function called, I decided to try and factor out the identical parts and use the resulting function:

(import '[java.util Random])

(defn- random-floating [f min max ^Random rand-gen]
  (let [r (f rand-gen)
        spread (- max min)
        rand (* spread r)]
    (+ rand min)))

; Errors at ".nextFloat" and ".nextDouble"
(defn random-float [min max ^Random rand-gen]
  (random-floating .nextFloat min max rand-gen)) 

(defn random-double [min max ^Random rand-gen]
  (random-floating .nextDouble min max rand-gen))


(defn random-boolean [rand-gen]
  (.nextBoolean rand-gen))       ; <<--- This works fine

The problem is, I'm getting "unresolved symbol" errors at both .nextFloat and .nextDouble, even though if I call them directly, they're found.

I tried qualifying them (Random/.nextFloat and java.util.Random/.nextFloat), and neither worked.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
  • 1
    hope this will help http://stackoverflow.com/questions/23118435/usage-of-java-methods-as-a-function-argument-in-clojure – OlegTheCat May 29 '16 at 22:54
  • @OlegTheCat Ahh! Damn. Thanks. Thats such an easy fix. I guess this is a dupe then. I'll accept if you post that. – Carcigenicate May 29 '16 at 22:55

0 Answers0