Fork me on GitHub
#beginners
<
2021-06-12
>
mathias_dw09:06:48

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.

borkdude14:06:55

@mathias_dw What protocol would you override to accomplish that?

borkdude14:06:15

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

mathias_dw14:06:18

the serialization one in mongo-driver-3

mathias_dw14:06:08

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

borkdude14:06:06

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
mathias_dw14:06:28

ah, smart trick, thanks!

borkdude14:06:32

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)

sova-soars-the-sora17:06:17

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

Lu17:06:12

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

delaguardo19:06:54

list* is super helpful in combination with apply

andy.fingerhut21:06:56

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

practicalli-johnny06:06:29

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 😉

sova-soars-the-sora17:06:32

tryna expand on my general purpose toolkit and knowledge

Lu17:06:39

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

mario-star 2
Lu17:06:17

I’d say I use these ones very frequently

wcf17:06:44

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 ?

phronmophobic18:06:00

This should work:

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

wcf18:06:46

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

sova-soars-the-sora18:06:04

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?

sova-soars-the-sora18:06:13

(or perhaps something else?)

phronmophobic18:06:48

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

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

wcf18:06:03

Oh that works. Thanks @U7RJTCH6J

👍 2
souenzzo18:06:36

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*

wcf18:06:33

I'll try this approach too. Thanks @U2J4FRT2T