This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-06-12
Channels
- # announcements (9)
- # babashka (2)
- # beginners (27)
- # clojure (19)
- # clojure-bay-area (1)
- # clojure-germany (7)
- # clojure-nl (1)
- # clojure-norway (1)
- # clojurescript (6)
- # conjure (1)
- # datomic (10)
- # fulcro (11)
- # graalvm (1)
- # introduce-yourself (3)
- # jobs (1)
- # luminus (1)
- # malli (9)
- # off-topic (23)
- # podcasts-discuss (3)
- # polylith (6)
- # practicalli (1)
- # re-frame (14)
- # sci (2)
- # shadow-cljs (5)
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?
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)
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
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 interopAre 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))))
Work with stdin from the repl usually is not a good experience.
You can use (require '[
and (def input (io/reader "file.json"))
Then you can swap from "file.json"
to *in*
I'll try this approach too. Thanks @U2J4FRT2T