Clojure UUID - 无法为defrecords创建ID

时间:2015-08-15 02:05:25

标签: clojure uuid

我尝试做的就是为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。帮助将不胜感激!

1 个答案:

答案 0 :(得分:3)

我怀疑使用专用的lib,因为使用jvm附带的内置UUID类通过互操作是多么容易。

(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"))