Fork me on GitHub

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? 🙂


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

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

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


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

😀 1

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


I just discovered this tool few days ago and I want to try as soon as I have some time


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.


Alright, thanks!


I found this library easy to start with


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


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


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

Will iterate help?

👍 1
Yosevu Kilonzo19:10:58

Hello, I’m trying to trying rewrite 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:'" "$@")
{:exit 128, :out "", :err "fatal: invalid --pretty format: 'format:'\n"}
I think it has to do with "$@" , but I don’t understand why yet.


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


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


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 : -)


@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.


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


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!


> 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.


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

👍 1
Yosevu Kilonzo23:10:42

Yes. That makes sense. Thanks @UJY23QLS1!

👍 1