Fork me on GitHub
#clojure
<
2019-02-16
>
superancetre01:02:55

Hi, is there a convention for writing docstring for a wrapper function wrapping a Java library?

superancetre01:02:27

Like should I use the Javadoc in the docstring, link to it? Any ideas?

seancorfield03:02:32

@julien.rouse No conventions. There's no formatting associated with Clojure docstrings. They're usually just short statements of what a function does, occasionally with examples of usage. There are some documentation tools that will render Markdown in a Clojure docstring...

👍 5
superancetre16:02:42

Thanks again Sean!

vemv05:02:37

Looking for a function f that makes the example = true...

> 'component/start
component/start
> `component/start
com.stuartsierra.component/start
> (let [s 'component/start] (= (f s) `component/start)) ;; <--- looking for an f

vemv05:02:22

(resolve 'component/start) is nil (in any case I want a symbol, not its resolved value)

vemv05:02:40

got it (symbol (ns-resolve *ns* 'component/stop))

vemv05:02:04

thanks for the hint!

seancorfield05:02:14

You need the alias in scope to resolve the symbol:

user=> (require '[com.stuartsierra.component :as component])
user=> (->> (resolve 'component/start) meta ((juxt (comp str :ns) :name)) (map name) (apply symbol))
com.stuartsierra.component/start
user=>   

seancorfield05:02:11

Depends on the context you're trying to resolve symbols in I guess...

seancorfield05:02:05

Interesting shortcut... FWIW, (resolve 'component/start) produces a Var for me: #'com.stuartsierra.component/start -- not sure why you didn't get that @vemv

vemv05:02:48

> (resolve 'component/start) is nil I probably said (and eval'ed) that wrongly. getting the var indeed

vemv05:02:28

so yeah (symbol (resolve 'component/stop)) is the shortest way

seancorfield05:02:58

Oh, I should have tried that first... that's a recent addition that you can go from a namespace object to a symbol I think...

seancorfield05:02:30

Yeah, new in 1.10

seancorfield05:02:28

seanc@DESKTOP-QU2UJ1N:~/clojure$ clj -Sdeps '{:deps {:com.stuartsierra/component {:mvn/version "RELEASE"}}}' -A1.9
Clojure 1.9.0
user=> (require '[com.stuartsierra.component :as component])
nil
user=> (resolve 'component/start)
#'com.stuartsierra.component/start
user=> (symbol *1)
ClassCastException clojure.lang.Var cannot be cast to java.base/java.lang.String  clojure.core/symbol (core.clj:579)
user=>
seanc@DESKTOP-QU2UJ1N:~/clojure$ clj -Sdeps '{:deps {:com.stuartsierra/component {:mvn/version "RELEASE"}}}' -A1.10
Clojure 1.10.0
user=> (require '[com.stuartsierra.component :as component])
nil
user=> (resolve 'component/start)
#'com.stuartsierra.component/start
user=> (symbol *1)
com.stuartsierra.component/start
user=>           

vemv05:02:28

nicely 'scienced'! and good to know 🙂

madstap20:02:40

Is there a library like chime, but for java-time instead of clj-time/joda-time?

lilactown21:02:40

I want to add a custom Datafiable implementation to a function created with defn

lilactown21:02:01

I tried doing (alter-meta! #'my-fn-name assoc 'clojure.core.protocols/Datafiable my-datafy-impl)

lilactown21:02:19

doesn’t seem to work

seancorfield21:02:47

The metadata needs to be keyed on the function name, not the protocol type.

seancorfield21:02:10

(alter-meta! #'my-fn-name assoc 'clojure.core.protocols/datafy my-datafy-impl)

lilactown21:02:13

yep just realized

seancorfield21:02:25

I got that wrong too when I first tried it at Conj 🙂

lilactown21:02:06

OK, so that answers one question. the next is: is there a way to attach it to the function itself without rewriting defn? 😬

seancorfield21:02:42

I'm not sure what you're trying to do here... why do you want a function to be datafiable?

lilactown21:02:45

because now, (datafy #'my-fn-name) works but (datafy my-fn-name) obviously does not, since the meta is on the var not the fn

lilactown21:02:53

reasons 😛

seancorfield21:02:53

You could alter-var-root to put in a new definition with metadata (wrapped on the function value, rather the Var) maybe?

lilactown21:02:52

I lose some other nice properties if I do that it looks like

lilactown21:02:24

like the REPL prints it as “clojure.lang.AFunction” instead of the var name

seancorfield21:02:47

Oh, because you no longer have access to the Var metadata itself?

hmaurer23:02:26

is there an idiomatic way in clojure to check if a list has one and only one item? Other than (= (count my-list) 1)

Jan K23:02:23

Could also use (= 1 (bounded-count 2 my-list)) to avoid fully realizing large lazy sequences 🙂

👍 20
hmaurer23:02:40

@UCPGSBNQ4 ouh; good call; thanks!