Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
521 views
in Technique[技术] by (71.8m points)

clojure - Anonymous function shorthand

There's something I don't understand about anonymous functions using the short notation #(..)

The following works:

REPL>  ((fn [s] s) "Eh")
"Eh"

But this doesn't:

REPL>  (#(%) "Eh")

This works:

REPL> (#(str %) "Eh")
"Eh"

What I don't understand is why (#(%) "Eh") doesn't work and at the same time I don't need to use str in ((fn [s] s) "Eh")

They're both anonymous functions and they both take, here, one parameter. Why does the shorthand notation need a function while the other notation doesn't?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
#(...)

is shorthand for

(fn [arg1 arg2 ...] (...))

(where the number of argN depends on how many %N you have in the body). So when you write:

#(%)

it's translated to:

(fn [arg1] (arg1))

Notice that this is different from your first anonymous function, which is like:

(fn [arg1] arg1)

Your version returns arg1 as a value, the version that comes from expanding the shorthand tries to call it as a function. You get an error because a string is not a valid function.

Since the shorthand supplies a set of parentheses around the body, it can only be used to execute a single function call or special form.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...