Nevena
Nevena

Reputation: 707

How to make a record from a sequence of values

I have a simple record definition, for example

(defrecord User [name email place])

What is the best way to make a record having it's values in a sequence

(def my-values ["John" "[email protected]" "Dreamland"])

I hoped for something like

(apply User. my-values)

but that won't work. I ended up doing:

(defn make-user [v]
  (User. (nth v 0) (nth v 1) (nth v 2)))

But I'm sensing there is some better way for achieving this...

Upvotes: 12

Views: 1127

Answers (6)

skylize
skylize

Reputation: 1421

The idiomatic way to call a Record constructor is with the Clojure symbol ->MyRecord and that works just fine with apply.

(def my-values ["John" "[email protected]" "Dreamland"])
(defrecord User [name email place])
(apply ->User my-values)

; => #my-ns.User{:name "John",
                 :email "[email protected]",
                 :place "Dreamland"}

Upvotes: 0

subsub
subsub

Reputation: 1857

Update for Clojure 1.4

defrecord now defines ->User and map->User thus following in Goran's footstaps, one can now

(defmacro instantiate [rec args] `(apply ~(symbol (str "->" rec)) ~args))

which also works with non-literal sequences as in (instantiate User my-values). Alternatively, along the lines of map->User one can define a function seq->User

(defmacro def-seq-> [rec] `(defn ~(symbol (str "seq->" rec)) [arg#] (apply ~(symbol (str "->" rec)) arg#)))

(def-seq-> User)

which will allow (seq->User my-values).

Upvotes: 0

Ross Goddard
Ross Goddard

Reputation: 4302

One simple thing you can do is to make use of destructuring.

(defn make-user [[name email place]]
  (User. name email place))

Then you can just call it like this

(make-user ["John" "[email protected]" "Dreamland"])

Upvotes: 2

Goran Jovic
Goran Jovic

Reputation: 9508

Warning: works only for literal sequables! (see Mihał's comment)

Try this macro:

(defmacro instantiate [klass values] 
        `(new ~klass ~@values))

If you expand it with:

(macroexpand '(instantiate User ["John" "[email protected]" "Dreamland"]))

you'll get this:

(new User "John" "[email protected]" "Dreamland")

which is basically what you need.

And you can use it for instantiating other record types, or Java classes. Basically, this is just a class constructor that takes a one sequence of parameters instead of many parameters.

Upvotes: 4

Brian Carper
Brian Carper

Reputation: 72926

Writing your own constructor function is probably the way to go. As Arthur Ulfeldt said, you then have a function you can use as a function (e.g. with apply) rather than a Java-interop constructor call.

With your own constructor function you can also do argument validation or supply default arguments. You gain another level of abstraction to work with; you can define make-user to return a hash-map for quick development, and if you later decide to change to records, you can do so without breaking everything. You can write constructors with multiple arities, or that take keyword arguments, or do any number of other things.

(defn- default-user [name]
  (str (.toLowerCase name) "@example.com"))

(defn make-user
  ([name] (make-user name nil nil))
  ([name place] (make-user name nil place))
  ([name user place]
     (when-not name
       (throw (Exception. "Required argument `name` missing/empty.")))
     (let [user (or user (default-user name))]
       (User. name user place))))

(defn make-user-keyword-args [& {:keys [name user place]}]
  (make-user name user place))

(defn make-user-from-hashmap [args]
  (apply make-user (map args [:name :user :place])))

user> (apply make-user ["John" "[email protected]" "Somewhere"])
#:user.User{:name "John", :email "[email protected]", :place "Somewhere"}

user> (make-user "John")
#:user.User{:name "John", :email "[email protected]", :place nil}

user> (make-user-keyword-args :place "Somewhere" :name "John")
#:user.User{:name "John", :email "[email protected]", :place "Somewhere"}

user> (make-user-from-hashmap {:user "foo"})
; Evaluation aborted.
; java.lang.Exception: Required argument `name` missing/empty.

Upvotes: 4

Arthur Ulfeldt
Arthur Ulfeldt

Reputation: 91554

the defrecord function creates a compiled class with some immutable fields in it. it's not a proper clojure functions (ie: not a class that implements iFn). If you want to call it's constructor with apply (which expects an iFun) you need to wrap it in an anonymous function so apply will be able to digest it.

(apply #(User. %1 %2 %3 %4) my-values)

it's closer to what you started with though your approach of defining a constructor with a good descriptive name has its own charm :)

from the API:

Note that method bodies are
not closures, the local environment includes only the named fields,
and those fields can be accessed directy.

Upvotes: 4

Related Questions