Clojure UUID - 为 defrecords 创建 ID 时遇到问题

Clojure UUID - Having trouble creating IDs for defrecords

我想做的就是在创建 clojure defrecord 时为它们创建一个自动生成的 UUID。我试过以下方法:

(ns myns
  (:require [clj-uuid :as uuid])

(defrecord Thing [thing-id name])

(defn create-thing
  [name]
  (map->Thing {:thing-id (uuid/v1)
               :name name}))

其次是:

(repeat 5 (create-thing "bob"))

但是我为我创建的每个 Thing 创建了 相同的 UUID。帮助将不胜感激!

考虑到使用 jvm 附带的内置 UUID class 通过互操作很容易做到这一点,我对为此使用专用库持怀疑态度。

(ns myns
  (:import (java.util UUID)))

(defrecord Thing [thing-id name])

(defn create-thing
  [name]
  (map->Thing {:thing-id (UUID/randomUUID)
               :name name}))

;; using repeatedly instead of repeat generates new values,
;; instead of reusing the initial value

(repeatedly 5 #(create-thing "bob"))