Clojure函数 - 返回在最后一条语句之前计算的值 [英] Clojure functions - returning value computed before the last statement

查看:91
本文介绍了Clojure函数 - 返回在最后一条语句之前计算的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些测试用clojure写的。这是一个简单的例子:

I have some tests written in clojure. This is a simple example:

(defn test1
  []
  (start-server)
  (run-pvt-and-expect "PVT-0")
  (stop-server)
 )

我想返回run-pvt-and-expect的结果,但是我需要在它之后执行其他函数。我如何在clojure中以功能的方式(不使用像let这样的结构)?感谢。

I would like to return the result of "run-pvt-and-expect", but I need to execute other functions after it. How can I do this in clojure in a functional way (without using constructs like "let")? Thanks.

注意:我阅读了这个问题及其答案,但不能应用于我的情况。此外,一个评论要求的例子从来没有,所以请考虑这是一个例子...

Note: I read this question and its answers but couldn't apply it to my case. Also, a comment asked for examples that were never given, so please consider this as an example...

推荐答案

(try 
    (+ 2 3)
    (finally (+ 6 7)))

产生5的结果不是13.

produces a result of 5 not 13.

这是相关的文档页面。
相关部分是:

Here is the relevant doc page. The releavant part is:


任何最终的exprs将被评估其副作用。

any finally exprs will be evaluated for their side effects.

另一个选项是编写一个类似于open的宏,以在返回正确的结果时启动和停止服务器。虽然这只是将问题移到了你如何写宏,因为它需要使用let或者try finally结构上面。

Another option would be to write a macro like with-open to start and stop your server while returning the right results. Though that just moves the problem to how do you write the macro, since it would either need to use a let or the try finally construct above.

这篇关于Clojure函数 - 返回在最后一条语句之前计算的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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