Fork me on GitHub
#beginners
<
2022-05-20
>
zakkor06:05:56

How would you indent/format this snippet?

(defn add-namespace [m nms]
  (into {} (map (fn [[key val]]
                  {(keyword nms (name key))
                   (if (map? val)
                     (add-namespace val (name key))
                     val)}) m)))

(defn attrify [m rename nms]
  (add-namespace (set/rename-keys m rename) nms))

yuhan10:05:30

I would put the m argument of map on its own separate line, it's generally considered poor readability in lisps to place additional arguments directly after some multi-line form, as you'll have to rely on parsing parens instead of indentation to figure out its nesting. I was also about to suggest rewriting using reduce-kv here instead of (into {} (map ...)) but on further reflection, I'm not sure either is more idiomatic.

yuhan10:05:14

Also consider using k and v instead of shadowing clojure.core/key and val, and returning a vector pair from your map function instead of a hash map.

šŸ‘ 1
zakkor10:05:52

Thanks for your feedback, here's the latest version:

(defn add-namespace [m nms]
  (into {}
        (map
         (fn [[k v]]
           [(keyword nms (name k))
            (if (map? v)
              (add-namespace v (name k))
              v)])
         m)))

Em16:05:19

this is a half tongue in cheek answer, and not at all trying to start a flame war, but iā€™d lint it like this: pbpaste | zprint and then iā€™d continue to never think about formatting again, bc zprint does all my formatting thinking. (it comes out looking like this, btw

(defn add-namespace
  [m nms]
  (into {}
        (map (fn [[key val]]
               {(keyword nms (name key))
                  (if (map? val) (add-namespace val (name key)) val)})
          m)))
)

devn02:05:45

I'd let out the key and value and return {k v}

devn02:05:42

I don't like the inline if on a value or the key transform without a let, personally

devn02:05:33

also seems like a place to use reduce-kv

zakkor06:05:08

I think zprint is exactly what I was after. Also appreciate all the advice, thanks!

šŸŽ‰ 1
Obi-wan Ralinoro16:05:51

Hi everyone, my name is Hoby, nice to meet you all. I'm learning how to program in the hope of switching career. My introduction to programming was through a book called How to Design Programs which was in Scheme. Then followed by a introduction to computer science in Python and Data Structures and Algorithms in Java. Now I'm doing a course specialization for web development (Node.js / React). The thing is I miss scheme, which I really liked, so I decided to look around if there was a language similar but was used outside of a classroom. That's how I found Clojure and Pact (a smart contract language used by Kadena). Now on to my questions: there a lot of openings in the main web stack if you're looking for a job even as a junior, and remote. How are the clojure stack jobs towards juniors? Are there any companies that would train people in their stack? I'm from a third world country so that would complicate things I guess. I'm not necessarily looking for a job now, but wondering if it's worth my time and effort to go all in on clojure? I love the language but if I can't get hired I wouldn't put much effort into it. Thanks a lot for reading, any input is valuable so don't hesitate to answer.

andy.fingerhut16:05:24

I do not have answers for you myself, but there are channels here #jobs-discuss and #remote-jobs where you might find some advice.

seancorfield16:05:36

Welcome @U03FVNJLX8R, but please don't cross-post the same thing in multiple channels.

Obi-wan Ralinoro20:05:56

@U04V70XH6 sorry, as you see I'm new, my bad breaking rules.

Laura Petrescu10:07:43

Hey @U03FVNJLX8R, welcome! I can answer this from the perspective of a recruiter: Clojure jobs are harder to find than others, but that comes with the good news that strong Clojure companies are aware of this tough market and have good development programs. Or at least they're more committed to training. Take the example of Reify Health (I'm not pitching my open roles, we don't have junior roles open right now anyway): during the recruitment process, we check for good dev practices regardless of language, and an overall thorough consideration of technical tradeoffs and business tradeoffs. TL:DR; Finding a junior role is hard in itself, and I wish you all the best with it. From there on, it should all go smoothly. If you apply for a Clojure role in particular, I doubt that the most important factor for whether you pass or not will be your specific Clojure knowledge. Good luck in your search!

Oliver Marks19:05:34

is there a fn to drop the namespace from keys ? other than doing (keyword (name :ns/key)) ?

seancorfield19:05:49

When I see people asking about this, I always ask "Why are you dropping the namespace qualifier?"

Oliver Marks19:05:53

so I am mapping from namespaced keys in a datascript db to keys stored in a map in an atom, the atoms keys are unqualified but except for that they map

seancorfield19:05:13

(if you're on Clojure 1.11 or ClojureScript 1.11.54, you can do (update-keys my-map (comp keyword name)) if you must šŸ™‚ )

Oliver Marks19:05:53

that's pretty much what I am doing, just felt like I would be missing a function

Oliver Marks19:05:29

translating for thing like json seems like a common use case, so figured there might have been a built in

seancorfield19:05:43

All the JSON libraries either strip the qualifier or have an option to do so. So you can work with qualified keys in Clojure/Script and then automatically produce unqualified keys for JSON.

seancorfield19:05:17

"the atoms keys are unqualified" -- why not use qualified keys in the atom?

Oliver Marks19:05:38

I guess I could just a case of doing a load of refactoring and its not where I wanted to focus my time šŸ™‚

Oliver Marks19:05:26

maybe I will have a think about it, updated the project to latest clojure thanks for clarifying that there is nothing in core as well

seancorfield19:05:27

"Embrace the qualifiers!" šŸ˜†

ā˜ļø 1
seancorfield19:05:51

At least update-keys means you don't have to walk the hash map yourself.

seancorfield19:05:07

(there's update-vals as well, which can also be very useful)

Oliver Marks19:05:22

yeah that is certainly true šŸ™‚