Fork me on GitHub
#clojure
<
2023-08-13
>
teodorlu12:08:44

Hi! is there a clojure core predicate for whether something is an immutable, serializable value? Should return true for maps, numbers, keywords, symbols, vectors, and should return false for channels, database connections, files.

igrishaev12:08:11

No there is no such a function. Technically it's possible to have a Clojure map that holds a java HashMap inside which is mutable. You can create a protocol IMutable with a single is-mutable? method and extend it for Keyword, Symbol, PersistentVector and so on. And for Object (default), the result would be false.

👍 4
Reut Sharabani12:08:08

Maybe you can (attempt to) read it as an edn literal? Not sure if that's what you need though.

teodorlu13:08:16

> Maybe you can (attempt to) read it as an edn literal? Not sure if that’s what you need though. That gives me a performance penalty I’d like to avoid. I want to keep only serializable, immutable values from a HTTP request map. I thought I could use a https://clojuredocs.org/clojure.walk (probably clojure.walk/postwalk) to achieve that, but then I’d need a predicate function for which values to keep! I was hoping for a fast whitelist of values—I’m hesitant to try to serialize and deserialize each value. Though it does sound like a fun exercise :thinking_face:

teodorlu13:08:26

@U7KDU667Q decided to give your idea a shot, “why optimize before we know we need to?“. Seems to work kind of nice! Though I suspect it won’t perform super well.

🚀 2
Reut Sharabani13:08:41

This is just off the top of my head, need to think about edge cases :) I'm not signing anything!

🙏 2