在Clojure中,如何使用匿名函数文字编写身份函数? [英] In clojure, how to write the identity function using the anonymous function literal?

查看:47
本文介绍了在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 函数.

但是您可以使用

#(do %)

如果你坚持的话.

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

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