在clojure中,如何使用匿名函数字面量写恒等函数? [英] In clojure, how to write the identity function using the anonymous function literal?

查看:27
本文介绍了在clojure中,如何使用匿名函数字面量写恒等函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用匿名函数字面量(#())?

以下代码不起作用:

(#(%) 5)

它引发异常,因为它被转换为:

It raises an exception because it is converted to:

((fn[x] (x)) 5)

问题在于使用#()时,函数体被括号包围.任何想法,如何优雅地克服这个问题?

The problem in that when using #(), the function body is enveloped with parentheses. Any idea, how to elegantly overcome this?

推荐答案

嗯,首先,有 identity 功能.

Well, first of all, there is the identity function.

但是你可以使用

#(do %)

如果你坚持.

这篇关于在clojure中,如何使用匿名函数字面量写恒等函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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