Clojure:前置函数 [英] Clojure: pre post functions

查看:111
本文介绍了Clojure:前置函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 http://blog.fogus.me/2009/12/21/clojures-pre-and-post/

我想要的不是前/后条件。

What I want is not exactly pre/post conditions.

我想要执行

I want to have pre/post functions that are executed exactly once.

我没有看到任何文档向我承诺这个功能关于pre / post条件(即他们没有执行多次。 )

I don't see any documentation promising me this feature about the pre/post conditions (i.e. that they're not executed multiple times.)

对于Clojure函数,

For a Clojure function, is there anyway to tag it with pre/post functions that are executed exactly once,


  • 当函数被调用时,pre函数
  • li>函数返回时的后函数
  • the pre function when the function is called
  • the post function when the function returns

谢谢!

推荐答案

您可以使用更高阶的函数相对容易地做到这一点:

You could do this relatively easily with a higher order function:

(defn wrap-fn [function pre post]
  (fn [& args]
    (apply pre args)
    (let [result (apply function args)]
      (apply post (cons result args)))))

(def f
  (wrap-fn
    +
    #(println (str "Calling function with args: " %&))
    #(println (str "Returning with result: " (first %&)))))

(f 2 3)
Calling function with args: (2 3)
Returning with result: 5

这篇关于Clojure:前置函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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