简体   繁体   English

Clojure中尚未提供的简单但令人信服的宏观示例

[英]Simple Yet Compelling Macro Examples which are Not Already in Clojure

I'm trying to write a macro tutorial, and now I need some examples which are simple to understand, and yet compelling. 我正在尝试编写一个宏教程,现在我需要一些简单易懂但又引人注目的示例。

The problem is that a lot of the obvious things are already in clojure and contrib. 问题是很多明显的事情已经在clojure和contrib中。 And I feel that "look, we can reimplement all the library functions" might not be the best argument for why macros are so great. 我觉得“看,我们可以重新实现所有的库函数”可能不是宏为什么如此伟大的最好的论据。

Has anyone got any cute (one-liners are best) examples that they wouldn't mind me using? 有没有人有任何可爱的(单行是最好的)例子,他们不介意我使用?

Here are the first three parts of the tutorial. 以下是本教程的前三部分。 It's a bit ropy at the moment, so any comments about how it could be made better would be gratefully received. 目前这有点危险,所以任何关于如何做得更好的评论都会感激不尽。

http://learnclojure.blogspot.com/2010/09/clojure-macro-tutorial-part-i-getting.html http://learnclojure.blogspot.com/2010/09/clojure-macro-tutorial-part-i-getting.html

http://learnclojure.blogspot.com/2010/09/clojure-macro-tutorial-part-ii-compiler.html http://learnclojure.blogspot.com/2010/09/clojure-macro-tutorial-part-ii-compiler.html

http://learnclojure.blogspot.com/2010/09/clojure-macro-tutorial-part-ii-syntax.html http://learnclojure.blogspot.com/2010/09/clojure-macro-tutorial-part-ii-syntax.html

I'm working on some cryptography software in clojure. 我正在使用clojure中的一些加密软件。 Its really fun and using unit testing makes it more fun because I don't get nervous about breaking things. 它非常有趣并且使用单元测试使它更有趣,因为我不会对破坏事情感到紧张。 The trouble is that all the crypto functions generate different results every time because they are driven by a fairly good IMHO psudo random number generator. 问题是所有加密函数每次都会产生不同的结果,因为它们是由相当好的IMHO psudo随机数生成器驱动的。

How do I test randomized functions? 我如何测试随机函数?

with a bind macro of course! 当然还有绑定宏!

(defmacro with-fake-prng [ & exprs ]
  "replaces the prng with one that produces consisten results"
  `(binding [com.cryptovide.split/get-prng (fn [] (cycle [1 2 3]))
             com.cryptovide.modmath/mody 719
             com.cryptovide.modmath/field-size 10]
       ~@exprs))

then I wrap my test functions in (with-fake-prng (deftest mytest ....)) 然后我将测试函数包装在(with-fake-prng (deftest mytest ....))

clojure has a lot of these "bind macroes". clojure有很多这些“绑定宏”。 like with-out-string and such. 喜欢with-out-string等。

I also have a macro that loads every namespace into the repl. 我还有一个宏,它将每个命名空间加载到repl中。 (I dont use this much now that I have switched to cake) (现在我已经切换到蛋糕,我不会用这么多)

(defmacro load-all []
  '(use 
   :reload-all
   'com.cryptovide.modmath
    ...  
   'com.cryptovide.gui
   'com.cryptovide.checksum
   'com.cryptovide.log))

ps: always mind the first rule of macro club ps:永远记住宏观俱乐部的第一条规则

I would talk more about patterns: when and how is a macro used. 我会更多地讨论模式:何时以及如何使用宏。 eg... 例如...

  • Protecting a resource. 保护资源。 Examples: binding , with-open , ... 示例: bindingwith-open ,......
     (let [~x (get-resource)] (try ~@dostuff (finally (release-resource ~x)))) 
  • Defining things. 定义的东西。 Examples: defn , defsnippet (enlive), defservice (ring) 示例: defndefsnippet (enlive), defservice (ring)
  • Macro/driver split. 宏/驱动程序拆分。 Especially this technique takes away a lot of macro pain. 特别是这种技术消除了很多宏观痛苦。 Like multiple evaluation or capture. 喜欢多次评估或捕获。 Example: with-bindings 示例: with-bindings
  • Beautifying ugly code. 美化丑陋的代码。 eg. 例如。 when Taming multi-dim arrays Taming多暗阵列时

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM