Fork me on GitHub

how to navigate clojure code in emacs? cider ? or there maybe some other method


@haiyuan.vinurs haven't completely got a question.


@haiyuan.vinurs emacs and install cider that should be enough to get you going


with cider that i must start a repl, is there a tool like global gtags that can parse the code statically


my normal workflow with cider is: open clojure project, start cider repl with :cider-jack-in, start to eval my code with :cider-eval-defun-at-point (mapped to ,ef hehe), finish the cider repl with C-c C-q


@haiyuan.vinurs Clojure really expects to have a live REPL for all your workflows. Check out Stu Halloway's talk on REPL-Driven Development, and another one called Running With Scissors. Without a REPL, you'll struggle to work with Clojure. Like @UC1FADN3T says: start a REPL at the beginning of your work and keep it open until you're done. I tend to keep my REPL running for days and days.


I'm confused, how does (reduce conj () [1 2 3 4]) reverse the sequence? (yes, walking through 4clojure problems!)


it's the reduce part that confused me, not the conj


(conj (list 1) 2) in your repl


and then (conj (conj (list 1) 2) 3) in your repl


the tricky thing is that (conj '(1) 2) appends while (conj [1] 2) prepends


conj has performance guarantees not location guarantees. Vectors are easy to append to. lists are easy to prepend


Ahh, I see, thank you Dan and Will. Both very insightful.


I will ponder (hammock?) the code further 🙂


@dharrigan you can also replace the reduce with reductions to get an idea of what is happening at each step of the reduce.


that's handy to know! thank you kindly!

Idan Melamed16:05:12

I was confused about that one until I realized that the () was just the initial value for the conj... Realizing that (reduce conj () [1 2 3 4]) is the same as (reduce conj [() 1 2 3 4]) made it clearer for me

Alex Miller (Clojure team)16:05:56

often if a reduce is confusing you can s/reduce/reductions/ to get some clarity - (reductions conj () [1 2 3 4])

Alex Miller (Clojure team)16:05:12

shows you all the intermediate accumulator values

👍 4

That's a great tip!

Kari Marttila18:05:55

I spent a nice debugging session with cognitect's AWS library wondering why its profile-credentials-provider cannot find my profile. Heh, a rookie mistake:

:aws-profile local-dynamodb
=> type: symbol (should be string, i.e.:)
:aws-profile "local-dynamodb"
But I got a chance to try Cursive debugger which was actually great and in the debugger I pretty soon figured out that
(get (config/parse f) profile-name
was returning nothing and therefore I realized soon that I had a symbol in configuration instead of string. Regarding my yesterday's question if it is possible to change environment variable value on the fly - I don't need to change it. I found out that Cognitect's AWS library provides a profile-credentials-provider - so, I can have both of my AWS profiles (local Docker DynamoDB profile and real AWS profile) in my confige.edn and using Mount switch the profile in the REPL without restart. Clojure is great!

Kari Marttila18:05:11

And I really love Cursive. I created a "reset" command and a simple hot key for it - I'm able to "stop states, refresh namespaces, start states" with one keystroke, i.e. e.g. switch the data source from my local dynamodb docker container to the real AWS DynamoDB. Yihaa!

Kari Marttila18:05:23

... and if you see Stuart Halloway there in Cognitect corridors send him my regards regarding his "Repl Driven Development" - it totally changed how I use REPL.

Kari Marttila19:05:03

Having a scratch file where I write all my REPL stuff nowadays instead of writing them in the actual REPL editor really boosted my Clojure productivity. You guys at Cognitect could list some good beginner "must-to-watch-videos" 🙂

Kari Marttila19:05:28

... or someone could write a book titled "50 Clojure coding and REPL tricks" - I surely would buy a copy. 🙂

👍 4
Kari Marttila19:05:20

If I have a vector like

[{:pgid {:S "1"}, :pgname {:S "Books-local-aws"}} {:pgid {:S "2"}, :pgname {:S "Movies-local-aws"}}]
... is there a fancy, Clojure idiomatic way, to turn it to this:
{"1" "Books-local-aws", "2" "Movies-local-aws"}
(I mean using the Clojure standard library without recur or reduce?) I should start to learn to use the standard library - I understood that the standard library often provides various transformations so that you don't need to loop yourself.


why is reduce not idiomatic?

Kari Marttila19:05:40

This works:

          [mymap item]
          (conj mymap {(get-in item [:pgid :S]) (get-in item [:pgname :S])}))


it's the best way to make an associative object out of a seq

Kari Marttila19:05:01

... but is there some standard library function that could be used instead?


you can use assoc instead of conj

Kari Marttila19:05:26

So, my reduce above is just fine?


(conj foo {bar baz}) is the same as (assoc foo bar baz) except it creates a hash-map nobody needs


yes, the reduce is fine, but I'd use assoc

Kari Marttila19:05:48


          [mymap item]
          (assoc mymap (get-in item [:pgid :S]) (get-in item [:pgname :S])))


also, this is open to judgment, but (-> item :pgid :S) might be more readable than (get-in item [:pgid :S]) for this specific case


a line break might help readability as well

(assoc foo
       bar baz)

Kari Marttila19:05:21


          [mymap item]
          (assoc mymap 
            (-> item :pgid :S) (-> item :pgname :S)))

Kari Marttila19:05:37

Good that I asked. 🙂

Kari Marttila19:05:42

So, occasionally if I'm wondering if my small code snippet is idiomatic Clojure or not, you guys don't mind me asking here? I learn a lot with you gurus. 🙂


This is exactly the right place for that sort of question

Kari Marttila20:05:26

All right! I'll be a lot more active here in the future! And I'll be learning Clojure 2x than I previously did. Yihaa!

😄 4

i would do this


there's also #code-reviews but usually that's usually used for bigger things


@nedcg my only problem with that is that putting an arg to a parent on the same line as a child that had a line-break in the middle feels strange


to me a line break mid form introduces a new level of nesting, so keeping an upper level on the same line has a weird friction to it - but of course that's subjective


@noisesmith the line-break is for visually associate the operations over the same parameter(%2), it feels weird to me decompact with line-breaks such a simple function


right, but {} and items are for reduce, not for #(assoc ...)


so I would remove the line break, or add another for each arg to reduce


but as I said at the start, it's subjective, we don't need to have consensus, but it's informative to hear each others reasons I think

👍 4

absolutelly, thanks for the feedback


another way to implement this pattern is with (into {} (map (juxt key-fn val-fn) coll)), where key-fn and val-fn are the corresponding “extractions” on each value in coll


(->> items
     (map (juxt (comp :S :pgid) (comp :S :pgname)))
     (into {}))


or even (into {} (map (juxt kfn vfn)) items)


out of curiosity, does that provide a benefit if there’s only one map?


yes, it avoids creating a lazy-seq nobody needs

👍 4

clojure doesn't do this collapsing of intermediary values automatically (many other languages do), but we can do it explicitly

Kari Marttila20:05:19

Interesting. I need to try that tomorrow (it's midnight here in Finland).

Kari Marttila20:05:55

How do I learn to see things like "it avoids creating a lazy-seq nobody needs"?


specifically, reading about transducers (

Kari Marttila20:05:27

You just looked at the code and immediately saw it?


i personally wouldn’t worry too much about it if you’re learning clojure


transducers can help performance for multi-step list processing

Kari Marttila20:05:21

Ok. I read it tomorrow. I think I'm not absolute beginner, I have used Clojure a bit but now want to be more fluent and learn more idiomatic Clojure coding...


over time you’ll learn what the code actually does: (map f coll) creates a lazy sequence, whereas (map f) does not. so if you’re going to do multiple steps (e.g. map then filter then into), you can avoid creating those intermediate sequence structures and use the more advanced transducer form in order to make it faster

🥘 4

I only really started to learn this stuff after I started digging into how I can make my horribly slow code, less so 😛 but 90% of the time it doesn’t matter