Fork me on GitHub
#beginners
<
2022-10-14
>
Banseok Yeo09:10:38

Hello, How can I implement an async side effect as a pure function in Clojure? (Like wrapping async side effects in monad in other functional languages)

delaguardo09:10:02

there are some libraries for that for example - https://github.com/adambard/failjure

πŸ‘ 1
valerauko11:10:45

in clojurescript my favorite take on this is how re-frame encourages you to have pure event/effect handlers

πŸ‘ 1
Chester Bushman15:10:51

πŸ‘‹ Hello, team!

πŸ‘‹ 6
skylize16:10:07

What are some reasons why I might ever want to use intern?

andy.fingerhut16:10:42

Sorry this does not fully answer your question (others might have use cases they can supply), but in most Clojure application code, no reason at all.

βž• 1
andy.fingerhut16:10:21

If you are adding features to Clojure itself, and/or developing IDE kinds of tools, or Clojure debug utilities, maybe then it might come in handy sometimes.

delaguardo17:10:54

when you don’t control a namespace but have to modify its behaviour

πŸ‘ 1
hiredman17:10:37

in our braintree code at work we have

(doseq [^java.lang.reflect.Field field (.getDeclaredFields Transaction$Status)
        :when (not= "$VALUES" (.getName field))
        :let [value (.get field nil)
              predicate-name (symbol (str "transaction-" (.replaceAll (.toLowerCase (.getName field)) "_" "-")
                                          "?"))]]
  (intern *ns* predicate-name (fn [^Transaction braintree-transaction]
                                (= value (.getStatus braintree-transaction)))))
which generates predicates for all the transaction statuses

🍰 1
skylize17:10:01

:thinking_face: So, I guess it's letting you dynamically create a var name from a value?

hiredman17:10:28

dynamically is a little vague, it is a top level form in a file, so it is running at code load time

hiredman17:10:34

we also have a more complicated case that generates tests (programatically making the same things deftest makes, which interning is part of) for different possible states of a google play subscription. I dont entirely recall why that makes different tests instead of doing a doseq in a single test, but I think it was to take advantage of existing fixtures that reset shared state between tests

skylize17:10:18

Well, I guess I just mean that (def some-name some-value) doesn't really offer a way for some-name to be anything other than literally some-name, while by interning you can generate the name and fill it in.

skylize17:10:18

Doesn't seem like the linter would be very happy about this though. Are you able to get clj-kondo to recognize the new symbols when you use them?

hiredman17:10:47

oh, no, linters hate this sort of thing

pppaul18:10:29

if you would use a macro to write a def, you can use intern instead. sometimes one is better than the other (a macro has limitations that functions do not)

πŸ™Œ 1
seancorfield05:10:09

I use intern in expectations.clojure.test to "import" several Vars from clojure.test into Expectations, preserving metadata etc (with Imported from clojure.test appended to the docstring!), so that users of Expectations don't need to require clojure.test to get at those useful functions: https://github.com/clojure-expectations/clojure-test/blob/develop/src/expectations/clojure/test.cljc#L540-L566

metal 1