Brandon
Brandon

Reputation: 3130

Clojure UUID - Having trouble creating IDs for defrecords

All I'm trying to do is create an auto-generated UUID for clojure defrecord's when they are created. I've tried the following:

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

(defrecord Thing [thing-id name])

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

Followed by:

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

But I get the same UUID created for every Thing I create. Help would be appreciated!

Upvotes: 2

Views: 468

Answers (1)

noisesmith
noisesmith

Reputation: 20194

I'm suspicious about using a dedicated lib for this, given how easy it is to do via interop using the built in UUID class that comes with the jvm.

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

Upvotes: 3

Related Questions