I think your v2 should be a btree so your O is logarithmic not n*n
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.
ah, smart trick, thanks!
It might have an adverse affect on performance with large volumes of data, so good to check this
depending on what you need to do, you can do metadata extension on a particular instance (but the protocol has to support it)
https://clojure.org/reference/protocols#_extend_via_metadata
apply
map
filter
reduce
what other functions do you use daily that might not be so well-known?
tryna expand on my general purpose toolkit and knowledge
keep remove some every? sort-by group-by select-keys keys vals
Iād say I use these ones very frequently
Some others that come to mind are mapcat concat reverse map-indexed partition-by for doseq reduce-kv
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 <http://System.in|System.in>
using java interopHow 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 @smith.adriane
Work with stdin from the repl usually is not a good experience.
You can use (require '[<http://clojure.java.io|clojure.java.io> :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 @souenzzo
list*
is super helpful in combination with apply
group-by
is sometimes just the right tool for the job