Clojure: Advance Message Features

Context

I know http://blog.fogus.me/2009/12/21/clojures-pre-and-post/

What I want is not quite the preconditions.

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

I do not see any documentation, promising me this function about preconditions (i.e. they are not executed several times.)

Question

For a Clojure function, anyway, to tag it with pre / post functions that execute exactly once,

  • pre-function when the function is called
  • post function when the function returns

Thanks!

+6
source share
2 answers

You can 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 
+2
source

Dire will do just that. All precondition predicates are called before evaluating a function. If the predicates all return true, the function evaluates. Otherwise, an exception is thrown. The following is an estimate of all postcondition predicates.

0
source

Source: https://habr.com/ru/post/916807/


All Articles