In clojure, how to write the identity function using the anonymous function literal?

心已入冬 提交于 2021-02-04 22:13:48

问题


How could one write the identity function in clojure using anonymous function literal (#())?

The following code doesn't work:

(#(%) 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?


回答1:


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

But you can use

#(do %)

if you insist.



来源:https://stackoverflow.com/questions/9148896/in-clojure-how-to-write-the-identity-function-using-the-anonymous-function-lite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!