Clojure:在let绑定中扩展var [英] Clojure: Expand a var in let binding

查看:56
本文介绍了Clojure:在let绑定中扩展var的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重用一组不同功能的let形式的局部分配.比方说

I want to reuse a set of local assignments in the let form of different functions. Let's say

(def common-assign
  [x 10
   y 20])

一种方法是使用eval:

(eval `(defn ~'foo []
         (let [~@common-assign
               ~'hello "world"])
         balala))

问题在于,现在您必须引用所有其他符号,这很麻烦.

The problem is that now you have to quote all other symbols, which is cumbersome.

还有其他干净的方法可以做我想做的事吗?

Is there any other clean way to do what I want?

推荐答案

(defmacro with-common [& body]
  `(let ~'[x 10, y 20]
     ~@body))

(with-common (+ x y))

这篇关于Clojure:在let绑定中扩展var的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆