@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
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))
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))
it's a little contrived, but I hope it helps as an example
Great thanks a ton!
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!
thanks, it's a tough line to walk, especially in our recent episodes where we focus on a single function
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.
For example in this code we used this trick https://github.com/lsevero/abclj/blob/master/src/abclj/core.clj#L60
these are also called closures
nice reading https://letoverlambda.com/index.cl/guest/chap2.html#:~:text=A%20closure%20is%20like%20an,can%20funcall%20in%20multiple%20ways.&text=This%20let%20over%20two%20lambdas,the%20same%20enclosing%20counter%20variable.. ( I need to warn about the style/kind of language used by the author... might be good to read https://letoverlambda.com/index.cl/clarifications too)