This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-10-29
Channels
- # aleph (3)
- # announcements (29)
- # babashka (99)
- # beginners (30)
- # calva (46)
- # cider (9)
- # clara (1)
- # cljsrn (4)
- # clojars (10)
- # clojure (41)
- # clojure-dev (4)
- # clojure-europe (45)
- # clojure-nl (3)
- # clojure-norway (13)
- # clojure-uk (5)
- # clojurescript (61)
- # community-development (11)
- # cursive (10)
- # data-science (1)
- # events (1)
- # fulcro (17)
- # graphql (1)
- # gratitude (1)
- # holy-lambda (1)
- # jobs (4)
- # jobs-discuss (5)
- # meander (22)
- # off-topic (50)
- # pedestal (3)
- # re-frame (3)
- # reagent (3)
- # reitit (82)
- # releases (2)
- # rewrite-clj (14)
- # shadow-cljs (3)
- # spacemacs (14)
- # tools-deps (7)
- # xtdb (33)
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!
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 😄
There's not, but if this is a common pattern in your code, you can write your own map-if!
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
.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
Hi everybody! Is there a tool that I can use to automatically run test-runner when a file changes?
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
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.
I found this library easy to start with https://github.com/jakemcc/test-refresh
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
🙂
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.@UGT1B8S3T $@
is a bash-syntax for arguments passed to a script
Oh I see! I’ll go try that. Thank you.
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:"
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/shell
which accepts a single string including spaces and single quotes, so you don't have to separate them all.
to capture the string:
(-> (babashka.process/process "git log --all -M -C ..." {:out :string}) deref :out)
Thaanks, I’ll try that too. I have to read through more of those docs!