Lisp / Clojure:编写函数生成宏是个好主意吗? [英] Lisp / Clojure: Is it a good idea to write function generating macros?

查看:95
本文介绍了Lisp / Clojure:编写函数生成宏是个好主意吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题要求创建一个Clojure宏来生成多个函数。我们想出了一种方法来做到这一点,但被困在这是个好主意的问题。



我的初步反应是不是真的,有两个原因


  1. 然后你的代码中没有定义函数,这可能会使你的代码复杂化! (想象一下,有人对你的一个函数有一个问题,看着源代码只能在任何地方找不到它)。

  2. 最好是考虑函数中代码的通用性或宏。让你的电脑写一堆非常相似的函数是一个很不好的方法。

你觉得怎么样?什么时候生成Lisp中的函数有意义?

解决方案

TL; DR:是什么意思?

可以将共性或子集抽象为一个函数(或多个函数),只有 em>通过宏定义的更动态的位?在可能的情况下,最好尽可能限制宏的范围。



函数/宏的自然是什么?如果他们是一个记录良好的系统方面的一部分,他们来自哪里不是真正重要。



他们很少了解,他们需要频繁检查以了解或验证行为?如果是这样,那么将它们作为实际功能可能更有意义。



每个人维护的函数/宏

或者更专注于底层系统实现的人?如果他们大部分被消费,那么它们的实施方式/时间/地点就更少了。


This question asks to create a Clojure macro to generate several functions. We figured out a way to do this but were stuck with the question of "Is this a good idea?".

My initial reaction is not really, for two reasons

  1. You then have functions that are not defined in your code, and this can complicate understanding your code quite a bit! (Imagine somebody has a problem with one of your functions and looks at the source code only to not find it anywhere).
  2. It is better to factor out the commonality of the code in a function or macro. Letting your computer write a bunch of functions that are very alike is a poor approach to that.

What do you think? When does generating functions in a Lisp make sense? Should it ever be 'on the fly' or would you prefer to have it in a file somewhere?

解决方案

TL;DR: It depends.

Can the commonality, or a subset, be abstracted into a function (or functions), with only the more dynamic bits defined through macros? When possible, it's best to make the macro-y parts as limited in scope as possible.

What's the nature of the functions/macros? If they're part of a well-documented system aspect, it doesn't really matter where they come from.

Are they poorly-understood, and do they require frequent inspection to understand or verify behavior? If so, then leaving them as real functions may make more sense. If they're not, and are more or less "stock" system aspects, do whatever is cleaner.

Are the functions/macros maintained by everybody, or by someone more focused on an underlying system implementation? If they're mostly consumed, it matters less how/where/when they're implemented.

这篇关于Lisp / Clojure:编写函数生成宏是个好主意吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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