Fork me on GitHub
#babashka
<
2022-11-22
>
borkdude13:11:10

You now have the unique chance to be in a #babashka workshop given by @rahul080327!

Daniel Gerson17:11:52

Question: I've never seen the notation of this function as part of the clojure language with accept as the first symbol before the argument vector. https://github.com/babashka/fs/blob/master/test/babashka/fs_test.clj#L212 . Is it just ignored or does it have some significance? :thinking_face: Scanning https://clojure.org/guides/learn/functions I don't see it. So curious.

enn17:11:42

In JVM Clojure, if provided, it’s used in the generated class name, so it is helpful to disambiguate between anonymous functions when reading stacktraces:

dev> (fn [])
#function[dev/eval325607/fn--325608]
dev> (fn my-cool-fn [])
#function[dev/eval325611/my-cool-fn--325612]
Not sure how that works in Babashka.

Daniel Gerson17:11:26

Thanks! Wish it was on the Clojure functions page.

skylize18:11:52

Also provides for self-reference without recur if your recursion is already stack-safe.

(let [foo (fn bar [x]
            (if (> x 1) x (bar (inc x))))]
  (foo 0))
; => 2

Alex Miller (Clojure team)18:11:23

that's rarely a good usage - here you could safely recur instead. and let-fn is sufficient for mutually recursive local functions. really the primary reason to name your fn is to share intent and to aid debuggability

skylize18:11:39

Yes. The example is terrible use case, which is definitely not stack safe. It was just a quick & dirty demonstration of the capacity to call the name recursively. I have pretty mixed feelings about let-fn. In addition to it's lack of symmetry with the syntax of let, it can get verbose quickly if you also want non-function let bindings. If I don't specifically need mutual recursion, I am personally inclined to avoid it.

Bob B19:11:18

regarding the docs themselves, as you mentioned the optional name is not covered in the "learn Clojure" page (which I assume is because it's not typicaly heavily used to get started), but it is covered in the reference: <https://clojure.org/reference/special_forms#fn>

👍 1
Alex Miller (Clojure team)19:11:12

I actually think it's kind of accidental that it does work, which may be why it was not part of the original docs

👀 1