Tomek Wałkuski
Tomek Wałkuski

Reputation: 1019

rand-nth always yields the same result after compilation

I've project created via Leiningen with core.clj:

(ns cotd.core
  (:gen-class)
  (:use [clojure.repl :only (doc)]))

(defmacro eval-doc
  [form]
  (let [resulting-symbol (eval form)]
    `(doc ~resulting-symbol)))

(defn- random-function-name []
  (rand-nth (keys (ns-publics 'clojure.core))))

(defn -main
  "Display random doc page"
  [& args]
  (eval-doc (random-function-name)))

And after compiling and running it always yields the same result:

$ java -jar cotd.jar
-------------------------
clojure.core/unchecked-negate
([x])
  Returns the negation of x, a long.
  Note - uses a primitive operator subject to overflow.
$ java -jar cotd.jar
-------------------------
clojure.core/unchecked-negate
([x])
  Returns the negation of x, a long.
  Note - uses a primitive operator subject to overflow.

But with two consecutive calls:

(do
  (eval-doc (random-function-name))
  (eval-doc (random-function-name))))

It yields two different results in single "call".

What I've tried is googling, reading, etc. but I have no clues what's going on...

How to invoke this rand-nth dynamically?

Upvotes: 0

Views: 188

Answers (1)

Tomek Wałkuski
Tomek Wałkuski

Reputation: 1019

The problem wasn't with rand-nth but because the resulting-symbol in the let statement is produced during the compilation phase. @beyamor provided answer here: Unable to get random (doc) from a namespace

Upvotes: 2

Related Questions