Fork me on GitHub
#clojure
<
2022-03-05
>
SK11:03:45

hi! I have a function that takes a long time and I'd like to cache the results. What's the best way to do that? I know memoize, but I don't want to now redefine all my functions as a def instead of defn.. is there some macro or something that does caching in an AOP-style way, when I don't need to explicitly rewrite my functions to memorize or use caches?

p-himik11:03:39

You can use both defn and memoize: https://stackoverflow.com/a/53649838/564509

❤️ 2
SK11:03:01

Nice! does it also work with multi-arity functions?

p-himik11:03:24

It should - nothing specific about multi-arity functions when it comes to either memoize or vars.

👍 2
ahungry16:03:56

https://github.com/ahungry/determinism is a package i did with aop - you could look and see how i approached the aop

👍 1
ahungry16:03:21

could use it to wrap all fns in a namespace with memoize

seancorfield00:03:39

If you want more than a "cache forever" memoize, or you want to cache based on a subset of your function arguments instead of all of them (or compute some cache key from those arguments), take a look at https://github.com/clojure/core.memoize which provides a lot of flexibility around memoization strategies.

SK16:03:13

any hints on how to override formatting when using clojure.data.json for converting clojure structures into json structures using write-string? Specifically I'd like to override the formatting of double values so they are rounded (eg 1.99999999 gets turned into 2).. should I extend the protocol for floats and try to somehow override the built-in protocol method?

Alex Miller (Clojure team)17:03:14

write and write-str have an options map that takes a :value-fn function - that could check for double/float and format in whatever way you want

👍 1
jmv17:03:47

Is there a way to read an edn file with comments attached as metadata? Basically with the comment attached the appropriate node in the edn structure.

ghadi18:03:48

metadata is not part of EDN, but is part of the Clojure reader. However, the edn reader inside Clojure (clojure.edn namespace) supports reading metadata

jmv19:03:07

Ah thank you, that is great info!

borkdude19:03:20

@UDXEK491P if you would like to read comments from .edn files, I recommend using #rewrite-clj

borkdude19:03:56

https://github.com/borkdude/rewrite-edn is a thin library on top of that for dealing with EDN using some convenience functions

nice 1
gratitude-thank-you 1