Fork me on GitHub
#babashka
<
2023-06-16
>
leifericf07:06:01

Here comes another newbie question 😅 I often want to capture :out from babashka.proces/sh and sometimes parse it to JSON, so I made these helper functions:

(defn sh->out
  [opts & args]
  (-> (apply process/sh opts args)
      :out))

(defn sh-out->json
  [opts & args]
  (-> (sh->out opts args)
      (json/parse-string true)))
And I also have this function to run Git commands:
(defn run-git-command
  [command path]
  (-> (process/sh {:dir path} "git" command)
      :out))
Example usage:
(->> (find-repo-paths (str (babashka.fs/home) (:local/repo-root-dir settings)))
     (map (partial run-git-command "status")))
I just realized that run-git-command does very little and can probably be removed in favor of using sh->out directly. But I can't figure out how to pass {:dir path} into sh->out (for its first parameter, opts) via the threading macro:
(-> (find-repo-paths (str (babashka.fs/home) (:local/repo-root-dir settings)))
    (map (partial sh->out {:dir ???} "git" "status")))

lispyclouds07:06:39

(->> (find-repo-paths (str (babashka.fs/home) (:local/repo-root-dir settings)))
     (map #(sh->out {:dir %} "git" "status")))
should work?

2
leifericf07:06:55

Oooh, I didn't consider using an anonymous function in there with %. Clever. It works! Thanks!

lispyclouds07:06:23

another thing ive learnt over time is that whenever i feel a need to use partial, see if #(...) solves it first. its a more general solution than partial

💡 4
leifericf07:06:05

Thanks to help from others in the community, I'm slowly building up a mental inventory of "rules of thumb," like "`#(...)` is often preferable to partial." They come with experience. Perhaps collecting such "rules of thumb" and sharing them somewhere would be helpful. Almost like "Clojure Proverbs" 😂

leifericf07:06:43

The closes thing I've seen to that is the book https://leanpub.com/elementsofclojure by @U066UJ2KE