Fork me on GitHub

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?


You can use both defn and memoize:

❤️ 2

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


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

👍 2
ahungry16:03:56 is a package i did with aop - you could look and see how i approached the aop

👍 1

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


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 which provides a lot of flexibility around memoization strategies.


any hints on how to override formatting when using 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

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.


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


Ah thank you, that is great info!


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

borkdude19:03:56 is a thin library on top of that for dealing with EDN using some convenience functions

nice 1
gratitude-thank-you 1