在Clojure的早期如何从函数返回? [英] How do you return from a function early in Clojure?

查看:146
本文介绍了在Clojure的早期如何从函数返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Common Lisp有 return-from ;在Clojure中有什么类型的 return ,当你想早点从函数返回时?

Common Lisp has return-from; is there any sort of return in Clojure for when you want to return early from a function?

推荐答案

clojure中没有任何明确的return语句。你可以使用catch / throw组合在一起,如果你想,但由于clojure比普通的lisp更多的功能,你实际上需要 一个早期返回在某些嵌套块比在CL中小得多。我可以看到返回语句的唯一好的原因是,当你处理的可变对象在clojure不是惯用的方式。

There isn't any explicit return statement in clojure. You could hack something together using a catch/throw combination if you want to, but since clojure is much more functional than common lisp, the chances you actually need an early return right in the middle of some nested block is much smaller than in CL. The only 'good' reason I can see for return statements is when you're dealing with mutable objects in a way that's not idiomatic in clojure.

我不会去只要说它从来没有用,但我认为在Clojure,如果你的算法需要一个return语句,它是一个主要的代码气味。

I wouldn't go as far as saying that it's never useful, but I think in Clojure, if your algorithm needs a return statement, it's a major code smell.

这篇关于在Clojure的早期如何从函数返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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