Fork me on GitHub
#beginners
<
2019-05-04
>
tabidots06:05:37

If I have a map like {1 {:k1 v1 :k2 v2}, 2 {:k1 v3 :k2 v4}} is there a better way to map or reduce over all :k1s than

(reduce-kv (fn [res _ {sub-key :k1}] (do-stuff res sub-key)) init the-map)
` ?

valtteri07:05:16

I’m not sure if I get what you’re after but maybe something like this?

user> (def m {1 {:k1 "v1" :k2 "v2"}, 2 {:k1 "v3" :k2 "v4"}})
#'user/m
user> (map :k1 (vals m))
("v1" "v3")

valtteri07:05:35

Or this perhaps?

user> (map #(select-keys % [:k1]) (vals m))
({:k1 "v1"} {:k1 "v3"})

valtteri07:05:48

And you can also combine map and reduce like this for example:

user> (->> m
           vals
           (map :k1)
           (reduce str))
"v1v3"

4
tabidots07:05:41

Cool, thanks. I ended up using something like the 1st/3rd snippets you posted:

(->> (select-keys big-map relevant-keys)
     vals
     (mapv :the-subkey-i-want)
     (reduce *'))

tabidots07:05:09

Admittedly what I am doing is already quite messy, but I’m trying to untangle some spaghetti Python into Clojure so I just want to get it working first, and then clean it up

valtteri07:05:03

Yeah that’s what I also often do. First get it to work and then make it properly. If something feels or looks ugly or too complex there’s a usually simpler way of thinking it.

Jeevaraj MV14:05:10

@here I just wanted to start with just creating CRUD operation,which is the best framework pls let me know

Eric Ervin21:05:04

Don't know the best framework (I honestly think it is no framework) but the Luminous creator wrote a book https://pragprog.com/book/dswdcloj2/web-development-with-clojure-second-edition

solf14:05:18

Not a question, just a thought: it's easier for my brain to read/write #(...) functions if I think of them as a partial function rather than an anonymous function

Kari Marttila17:05:43

All Ring examples seem to use Leiningen or Boot. Is there some documentation or blog which gives short instructions how to configure and use Ring with deps.edn for development use etc?

donaldball17:05:39

Something like this might suit:

{:paths ["resources" "src"]
 :deps {hiccup {:mvn/version "1.0.5"}
        ring {:mvn/version "1.7.1"}}}

seancorfield20:05:30

@kari.marttila Not sure what you're looking for. We use Ring etc with deps.edn all the time and I don't know what instructions or configuration you think you need? It's just dependencies.

seancorfield20:05:14

We used to use Boot and before that we used to use Leiningen and it's all the same.

seancorfield20:05:28

We didn't use any plugins.

seancorfield20:05:04

Here's an example repo using deps.edn with Ring, Selmer, Compojure.

Luiz Sol22:05:04

Hi everyone. I'm just starting to learn clojure and to do so I'm trying to port an application I've build in the past to the clojure ecosystem (clj, cljs and datomic). I was able to find lots of materials on the first two topics but I'm not being able to find a beginners guide to building and configuring datomic. Most guides either assume that I'll use only the in-memory version of datomic or that someone else already took care of deploying a datomic envinronment. *TL;DR*: Is there any place in which I can learn how to set up and configure datomic pro starter with a postgresql (preferably using docker)?

jumar12:05:53

Better skip the Datomic part if you're just starting with Clojure - at least, that would be my recommendation. Start with something you're already familiar with - that is use the same DB technology if possible. You can later rewrite that part and compare to the first version.

Luiz Sol17:05:15

Maybe you're right. Thanks a lot.

sotrhraven23:05:11

I was looking through some code. I saw a map like {^:height height} is it similar to using ^Observable for a java type.

seancorfield23:05:36

@sotrhraven That's adding metadata to a Clojure object. Any map of data can be added to (most) Clojure objects.

seancorfield23:05:56

^Observable is a type hint, which is slightly different.

sotrhraven23:05:54

ok I will look into metadata on the clojure website.

seancorfield23:05:22

^Observable x will add the metadata {:tag Observable} to x so it is a type of metadata, but it's specifically for the compiler (to avoid reflection etc).

seancorfield23:05:06

But metadata can be arbitrary data. In Clojure 1.10, you can add metadata to objects to satisfy extensible protocols which is really powerful.