元编程 - 自解释代码 - 教程,文章,书籍 [英] Metaprogramming - self explanatory code - tutorials, articles, books

查看:253
本文介绍了元编程 - 自解释代码 - 教程,文章,书籍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找改善我的编程技巧(实际上我尽量每年吸收更少,正如我们的Jeff Atwood所说),所以我正在考虑阅读有关元编程和自我解释代码的东西。



我正在寻找类似于白痴的指南(免费书籍下载,在线资源)。此外,我想要的比你的平均wiki页面和一些语言不可知或最好用Java示例。



你知道这样的资源,将允许有效地把所有的(我知道经验有很多话要说,但我想要建立经验,避免流的坏决定 - 经验 - 好的决定)?



EDIT:



此示例中的实用程序员



...实现一个迷你语言来控制一个简单的绘图包...该语言由单字母命令组成。某些命令后面跟着一个数字。例如,以下输入将绘制一个矩形:

  P 2#选择笔2 
D# b $ b W 2#draw west 2cm
N 1#then north 1
E 2#then east 2
S 1#then back south
U#pen up

谢谢!

解决方案>

欢迎来到元编程的奇妙世界:)元编程涉及许多事情。我会尽量列出我的想法:





结论:元程序是程序对自身进行推理或修改自身的能力。就像元栈溢出本身是提出问题堆栈溢出本身的地方。元编程不是一种特定的技术,而是一系列的概念和技术。



一些事情属于元程序设计的范畴。从你的问题,你似乎对宏/ DSL部分更感兴趣。但一切都是最终相关的,所以元编程的其他方面也是绝对值得一看。



PS:我知道我提供的大多数链接不是教程或介绍性文章。这些是我喜欢的资源,描述元编程的概念和优势,我认为更有趣


I am looking into improving my programming skils (actually I try to do my best to suck less each year, as our Jeff Atwood put it), so I was thinking into reading stuff about metaprogramming and self explanatory code.

I am looking for something like an idiot's guide to this (free books for download, online resources). Also I want more than your average wiki page and also something language agnostic or preferably with Java examples.

Do you know of such resources that will allow to efficiently put all of it into practice (I know experience has a lot to say in all of this but i kind of want to build experience avoiding the flow bad decisions - experience - good decisions)?

EDIT:

Something of the likes of this example from the Pragmatic Programmer:

...implement a mini-language to control a simple drawing package... The language consists of single-letter commands. Some commands are followed by a single number. For example, the following input would draw a rectangle:

P 2 # select pen 2
D # pen down
W 2 # draw west 2cm
N 1 # then north 1
E 2 # then east 2
S 1 # then back south
U # pen up

Thank you!

解决方案

Welcome to the wonderful world of meta-programming :) Meta programming relates actually to many things. I will try to list what comes to my mind:

  • Macro. The ability to extend the syntax and semantics of a programming language was explored first under the terminology macro. Several languages have constructions which resemble to macro, but the piece of choice is of course Lisp. If you are interested in meta-programming, understanding Lisp and the macro system (and the homoiconic nature of the languge where code and data have the same representation) is definitively a must. If you want a Lisp dialect that runs on the JVM, go for Clojure. A few resources:

    There is otherwise plenty of resource about Lisp.

  • DSL. The ability to extend one language syntax and semantics is now rebranded under the term "DSL". The easiest way to create a DSL is with the interpreter pattern. Then come internal DSL with fluent interface and external DSL (as per Fowler's terminology). Here is a nice video I watched recently:

    The other answers already pointed to resources in this area.

  • Reflection. Meta-programming is also inseparable form reflection. The ability to reflect on the program structure at run-time is immensely powerful. It's important then to understand what introspection, intercession and reification are. IMHO, reflection permits two broad categories of things: 1. the manipulation of data whose structure is not known at compile time (the structure of the data is then provided at run-time and the program stills works reflectively). 2. powerful programming patterns such as dynamic proxy, factories, etc. Smalltalk is the piece of choice to explore reflection, where everything is reflective. But I think Ruby is also a good candidate for that, with a community that leverage meta programming (but I don't know much about Ruby myself).

    There is also a rich literature on reflection.

  • Annotations. Annotations could be seen as a subset of the reflective capabilities of a language, but I think it deserves its own category. I already answered once what annotations are and how they can be used. Annotations are meta-data that can be processed at compile-time or at run-time. Java has good support for it with the annotation processor tool, the Pluggable Annotation Processing API, and the mirror API.

  • Byte-code or AST transformation. This can be done at compile-time or at run-time. This is somehow are low-level approach but can also be considered a form of meta-programming (In a sense, it's the same as macro for non-homoiconic language.)

Conclusion: Meta-programming is the ability for a program to reason about itself or to modify itself. Just like meta stack overflow is the place to ask question about stack overflow itself. Meta-programming is not one specific technique, but rather an ensemble of concepts and techniques.

Several things fall under the umbrella of meta-programming. From your question, you seem more interested in the macro/DSL part. But everything is ultimately related, so the other aspects of meta-programming are also definitively worth looking at.

PS: I know that most of the links I've provided are not tutorials, or introductory articles. These are resources that I like which describe the concept and the advantages of meta-programming, which I think is more interesting

这篇关于元编程 - 自解释代码 - 教程,文章,书籍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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