Fork me on GitHub
#beginners
<
2021-10-21
>
maverick12:10:13

I have an atom with values as `(def values (atom {:data [{:a "test"} {:a "new"}]}))` I want to remove map from `:data` where `:a = "test"`

pavlosmelissinos12:10:45

@UDF11HLKC’s answer looks right, but there are multiple problems hidden in your question. I'm seeing this a lot in most beginners (I do it quite often). As a suggestion for the future: Break hard problems down! You want to remove a map from a vector of maps that are a value in a map and that map also happens to be an atom. Broken down into smaller problems, that's more or less: 1. Given a map like {:a "test"} or {:a "new"}, can you tell whether it's a map that you want to keep or not? 2. Given a vector of maps like the above, how can you keep just the ones you want? 3. What if that vector is the value of a map key? 4. How do you store the result of such an operation in an atom?

Lennart Buit12:10:28

(swap! values (fn [m] (update m :data #(filterv (fn [{:keys [a]}] (not= a "test")) %))))

Lennart Buit12:10:08

uh almost, edited

maverick12:10:59

Thanks, I tried

maverick12:10:04

(swap! values assoc-in [:data] (into [] (remove #(= (:a %) "test") (:data @values))))

maverick12:10:52

I am not sure which one is efficient to use ?

Lennart Buit12:10:18

efficient, not sure, but there is an atomicity difference between yours and mine I think

Lennart Buit12:10:50

you deference the atom twice, once in (:data @values) and once in the swap! operation

Lennart Buit12:10:41

so, the list you removed maps from may have changed before your change was ready to be swapped in. If that makes sense

maverick12:10:31

It make sense.

maverick12:10:44

I was thinking If we have about 100000 maps in it. So which will be faster

Lennart Buit12:10:01

I haven’t measured, but I’d assume yours is faster because its not as thread safe as mine. In my version, if the swap fails, it needs to filter the entire list again. In yours, it doesn’t, but it will swap in a stale filtered list

maverick13:10:02

Okay, Thanks

orpheus14:10:52

Could anyone recommend Programming Clojure vs Getting Clojure as a first clojure learning resource?

⚔️ 1
Colin P. Hill14:10:49

I’ve been enjoying the style of Getting Clojure

Colin P. Hill14:10:22

I’m coming back to Clojure after having first made an effort to learn it a few years ago though

Alex Miller (Clojure team)14:10:16

as a co-author of Programming Clojure, I love Russ's writing :)

1
athomasoriginal14:10:55

I’ve read both. I liked Getting Clojure for a first and then Programming Clojure as a follow up.

cdpjenkins14:10:58

I learned Clojure from Programming Clojure (the first edition - it was a while ago) and I really liked it. I more recently read the third edition and it was still good.

Alex Miller (Clojure team)14:10:33

PC has a lot more "coming from Java" feel whereas I think Russ's is much more "coming from Ruby" feel (all of the above authors are polyglots of course, but that's my sensibility)

👍 3
cdpjenkins14:10:25

Heh, I was most definitely “coming from Java” back when I first read Programming Clojure 🙂

orpheus15:10:57

Thank you all. Couldn’t decide between the comments so I just got both

😺 1
😎 2
RAJAT GOYAL18:10:19

what do someone mean by "Just ensure to include :dev and :test aliases in your project"?

dpsutton18:10:42

i can think of two contexts. Good discipline to have two aliases in your project dev and test such that new people can run tests easily by just clj -A:test and can get a dev environment up easily by just clj -A:dev.

dpsutton18:10:02

the other context is if they were telling you to start a clojure process with those aliases. clj -A:dev:test as opposed to clj

RAJAT GOYAL18:10:58

sorry I didn't get you, what do they want me to do ? add something in the code to "add these aliases" or are they asking me to run the commands you have given in the terminal anything like that.

RAJAT GOYAL18:10:26

yess the 2nd context seems to be the condition here as they are asking me to setup a clojure project running in my local machine.

dpsutton18:10:34

i'm not sure. that's why i listed both contexts that sound plausible to me

RAJAT GOYAL18:10:56

"Setup a Clojure development environment: You will need Calva for VS Code, and sample project. The sample project uses clojure and polylith. Your goal is just get it running in your local system. "

RAJAT GOYAL18:10:20

this is the goal I have right now.

dpsutton18:10:23

do you have a link?

RAJAT GOYAL18:10:16

yaah wait check your dm

dpsutton18:10:30

just post it here if its a public link to a tutorial

RAJAT GOYAL18:10:26

I think it's not a public link. this is for the intern, I am appllying for.

pavlosmelissinos19:10:24

@U02JX4YBPCH The link is public and it refers to the second context @U11BV7MTK mentioned before: > the other context is if they were telling you to start a clojure process with those aliases. `clj -A:dev:test` as opposed to `clj` The polylith project is arguably quite complicated for a beginner but in a nutshell :dev and :test are aliases in its deps.edn file. https://github.com/Croooook/clojure-polylith-realworld-example-app/blob/e242541e65f93c56055c09a0c587ade90e532eb2/deps.edn#L4-L44 and the test alias is right below that.. As you can see they're used to define stuff like a custom classpath and dependencies. These might ease your way: • Skim the Clojure CLI tools guide https://clojure.org/guides/deps_and_cli • Read this to understand what aliases are in the context of Clojure CLI tools https://practical.li/clojure/reference/clojure-cli-tools/defining-aliases.html • Check out the deps.edn files of other projects to figure out how they are structured, e.g. https://github.com/clojure/core.async/blob/master/deps.edn or https://github.com/seancorfield/next-jdbc/blob/develop/deps.edn or https://github.com/rm-hull/nvd-clojure/blob/master/deps.edn

🙌 1
RAJAT GOYAL19:10:39

Thanks! to both of you sir.

pez19:10:57

Since Calva is mentioned in the task, this page could be relevant: https://calva.io/polylith/

🙌 1
1
pez19:10:48

It even uses the Real World Example as the example.

🙌 1
pez19:10:37

You are super welcome to ask for Calva help in #calva. And there is also a #polylith channel.

🙌 1
RAJAT GOYAL18:10:41

I am completely new to this topic but I really want to learn it. And it's going somewhat confusing tbh.

pez19:10:59

It is a lot to take in at the same time, and we who have been in here for a while tend to forget how it was when we were completely new to it. Please bear with us, we really want to help. While you are new and experiencing the quirks, please consider sharing your experience in #improve-getting-started.

🙌 1
pez19:10:34

I think it is quite heavy to bring in Polylith into the mix right off the bat. You can prepare yourself a bit before you open that box. I have tried to pave a beginner’s path here: https://calva.io/get-started-with-clojure/

🙌 1
Pablo20:10:59

Hello Is it possible to define both, :once and :each fixtures on the same namespace?

dpsutton21:10:00

(ns test-ns
    (:require [clojure.test :as t]))
(t/use-fixtures :once (fn [f] (println "doing once fixture") (f)))
(t/use-fixtures :each (fn [f] (println "doing each fixture") (f)))
(t/deftest foo (t/is (= 1 2)))
(t/run-tests)

doing once fixture
doing each fixture
{:test 1, :pass 0, :fail 1, :error 0, :type :summary}

Pablo21:10:58

lol, easy! Thnks 😄

dpsutton21:10:11

no worries. i was reading (doc clojure.test) and it didn't say anything to the contrary and seemed to indicate it would work

seancorfield21:10:07

Yes, this allows you to have both "around each test" and "around the whole ns" for testing. I think in most cases folks tend to just have one or the other. What I often find is missing is "around the entire test suite" (a feature that I requested for Polylith, and that was added recently). We have a wrapper for running tests at work that provides this "whole suite" fixture as well.

👍 1
1