Re: How get function name in body?

2021-05-04 Thread Brent Millare
That's a useful tip! Thanks

On Monday, May 3, 2021 at 11:21:18 AM UTC-4 noise...@gmail.com wrote:

> there's a handy trick for pulling in the standard repl aliases / refers:
>
> (cmd)user=> clojure.main/repl-requires
> [[clojure.repl :refer (source apropos dir pst doc find-doc)] 
> [clojure.java.javadoc :refer (javadoc)] [clojure.pprint :refer (pp pprint)]]
> (ins)user=> (ns foo.bar)
> nil
> (ins)foo.bar=> (doc fn)
> Syntax error compiling at (REPL:1:1).
> Unable to resolve symbol: doc in this context
> (ins)foo.bar=> (apply require clojure.main/repl-requires)
> nil
> (cmd)foo.bar=> (doc fn)
> -
> clojure.core/fn
>   (fn name? [params*] exprs*)
>   (fn name? ([params*] exprs*) +)
> ([& sigs])
> Special Form
> ...
>
> On Thu, Apr 29, 2021 at 10:41 PM Sean Corfield  wrote:
>
>> Sorry, it's clojure.repl/demunge so it depends on how you run your REPL 
>> and evaluate code -- in my case, clojure.repl symbols were all referred in. 
>> In your case, you'll need to require clojure.repl and refer it in:
>>
>> (require '[clojure.repl :refer [demunge])
>>
>> or make that part of your ns form as:
>>
>>   (:require [clojure.repl :refer [demunge]])
>>
>> -- 
>> Sean A Corfield -- (904) 302-SEAN
>> An Architect's View -- https://corfield.org/
>> World Singles Networks, LLC. -- https://worldsinglesnetworks.com/
>>
>> "Perfection is the enemy of the good."
>> -- Gustave Flaubert, French realist novelist (1821-1880)
>>
>> On Thu, Apr 29, 2021 at 10:27 PM damon kwok  wrote:
>>
>>>
>>> Clojure 1.10.1
>>>(defn haha []
>>>(println (demunge (.getName (.getCallerClass 
>>> (java.lang.StackWalker/getInstance 
>>> java.lang.StackWalker$Option/RETAIN_CLASS_REFERENCE))
>>> Syntax error compiling at (REPL:1:23).
>>> Unable to resolve symbol: demunge in this context
>>> 在2021年4月30日星期五 UTC+8 上午4:35:45 写道:
>>>
>>>> That's definitely nicer:
>>>>
>>>> dev=> (*defn* *add* [a b]
>>>>
>>>>  #_=>   (*println* (demunge (*.getName* (*.getCallerClass* (
>>>> *java.lang.StackWalker*/getInstance *java.lang.StackWalker$Option*
>>>> /RETAIN_CLASS_REFERENCE)
>>>>
>>>>  #_=>   (*+* a b))
>>>>
>>>> #'dev/add
>>>>
>>>> dev=> (add 1 2)
>>>>
>>>> dev/add
>>>>
>>>> 3
>>>>
>>>> dev=> 
>>>>
>>>> Thanks, Rémi!
>>>>
>>>> On Thu, Apr 29, 2021 at 12:08 PM Remi Forax  wrote:
>>>>
>>>>>
>>>>>
>>>>> --
>>>>>
>>>>> *De: *"Sean Corfield" 
>>>>> *À: *"clojure" 
>>>>> *Envoyé: *Jeudi 29 Avril 2021 01:26:34
>>>>> *Objet: *Re: How get function name in body?
>>>>>
>>>>> Consider that:
>>>>>
>>>>> (defn add [a b] (+ a b))
>>>>>
>>>>> is expanded to (something like):
>>>>>
>>>>> (def add (fn [a b] (+ a b)))
>>>>>
>>>>> So the actual code that runs is an anonymous function, which is bound 
>>>>> to the (global) var #'add -- the function itself has no name.
>>>>>
>>>>> dev=> (*macroexpand* '(*defn**add* [a b] (*+* a b)))
>>>>>
>>>>> (def add (clojure.core/fn ([a b] (+ a b
>>>>>
>>>>> dev=> (*defn**add* [a b] (*+* a b))
>>>>>
>>>>> #'dev/add
>>>>>
>>>>> dev=> add
>>>>>
>>>>> #object[dev$add 0x72f91c91 "dev$add@72f91c91"]
>>>>>
>>>>> So the value of add is an object, with type dev$add, a class that 
>>>>> implements a bunch of things including clojure.lang.IFn and 
>>>>> java.lang.Runnable and java.util.concurrent.Callable etc.
>>>>>
>>>>> The "name" of function is hard to get to, inside a function because of 
>>>>> that.
>>>>>
>>>>> One way to figure that out at runtime is to dig into the stack, which 
>>>>> you can get at by creating an exception and inspecting it: be aware that 
>>>>> this is an expensive operation!
>>>>>
>>>>> dev=> (*defn**add* [a b]
>>>>>
>>

Re: How get function name in body?

2021-05-03 Thread Justin Smith
there's a handy trick for pulling in the standard repl aliases / refers:

(cmd)user=> clojure.main/repl-requires
[[clojure.repl :refer (source apropos dir pst doc find-doc)]
[clojure.java.javadoc :refer (javadoc)] [clojure.pprint :refer (pp pprint)]]
(ins)user=> (ns foo.bar)
nil
(ins)foo.bar=> (doc fn)
Syntax error compiling at (REPL:1:1).
Unable to resolve symbol: doc in this context
(ins)foo.bar=> (apply require clojure.main/repl-requires)
nil
(cmd)foo.bar=> (doc fn)
-
clojure.core/fn
  (fn name? [params*] exprs*)
  (fn name? ([params*] exprs*) +)
([& sigs])
Special Form
...

On Thu, Apr 29, 2021 at 10:41 PM Sean Corfield  wrote:

> Sorry, it's clojure.repl/demunge so it depends on how you run your REPL
> and evaluate code -- in my case, clojure.repl symbols were all referred in.
> In your case, you'll need to require clojure.repl and refer it in:
>
> (require '[clojure.repl :refer [demunge])
>
> or make that part of your ns form as:
>
>   (:require [clojure.repl :refer [demunge]])
>
> --
> Sean A Corfield -- (904) 302-SEAN
> An Architect's View -- https://corfield.org/
> World Singles Networks, LLC. -- https://worldsinglesnetworks.com/
>
> "Perfection is the enemy of the good."
> -- Gustave Flaubert, French realist novelist (1821-1880)
>
> On Thu, Apr 29, 2021 at 10:27 PM damon kwok  wrote:
>
>>
>> Clojure 1.10.1
>>(defn haha []
>>(println (demunge (.getName (.getCallerClass
>> (java.lang.StackWalker/getInstance
>> java.lang.StackWalker$Option/RETAIN_CLASS_REFERENCE))
>> Syntax error compiling at (REPL:1:23).
>> Unable to resolve symbol: demunge in this context
>> 在2021年4月30日星期五 UTC+8 上午4:35:45 写道:
>>
>>> That's definitely nicer:
>>>
>>> dev=> (*defn* *add* [a b]
>>>
>>>  #_=>   (*println* (demunge (*.getName* (*.getCallerClass* (
>>> *java.lang.StackWalker*/getInstance *java.lang.StackWalker$Option*
>>> /RETAIN_CLASS_REFERENCE)
>>>
>>>  #_=>   (*+* a b))
>>>
>>> #'dev/add
>>>
>>> dev=> (add 1 2)
>>>
>>> dev/add
>>>
>>> 3
>>>
>>> dev=>
>>>
>>> Thanks, Rémi!
>>>
>>> On Thu, Apr 29, 2021 at 12:08 PM Remi Forax  wrote:
>>>
>>>>
>>>>
>>>> --
>>>>
>>>> *De: *"Sean Corfield" 
>>>> *À: *"clojure" 
>>>> *Envoyé: *Jeudi 29 Avril 2021 01:26:34
>>>> *Objet: *Re: How get function name in body?
>>>>
>>>> Consider that:
>>>>
>>>> (defn add [a b] (+ a b))
>>>>
>>>> is expanded to (something like):
>>>>
>>>> (def add (fn [a b] (+ a b)))
>>>>
>>>> So the actual code that runs is an anonymous function, which is bound
>>>> to the (global) var #'add -- the function itself has no name.
>>>>
>>>> dev=> (*macroexpand* '(*defn**add* [a b] (*+* a b)))
>>>>
>>>> (def add (clojure.core/fn ([a b] (+ a b
>>>>
>>>> dev=> (*defn**add* [a b] (*+* a b))
>>>>
>>>> #'dev/add
>>>>
>>>> dev=> add
>>>>
>>>> #object[dev$add 0x72f91c91 "dev$add@72f91c91"]
>>>>
>>>> So the value of add is an object, with type dev$add, a class that
>>>> implements a bunch of things including clojure.lang.IFn and
>>>> java.lang.Runnable and java.util.concurrent.Callable etc.
>>>>
>>>> The "name" of function is hard to get to, inside a function because of
>>>> that.
>>>>
>>>> One way to figure that out at runtime is to dig into the stack, which
>>>> you can get at by creating an exception and inspecting it: be aware that
>>>> this is an expensive operation!
>>>>
>>>> dev=> (*defn**add* [a b]
>>>>
>>>> #_=> (*let* [frame (*first* (*:trace* (*Throwable->map* (*ex-info**""*
>>>> {}]
>>>>
>>>> #_=> (*println* (demunge (*name* (*first* frame
>>>>
>>>> #_=> (*+* a b)))
>>>>
>>>> #'dev/add
>>>>
>>>> dev=> (add 1 2)
>>>>
>>>> dev/add
>>>>
>>>> 3
>>>>
>>>> dev=>
>>>>
>>>> demunge is what turns Clojure's generated classnames back into readable
>>>> source names.
>>&

Re: How get function name in body?

2021-04-29 Thread Sean Corfield
Sorry, it's clojure.repl/demunge so it depends on how you run your REPL and
evaluate code -- in my case, clojure.repl symbols were all referred in. In
your case, you'll need to require clojure.repl and refer it in:

(require '[clojure.repl :refer [demunge])

or make that part of your ns form as:

  (:require [clojure.repl :refer [demunge]])

-- 
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- https://corfield.org/
World Singles Networks, LLC. -- https://worldsinglesnetworks.com/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)

On Thu, Apr 29, 2021 at 10:27 PM damon kwok  wrote:

>
> Clojure 1.10.1
>(defn haha []
>(println (demunge (.getName (.getCallerClass
> (java.lang.StackWalker/getInstance
> java.lang.StackWalker$Option/RETAIN_CLASS_REFERENCE))
> Syntax error compiling at (REPL:1:23).
> Unable to resolve symbol: demunge in this context
> 在2021年4月30日星期五 UTC+8 上午4:35:45 写道:
>
>> That's definitely nicer:
>>
>> dev=> (*defn* *add* [a b]
>>
>>  #_=>   (*println* (demunge (*.getName* (*.getCallerClass* (
>> *java.lang.StackWalker*/getInstance *java.lang.StackWalker$Option*
>> /RETAIN_CLASS_REFERENCE)
>>
>>  #_=>   (*+* a b))
>>
>> #'dev/add
>>
>> dev=> (add 1 2)
>>
>> dev/add
>>
>> 3
>>
>> dev=>
>>
>> Thanks, Rémi!
>>
>> On Thu, Apr 29, 2021 at 12:08 PM Remi Forax  wrote:
>>
>>>
>>>
>>> --
>>>
>>> *De: *"Sean Corfield" 
>>> *À: *"clojure" 
>>> *Envoyé: *Jeudi 29 Avril 2021 01:26:34
>>> *Objet: *Re: How get function name in body?
>>>
>>> Consider that:
>>>
>>> (defn add [a b] (+ a b))
>>>
>>> is expanded to (something like):
>>>
>>> (def add (fn [a b] (+ a b)))
>>>
>>> So the actual code that runs is an anonymous function, which is bound to
>>> the (global) var #'add -- the function itself has no name.
>>>
>>> dev=> (*macroexpand* '(*defn**add* [a b] (*+* a b)))
>>>
>>> (def add (clojure.core/fn ([a b] (+ a b
>>>
>>> dev=> (*defn**add* [a b] (*+* a b))
>>>
>>> #'dev/add
>>>
>>> dev=> add
>>>
>>> #object[dev$add 0x72f91c91 "dev$add@72f91c91"]
>>>
>>> So the value of add is an object, with type dev$add, a class that
>>> implements a bunch of things including clojure.lang.IFn and
>>> java.lang.Runnable and java.util.concurrent.Callable etc.
>>>
>>> The "name" of function is hard to get to, inside a function because of
>>> that.
>>>
>>> One way to figure that out at runtime is to dig into the stack, which
>>> you can get at by creating an exception and inspecting it: be aware that
>>> this is an expensive operation!
>>>
>>> dev=> (*defn**add* [a b]
>>>
>>> #_=> (*let* [frame (*first* (*:trace* (*Throwable->map* (*ex-info**""*
>>> {}]
>>>
>>> #_=> (*println* (demunge (*name* (*first* frame
>>>
>>> #_=> (*+* a b)))
>>>
>>> #'dev/add
>>>
>>> dev=> (add 1 2)
>>>
>>> dev/add
>>>
>>> 3
>>>
>>> dev=>
>>>
>>> demunge is what turns Clojure's generated classnames back into readable
>>> source names.
>>>
>>>
>>> Instead of using the stacktrace of java.lang.Throwable, you can use
>>> StackWalker.getCallerClass() if you are using a jdk 9+
>>>
>>>
>>> The more important question is: why do you want the function's name
>>> inside the body?
>>> --
>>> Sean A Corfield -- (904) 302-SEAN
>>> An Architect's View -- https://corfield.org/
>>> World Singles Networks, LLC. -- https://worldsinglesnetworks.com/
>>>
>>> "Perfection is the enemy of the good."
>>> -- Gustave Flaubert, French realist novelist (1821-1880)
>>>
>>>
>>> Rémi
>>>
>>> [1]
>>> https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/lang/StackWalker.html#getCallerClass()
>>>
>>> --
>>> 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
>>> yo

Re: How get function name in body?

2021-04-29 Thread damon kwok

Clojure 1.10.1
   (defn haha []
   (println (demunge (.getName (.getCallerClass 
(java.lang.StackWalker/getInstance 
java.lang.StackWalker$Option/RETAIN_CLASS_REFERENCE))
Syntax error compiling at (REPL:1:23).
Unable to resolve symbol: demunge in this context
在2021年4月30日星期五 UTC+8 上午4:35:45 写道:

> That's definitely nicer:
>
> dev=> (*defn* *add* [a b]
>
>  #_=>   (*println* (demunge (*.getName* (*.getCallerClass* (
> *java.lang.StackWalker*/getInstance *java.lang.StackWalker$Option*
> /RETAIN_CLASS_REFERENCE)
>
>  #_=>   (*+* a b))
>
> #'dev/add
>
> dev=> (add 1 2)
>
> dev/add
>
> 3
>
> dev=> 
>
> Thanks, Rémi!
>
> On Thu, Apr 29, 2021 at 12:08 PM Remi Forax  wrote:
>
>>
>>
>> --
>>
>> *De: *"Sean Corfield" 
>> *À: *"clojure" 
>> *Envoyé: *Jeudi 29 Avril 2021 01:26:34
>> *Objet: *Re: How get function name in body?
>>
>> Consider that:
>>
>> (defn add [a b] (+ a b))
>>
>> is expanded to (something like):
>>
>> (def add (fn [a b] (+ a b)))
>>
>> So the actual code that runs is an anonymous function, which is bound to 
>> the (global) var #'add -- the function itself has no name.
>>
>> dev=> (*macroexpand* '(*defn**add* [a b] (*+* a b)))
>>
>> (def add (clojure.core/fn ([a b] (+ a b
>>
>> dev=> (*defn**add* [a b] (*+* a b))
>>
>> #'dev/add
>>
>> dev=> add
>>
>> #object[dev$add 0x72f91c91 "dev$add@72f91c91"]
>>
>> So the value of add is an object, with type dev$add, a class that 
>> implements a bunch of things including clojure.lang.IFn and 
>> java.lang.Runnable and java.util.concurrent.Callable etc.
>>
>> The "name" of function is hard to get to, inside a function because of 
>> that.
>>
>> One way to figure that out at runtime is to dig into the stack, which you 
>> can get at by creating an exception and inspecting it: be aware that this 
>> is an expensive operation!
>>
>> dev=> (*defn**add* [a b]
>>
>> #_=> (*let* [frame (*first* (*:trace* (*Throwable->map* (*ex-info**""* 
>> {}]
>>
>> #_=> (*println* (demunge (*name* (*first* frame
>>
>> #_=> (*+* a b)))
>>
>> #'dev/add
>>
>> dev=> (add 1 2)
>>
>> dev/add
>>
>> 3
>>
>> dev=>
>>
>> demunge is what turns Clojure's generated classnames back into readable 
>> source names.
>>
>>
>> Instead of using the stacktrace of java.lang.Throwable, you can use 
>> StackWalker.getCallerClass() if you are using a jdk 9+
>>
>>
>> The more important question is: why do you want the function's name 
>> inside the body?
>> -- 
>> Sean A Corfield -- (904) 302-SEAN
>> An Architect's View -- https://corfield.org/
>> World Singles Networks, LLC. -- https://worldsinglesnetworks.com/
>>
>> "Perfection is the enemy of the good."
>> -- Gustave Flaubert, French realist novelist (1821-1880)
>>
>>
>> Rémi
>>
>> [1] 
>> https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/lang/StackWalker.html#getCallerClass()
>>
>> -- 
>> 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
>> --- 
>> You received this message because you are subscribed to the Google Groups 
>> "Clojure" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to clojure+u...@googlegroups.com.
>>
> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/clojure/843762456.638578.1619723272421.JavaMail.zimbra%40u-pem.fr
>>  
>> <https://groups.google.com/d/msgid/clojure/843762456.638578.1619723272421.JavaMail.zimbra%40u-pem.fr?utm_medium=email_source=footer>
>> .
>>
>
>
> -- 
> Sean A Corfield -- (904) 302-SEAN
> An Architect's View -- https://corfield.org/
> World Singles Networks, LLC. -- https://worldsinglesnetworks.com/
>
> "Perfection is the enemy of the good."
> -- Gustave Flaubert, French realist novelist (1821-1880)
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/a3a4a5a8-18e8-4a07-b457-6ed58a75a800n%40googlegroups.com.


Re: How get function name in body?

2021-04-29 Thread Sean Corfield
That's definitely nicer:

dev=> (*defn* *add* [a b]

 #_=>   (*println* (demunge (*.getName* (*.getCallerClass* (
*java.lang.StackWalker*/getInstance *java.lang.StackWalker$Option*
/RETAIN_CLASS_REFERENCE)

 #_=>   (*+* a b))

#'dev/add

dev=> (add 1 2)

dev/add

3

dev=>

Thanks, Rémi!

On Thu, Apr 29, 2021 at 12:08 PM Remi Forax  wrote:

>
>
> --
>
> *De: *"Sean Corfield" 
> *À: *"clojure" 
> *Envoyé: *Jeudi 29 Avril 2021 01:26:34
> *Objet: *Re: How get function name in body?
>
> Consider that:
>
> (defn add [a b] (+ a b))
>
> is expanded to (something like):
>
> (def add (fn [a b] (+ a b)))
>
> So the actual code that runs is an anonymous function, which is bound to
> the (global) var #'add -- the function itself has no name.
>
> dev=> (*macroexpand* '(*defn**add* [a b] (*+* a b)))
>
> (def add (clojure.core/fn ([a b] (+ a b
>
> dev=> (*defn**add* [a b] (*+* a b))
>
> #'dev/add
>
> dev=> add
>
> #object[dev$add 0x72f91c91 "dev$add@72f91c91"]
>
> So the value of add is an object, with type dev$add, a class that
> implements a bunch of things including clojure.lang.IFn and
> java.lang.Runnable and java.util.concurrent.Callable etc.
>
> The "name" of function is hard to get to, inside a function because of
> that.
>
> One way to figure that out at runtime is to dig into the stack, which you
> can get at by creating an exception and inspecting it: be aware that this
> is an expensive operation!
>
> dev=> (*defn**add* [a b]
>
> #_=> (*let* [frame (*first* (*:trace* (*Throwable->map* (*ex-info**""*
> {}]
>
> #_=> (*println* (demunge (*name* (*first* frame
>
> #_=> (*+* a b)))
>
> #'dev/add
>
> dev=> (add 1 2)
>
> dev/add
>
> 3
>
> dev=>
>
> demunge is what turns Clojure's generated classnames back into readable
> source names.
>
>
> Instead of using the stacktrace of java.lang.Throwable, you can use
> StackWalker.getCallerClass() if you are using a jdk 9+
>
>
> The more important question is: why do you want the function's name inside
> the body?
> --
> Sean A Corfield -- (904) 302-SEAN
> An Architect's View -- https://corfield.org/
> World Singles Networks, LLC. -- https://worldsinglesnetworks.com/
>
> "Perfection is the enemy of the good."
> -- Gustave Flaubert, French realist novelist (1821-1880)
>
>
> Rémi
>
> [1]
> https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/lang/StackWalker.html#getCallerClass()
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
> 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 unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/clojure/843762456.638578.1619723272421.JavaMail.zimbra%40u-pem.fr
> <https://groups.google.com/d/msgid/clojure/843762456.638578.1619723272421.JavaMail.zimbra%40u-pem.fr?utm_medium=email_source=footer>
> .
>


-- 
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- https://corfield.org/
World Singles Networks, LLC. -- https://worldsinglesnetworks.com/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/CAD4thx8qUDQZB%2B9Qbb0RXRYaAV7Q2U_1tExC%2BCjC47RAHgYviA%40mail.gmail.com.


Re: How get function name in body?

2021-04-29 Thread Remi Forax
> De: "Sean Corfield" 
> À: "clojure" 
> Envoyé: Jeudi 29 Avril 2021 01:26:34
> Objet: Re: How get function name in body?

> Consider that:

> (defn add [a b] (+ a b))

> is expanded to (something like):

> (def add (fn [a b] (+ a b)))

> So the actual code that runs is an anonymous function, which is bound to the
> (global) var #'add -- the function itself has no name.

> dev=> ( macroexpand '( defn add [a b] ( + a b)))

> (def add (clojure.core/fn ([a b] (+ a b

> dev=> ( defn add [a b] ( + a b))

> #'dev/add

> dev=> add

> #object[dev$add 0x72f91c91 "dev$add@72f91c91"]

> So the value of add is an object, with type dev$add, a class that implements a
> bunch of things including clojure.lang.IFn and java.lang.Runnable and
> java.util.concurrent.Callable etc.

> The "name" of function is hard to get to, inside a function because of that.

> One way to figure that out at runtime is to dig into the stack, which you can
> get at by creating an exception and inspecting it: be aware that this is an
> expensive operation!

> dev=> ( defn add [a b]

> #_=> ( let [frame ( first ( :trace ( Throwable->map ( ex-info "" {}]

> #_=> ( println (demunge ( name ( first frame

> #_=> ( + a b)))

> #'dev/add

> dev=> (add 1 2)

> dev/add

> 3

> dev=>

> demunge is what turns Clojure's generated classnames back into readable source
> names.

Instead of using the stacktrace of java.lang.Throwable, you can use 
StackWalker.getCallerClass() if you are using a jdk 9+ 

> The more important question is: why do you want the function's name inside the
> body?
> --
> Sean A Corfield -- (904) 302-SEAN
> An Architect's View -- [ https://corfield.org/ | https://corfield.org/ ]
> World Singles Networks, LLC. -- [ https://worldsinglesnetworks.com/ |
> https://worldsinglesnetworks.com/ ]

> "Perfection is the enemy of the good."
> -- Gustave Flaubert, French realist novelist (1821-1880)

Rémi 

[1] [ 
https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/lang/StackWalker.html#getCallerClass()
 | 
https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/lang/StackWalker.html#getCallerClass()
 ] 

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/843762456.638578.1619723272421.JavaMail.zimbra%40u-pem.fr.


Re: How get function name in body?

2021-04-28 Thread Sean Corfield
Consider that:

(defn add [a b] (+ a b))

is expanded to (something like):

(def add (fn [a b] (+ a b)))

So the actual code that runs is an anonymous function, which is bound to
the (global) var #'add -- the function itself has no name.

dev=> (*macroexpand* '(*defn* *add* [a b] (*+* a b)))

(def add (clojure.core/fn ([a b] (+ a b

dev=> (*defn* *add* [a b] (*+* a b))

#'dev/add

dev=> add

#object[dev$add 0x72f91c91 "dev$add@72f91c91"]

So the value of add is an object, with type dev$add, a class that
implements a bunch of things including clojure.lang.IFn and
java.lang.Runnable and java.util.concurrent.Callable etc.

The "name" of function is hard to get to, inside a function because of that.

One way to figure that out at runtime is to dig into the stack, which you
can get at by creating an exception and inspecting it: be aware that this
is an expensive operation!

dev=> (*defn* *add* [a b]

 #_=>   (*let* [frame (*first* (*:trace* (*Throwable->map* (*ex-info* *""*
{}]

 #_=> (*println* (demunge (*name* (*first* frame

 #_=> (*+* a b)))

#'dev/add

dev=> (add 1 2)

dev/add

3

dev=>

demunge is what turns Clojure's generated classnames back into readable
source names.

The more important question is: why do you want the function's name inside
the body?
-- 
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- https://corfield.org/
World Singles Networks, LLC. -- https://worldsinglesnetworks.com/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)
On Wed, Apr 28, 2021 at 6:35 AM damon kwok  wrote:

>
> How get function name in body?
>
> (defn add [a b]
>  ;; how get "add" here?
>   (+ a b))
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
> 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 unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/clojure/5c533ccf-ec0b-4c84-b759-851f1fe3b3can%40googlegroups.com
> <https://groups.google.com/d/msgid/clojure/5c533ccf-ec0b-4c84-b759-851f1fe3b3can%40googlegroups.com?utm_medium=email_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/CAD4thx93gtbitV20HNk9XBGF%2BkoDyCLYadNp32dvrOSQdpRN0w%40mail.gmail.com.


Re: How get function name in body?

2021-04-28 Thread Blake Watson
Well, it seems like it should be possible. At the end of the defn macro,
metadata is attached to a function, so you can see the name with:

```
(meta #'my.ns/add)
```

And you could do this inside the function:

```
(defn add [a b]
  (let [name (:name (meta #'add))]
(str a b name)))
```

But that can't be what you want. The thing is, the actual function body
doesn't know that it's being bound to the symbol "add" and making it aware
would seem to contravene a number of league bylaws.

That is to say, the macro "defn" attaches the meta-information to the
symbol #add, not to the code that follows.

You could certainly make your own macro to make the bound variable name
visible to the body of the code, but I'm not sure you can do that with the
built-in defn.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/CAJAnwPmWK82nS1udL7MvVeDpa1rQHQ3c34G37WWasm%3D7DieroA%40mail.gmail.com.


How get function name in body?

2021-04-28 Thread damon kwok

How get function name in body?

(defn add [a b]
 ;; how get "add" here?
  (+ a b))

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/5c533ccf-ec0b-4c84-b759-851f1fe3b3can%40googlegroups.com.


Re: get function

2016-09-13 Thread Sean Corfield
On 9/13/16, 5:02 AM, "Erik Assum"  wrote:
> you’ll see that `get` accepts an additional argument, the default value, 
> which e.g. the keyword does not.

Just FYI, both the keyword first form and the map first form do accept an 
additional default argument:


boot.user> (:b {:a 1 :b 2} 3)
2
boot.user> (:b {:a 1 :c 3} 2)
2
boot.user> ({:a 1 :b 2} :b 3)
2
boot.user> ({:a 1 :c 3} :b 2)
2
boot.user>

Sean Corfield -- (970) FOR-SEAN -- (904) 302-SEAN
An Architect's View -- http://corfield.org/

"If you're not annoying somebody, you're not really alive."
-- Margaret Atwood



-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: get function

2016-09-13 Thread Stuart Sierra
The `get` function will accept `nil` as the map argument and return `nil`, 
but invoking `nil` will throw a NullPointerException.

By convention, people use `get` when they don't know what the map argument 
is, since it might be nil. And we use keywords as functions when they are 
literals, like (:foo m).

(let [m (...something that might return a map or nil...)]
  (get m "some key"))  ; returns nil or value

(let [m (...something that might return a map or nil...)]
  (m "some key"))  ; might throw exception

All forms of map lookup support a default argument if the key is not found:

user=> (let [m {:a 1 :b 2}] (m :c "not found"))
"not found"
user=> (let [m {:a 1 :b 2}] (:c m "not found"))
"not found"
user=> (let [m {:a 1 :b 2}] (get m :c "not found"))
"not found"

–S


On Tuesday, September 13, 2016 at 7:58:14 AM UTC-4, Rickesh Bedia wrote:
>
> Hi everyone,
>
> I am new to Clojure and have been playing around with it for a little 
> while. I have seen that (get [3 2 1] 0) and ([[3 2 1] 0) both return the 
> value 3. Similarly (get {:a 0 :b 1} :b) and ({:a 0 :b 1} :b) return 1.
>
> I was wondering if anyone could explain why the get function is useful or 
> maybe an example?
>
> Apologies in advance if this question is due to ignorance and I haven't 
> reached the level where this function is used.
>
> Thanks in advance
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: get function

2016-09-13 Thread Erik Assum
From 
https://clojuredocs.org/clojure.core/get

you’ll see that `get` accepts an additional argument, the default value, which 
e.g. the keyword does not.

As for getting stuff out of maps, the idiomatic way is
(def m {:a 1 :b 2})
(:b m)
;; => 2

There is otherwise a nice discussion about the finer points of this here:
http://stackoverflow.com/questions/7034803/idiomatic-clojure-map-lookup-by-keyword

Erik.

> On 13 Sep 2016, at 11:49, 'Rickesh Bedia' via Clojure 
> <clojure@googlegroups.com> wrote:
> 
> Hi everyone,
> 
> I am new to Clojure and have been playing around with it for a little while. 
> I have seen that (get [3 2 1] 0) and ([[3 2 1] 0) both return the value 3. 
> Similarly (get {:a 0 :b 1} :b) and ({:a 0 :b 1} :b) return 1.
> 
> I was wondering if anyone could explain why the get function is useful or 
> maybe an example?
> 
> Apologies in advance if this question is due to ignorance and I haven't 
> reached the level where this function is used.
> 
> Thanks in advance

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


get function

2016-09-13 Thread 'Rickesh Bedia' via Clojure
Hi everyone,

I am new to Clojure and have been playing around with it for a little 
while. I have seen that (get [3 2 1] 0) and ([[3 2 1] 0) both return the 
value 3. Similarly (get {:a 0 :b 1} :b) and ({:a 0 :b 1} :b) return 1.

I was wondering if anyone could explain why the get function is useful or 
maybe an example?

Apologies in advance if this question is due to ignorance and I haven't 
reached the level where this function is used.

Thanks in advance

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.