开发者

Dynamic let List Destructuring in Clojure

开发者 https://www.devze.com 2023-01-06 17:08 出处:网络
I have a let statement in which I would like to dynamically destructure a list. The following is my solution:

I have a let statement in which I would like to dynamically destructure a list. The following is my solution:

symList  ;; list of some Strings which will become the vector of Symbols to assign to
valList  ;; list of some values, same length as symList

(let [(map read-st开发者_Python百科ring symList) valList]
  ...)

An example value of symList would be ("pt1" "pt2") and an example value of valList would be (1 2)

However, this produces an exception that the first part is an "unsupported binding form". I suspect I am missing something regarding syntax-quoting, or that it's not possible. Any advice would be greatly appreciated.

EDIT: I will only know these values come run time, hence this approach. Secondly, I need to be able to pass the lexical scope later on, hence the use of let.


If symList and valList have the correct value at compilation time, then you could write a macro to do this. If they are only known at runtime, you are going to have to use/write a function to do the destructuring for you and return the result of that destructuring as some data structure.

In the second case, for something simple like this, you can use (zipmap symList valList) to get a map.


(let [valList (map str symList)]
   (somefun valList))

The error you are getting is because your let statement was backwards (let [val SOURCE] STUFF)


maybe matchure can give you what you want


Although I've been unable to find a way to dynamically destructure a list, for those of you who are interested in creating values that are lexically instead of dynamically scoped, I found that intern to whatever namespace you need works well.

0

精彩评论

暂无评论...
验证码 换一张
取 消