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

查看:18
本文介绍了如何从 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 小得多.我能看到 return 语句的唯一好"原因是当您以一种在 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 中,如果您的算法需要返回语句,那是主要的代码异味.

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天全站免登陆