anonymous functions with names

111 views
Skip to first unread message

Erlis Vidal

unread,
Aug 31, 2012, 11:52:23 AM8/31/12
to clo...@googlegroups.com
Hi guys, 

I've been reading but I'm still confused about the difference between an anonymous function with name vs a defn function

(fn my-func1[x] x)

(defn my-func2[x] x)

Thanks, 
Erlis 

Baishampayan Ghose

unread,
Aug 31, 2012, 11:54:42 AM8/31/12
to clo...@googlegroups.com

In case of a named anonymous function the name is only bound in the lexical scope of the anonymous function being defined.

This is useful when defining recursive anonymous functions.

Sent from phone. Please excuse brevity.

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Tamreen Khan

unread,
Aug 31, 2012, 11:57:51 AM8/31/12
to clo...@googlegroups.com
The name given an anonymous function can only be used within the scope of that function.

This will work:

(fn my-func1 [x] (my-func x)) ; Leads to infinite recursion, of course

This won't work because my-func1 is called outside of the function's lexical scope:

(fn my-func1 [x] x)
(my-func1 100)

However, functions defined with defn use a global var, so they're not limited to any particular lexical scope.

Erlis 

--

Erlis Vidal

unread,
Aug 31, 2012, 12:56:38 PM8/31/12
to clo...@googlegroups.com
Thank you guys!!

On Fri, Aug 31, 2012 at 11:59 AM, Jim - FooBar(); <jimpi...@gmail.com> wrote:
defn will create a global var while fn won't...it is only visible in the scope it was originally defined...remember you can always do:

(def f (fn [x] ...)) which is he same as defn

Jim
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to

For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to

dmirylenka

unread,
Sep 3, 2012, 5:30:21 AM9/3/12
to clo...@googlegroups.com, er...@erlisvidal.com
Just 2 cents:

A name you give to the anonymous function also appears in the stack traces instead of the things like fn_123_4532,
which is very convenient for debugging.

Stathis Sideris

unread,
Sep 3, 2012, 5:36:20 AM9/3/12
to clo...@googlegroups.com, er...@erlisvidal.com
Thanks, this is a *very* useful bit of information.

Mayank Jain

unread,
Sep 3, 2012, 5:52:23 AM9/3/12
to clo...@googlegroups.com, er...@erlisvidal.com
@dmirylenka
Thanks. That is useful to know.

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
Reply all
Reply to author
Forward
0 new messages