Fork me on GitHub

Hi, is there a way to overwrite a protocol implementation for some type in a thread-local way (or for some scope would be even better). Concretely, I'm using mongo-driver-3 to store clojure data in mongo, and for the specific data structure I need to encode the namespace of keywords in the field name. But I'd rather not do that for everything, since it would be very unpredictable for other libraries/code.


@mathias_dw What protocol would you override to accomplish that?


I guess you could also pre-process the data yourself (stringify the keys that is)


the serialization one in mongo-driver-3


Yeah, I can definitely do it myself, although taking care of all the nesting feels like duplication of the serialization code. But I was mainly wondering if such a "local extends" exists


I'm not aware of this. But what you could do is override the protocol with an implementation and use a dynamic var to control the namespacing, defaulting to the previous behavior.

👍 3

ah, smart trick, thanks!


It might have an adverse affect on performance with large volumes of data, so good to check this

Alex Miller (Clojure team)14:06:28

depending on what you need to do, you can do metadata extension on a particular instance (but the protocol has to support it)


apply map filter reduce what other functions do you use daily that might not be so well-known?


Some others that come to mind are mapcat concat reverse map-indexed partition-by for doseq reduce-kv


list* is super helpful in combination with apply


group-by is sometimes just the right tool for the job


clojure.set/map-invert is useful when using a hash map as a dictionary and wish to translate in both directions, e.g. encode/decode. for is often used to generate hiccup for web pages, avoiding a lot of repetitive code. cycle and rand-nth to generate repeating sequences and random selection, e.g for creating a carousel of video thumbnails on the London Clojurians website. I've been using a lot of conditional functions recently, and or if when cond Also using :`clojure-keywords` and {:hash "maps"} as functions to extract data. Hopefully those are fairly well known though 😉


tryna expand on my general purpose toolkit and knowledge


keep remove some every? sort-by group-by select-keys keys vals

mario-star 2

I’d say I use these ones very frequently


Hi guys, I've just started to learn Clojure and I have a newbie question. How to read a file from stdin? I'd like to know the best way to do that in Clojure. It would be great to do something like that in command line: my-app < file.json Could someone help me with that ?


This should work:

(def input-string (slurp *in*))
You can also use use the input stream from using java interop


How can use this approach in a main function for instance?


Are you wanting to provide the input file as an argument on the command line, or wanting to dynamically grab some keystrokes from the REPL?


(or perhaps something else?)


Here's an example. You can just use a let

(defn -main [& args]
  (let [input-string (slurp *in*)]
    (println "character count:" (count input-string))))


Oh that works. Thanks @U7RJTCH6J

👍 2

Work with stdin from the repl usually is not a good experience. You can use (require '[ :as io]) and (def input (io/reader "file.json")) Then you can swap from "file.json" to *in*


I'll try this approach too. Thanks @U2J4FRT2T