动态代码与clojure [英] Dynamic code with clojure

查看:85
本文介绍了动态代码与clojure的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在clojure中创建一些动态代码。在下面的函数中,想法是动态生成(和)宏的条件。

I'm trying to create some dynamic code within clojure. In the function below, the idea is that the conditions for the (and) macro will be dynamically generated.

(defn matching-keys [rec match-feed keys]
  (> (count (clojure.set/select #(and (for [k keys]
                                        (= (% k) (rec k))))
                                (set match-feed)))
     0))

工作!那么当传递 [:tag:attrs] 的键时,这个代码会产生一个类似这样的东西:

So if it worked!! then this code would produce an (and) something like this when passed keys of [:tag :attrs]:

(and (= (% :tag) (rec :tag))
     (= (% :attrs) (rec :attrs)))

我一直在使用各种``和〜`尝试使它工作,并且现在处于混乱的状态。欢迎任何指导。

I've been messing around with various `` and~` operators to try to make it work, and am now in a state of confusion. Any guidance is welcome.

感谢,

Colin

推荐答案

您不需要动态生成的代码。将匿名函数更改为#(每个?(fn [k](=(%k)(rec k)))keys)

You don't need dynamically generated code for this. Changing the anonymous function to #(every? (fn [k] (= (% k) (rec k))) keys) should do what you want without generating code at runtime.

使用高阶函数的功能意味着你几乎不需要动态生成代码。

The ability to use higher-order functions means that you should hardly ever need to dynamically generate code.

这篇关于动态代码与clojure的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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