Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a vector to a record?

Tags:

clojure

For example, if I had:

(defrecord Item [name cost])

How could I convert ["ball" 10] to {:name "ball", :cost 10}?

like image 737
inline Avatar asked Aug 31 '12 07:08

inline


1 Answers

user=> (defrecord Item [name cost])
user=> (apply ->Item ["ball" 10])
#user.Item{:name "ball", :cost 10}

Short explain of what's going on. (->Item "ball" 10) is one of syntax for creating record from given arguments. It's the same as (Item. "ball" 10). In your case you have vector of arguments, so we use (apply fn args-vector) to deal with.

like image 107
Alexey Kachayev Avatar answered Nov 19 '22 10:11

Alexey Kachayev