Problem with passing a vector as a binding to the for macro

后端 未结 4 1096
野性不改
野性不改 2021-02-20 06:13

I have an arbitrary number of lists which I would like to process using the for macro. I want to create a function that passes a vector as the binding since the number of lists

4条回答
  •  -上瘾入骨i
    2021-02-20 06:40

    Here is a method of last resort. Be warned, wherever you see read-string that is code for Here Be Dragons! (Due to security risks, and lack of compile-time consistency guarantees about the behaviour of your code)

    (def list1 '("pink" "green"))
    (def list2 '("dog" "cat"))
    (for [A list1 B list2] (str A "-" B))
    
    (def testvector (vec (list 'A list1 'B list2)))
    
    (def testvector-vec (vec (list 'A (vec list1) 'B (vec list2))))
    
    (def for-string (str "(for " testvector-vec "(str A \"-\" B))"))
    
    (eval (read-string for-string))
    > ("pink-dog" "pink-cat" "green-dog" "green-cat")
    

提交回复
热议问题