How to make a record from a sequence of values

后端 未结 5 1315
执笔经年
执笔经年 2020-12-20 13:38

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 value

5条回答
  •  清酒与你
    2020-12-20 14:05

    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).

提交回复
热议问题