clojuredesign-podcast

Discussions around the Functional Design in Clojure podcast - https://clojuredesign.club/
nate 2020-11-10T16:41:58.051500Z

@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

nate 2020-11-10T16:42:41.051600Z

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

nate 2020-11-10T16:43:12.051800Z

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

nate 2020-11-10T16:43:34.052Z

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

2020-11-10T17:01:29.052200Z

Great thanks a ton!

2020-11-10T17:02:37.052400Z

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!

nate 2020-11-10T17:46:37.052800Z

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

bartuka 2020-11-10T23:49:16.055100Z

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.

bartuka 2020-11-10T23:49:26.055500Z

For example in this code we used this trick https://github.com/lsevero/abclj/blob/master/src/abclj/core.clj#L60

bartuka 2020-11-10T23:51:53.056100Z

these are also called closures