Fork me on GitHub
#clojuredesign-podcast
<
2020-11-10
>
nate16:11:58

@heow sorry to miss your thread about let examples, but here's one from my recent code. I posted a short babashka script for downloading artifacts from CircleCI, and the artifacts-for function has an example. The first let line fetches recent builds, but I don't know if I've found a build number that I want, so the artifact-urls local variable is guarded by a when call. https://gist.github.com/justone/87918a0ca5ab65575d94bc6a127aa48e

nate16:11:41

the artifact-urls function:

(defn artifacts-for
  [ci user project job-name]
  (let [recent-builds (-> (recent-builds-url ci user project recent-build-count "completed")
                          (curl/get)
                          (parse-body-json))
        recent-job-build-num (find-build-num-for recent-builds job-name)
        artifact-urls (when recent-job-build-num
                        (-> (artifacts-url ci user project recent-job-build-num)
                            (curl/get)
                            (parse-body-json)))]
    artifact-urls))

nate16:11:12

It could also be done with a some->>, like this:

(defn artifacts-for
  [ci user project job-name]
  (let [recent-builds (-> (recent-builds-url ci user project recent-build-count "completed")
                          (curl/get)
                          (parse-body-json))
        artifact-urls (some->> (find-build-num-for recent-builds job-name)
                               (artifacts-url ci user project)
                               (curl/get)
                               (parse-body-json))]
    artifact-urls))

nate16:11:34

it's a little contrived, but I hope it helps as an example

heow17:11:29

Great thanks a ton!

heow17:11:37

I know exactly how hard this is, I've written articles on functional programming without code and it's a challenge. :-) That the podcast has a really great job of it!

nate17:11:37

thanks, it's a tough line to walk, especially in our recent episodes where we focus on a single function

bartuka23:11:16

I just listen to the Let Tricks episode too 😃 cool. There is a very famous trick in other communities called "Let over Lambda" (there is still a book with the same name) which is using let and defining a function inside the let body. The idea is to create a lexical scope to the variables that are visible only by that function.

bartuka23:11:53

these are also called closures