Fork me on GitHub
#beginners
<
2021-10-29
>
Joaco07:10:54

Hi! one question about apply map, more than once I have had the need to apply map over items of list/array only if a condition is valid for the current item mapped , and always my approach is inside the map function do a if and if true apply the change else return the same data. is there a better way to do it in clojure? thanks!

Geoffrey Gaillard07:10:35

Hi! I understand your question as: If I should decide to map or not to map over a collection, should I still place the conditional inside of the mapping function? Is this code example answering your question? 🙂

Joaco08:10:45

thank for your answer, yes, my question is about your first example, the condition depends on the current item being mapped, this is the way I normally do, but since the standard library has a lot of functions, I thought there might be something like map-if 😄

Alexis Schad08:10:23

There's not, but if this is a common pattern in your code, you can write your own map-if!

👍 1
Fredrik08:10:28

There's nothing exactly like that in the standard library, but there are a couple of ways you can make this pattern less tedious. The first one is syntactic: The conditional threading macro cond-> allows us to write Geoffrey's first example above as

(map (fn [x] (cond-> x (odd? x) inc)) my-collection)
The second one is to make your own map-if.

❤️ 1
Fredrik08:10:53

A basic way to do that would be to take the mapping function f and transform it to the form (fn [x] (if (pred x) (f x) x)) before giving it to map

1
Joaco08:10:17

thanks @U024X3V2YN4 for showing me the cond-> I did not know it!

😀 1
Gerome07:10:42

Hi everybody! Is there a tool that I can use to automatically run test-runner when a file changes?

SenyorJou07:10:42

I just discovered this tool https://github.com/lambdaisland/kaocha few days ago and I want to try as soon as I have some time

Fredrik07:10:49

Kaocha is great, you should definately check it out. If you happen to be using Cider with Emacs, you can also try enabling cider-auto-test-mode, which will run all associated tests whenever you load the buffer.

Gerome18:10:22

Alright, thanks!

xbrln18:10:08

I found this library easy to start with https://github.com/jakemcc/test-refresh

Gerome20:11:58

Thanks for all the suggestions. I will give them a try.

pinkfrog13:10:48

Hi&gt; What’s the datalog query that finds all entity-ids in the database?

Benjamin17:10:00

How do I call a function n times with it's own return value? I feel there is a way with reduce but is there a simpler solution? iterate 🙂

💪 1
dorab17:10:19

Will iterate help?

👍 1
Yosevu Kilonzo19:10:58

Hello, I’m trying to trying rewrite https://github.com/garybernhardt/dotfiles/blob/main/bin/git-churn with babashka to get the hang of it. Could I get some feedback?

git log --all -M -C --name-only --format='format:' "$@" | sort | grep -v '^$' | uniq -c | sort -n
I’m running into an error with the --format part of the git log command:
(sh "git" "log" "--all" "-M" "-C" "--name-only" "--format='format:'" "$@")
Error:
{:exit 128, :out "", :err "fatal: invalid --pretty format: 'format:'\n"}
I think it has to do with "$@" , but I don’t understand why yet.

borkdude19:10:39

@UGT1B8S3T $@ is a bash-syntax for arguments passed to a script

borkdude19:10:13

so you could do: (apply sh ... ... ... *command-line-args*)

borkdude19:10:23

to pass any arguments that a user passes to this command

Yosevu Kilonzo19:10:28

Oh I see! I’ll go try that. Thank you.

Yosevu Kilonzo21:10:31

So it actually works using $@ e.g. (apply sh "git" "log" "--all" "-M" "-C" "--name-only" "--format=format:" "$@") As well as (apply sh "git" "log" "--all" "-M" "-C" "--name-only" "--format=format:" *command-line-args*) The error I was getting came from the single quotes in "--format='format:'". It works with "--format=format:"

Yosevu Kilonzo21:10:01

I’m not sure if I want to find out why at this moment though : -)

borkdude21:10:53

@UGT1B8S3T You can also use babashka.process/process or babashka.tasks/shellwhich accepts a single string including spaces and single quotes, so you don't have to separate them all.

borkdude21:10:16

e.g. (babashka.tasks/shell "git log --all -M -C ...")

borkdude21:10:15

to capture the string: (-> (babashka.process/process "git log --all -M -C ..." {:out :string}) deref :out)

Yosevu Kilonzo21:10:56

Thaanks, I’ll try that too. I have to read through more of those docs!

walterl22:10:17

> I’m not sure if I want to find out why It's because your shell "removes" the single quotes around 'format:' in your original git command, but sh won't remove it because you specify the program args separately and they're used verbatim.

walterl22:10:39

Is that clear? I think I may be explaining it badly.

👍 1
Yosevu Kilonzo23:10:42

Yes. That makes sense. Thanks @UJY23QLS1!

👍 1