This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-09-06
Channels
- # announcements (3)
- # beginners (83)
- # calva (11)
- # cider (24)
- # cljdoc (2)
- # cljs-dev (1)
- # clojure (216)
- # clojure-berlin (1)
- # clojure-dev (18)
- # clojure-europe (8)
- # clojure-italy (5)
- # clojure-losangeles (2)
- # clojure-nl (4)
- # clojure-spec (34)
- # clojure-uk (75)
- # clojuredesign-podcast (12)
- # clojurescript (33)
- # clojutre (13)
- # community-development (1)
- # core-async (38)
- # cursive (19)
- # datomic (28)
- # duct (3)
- # emacs (1)
- # events (5)
- # figwheel-main (3)
- # fulcro (93)
- # kaocha (20)
- # lambdaisland (2)
- # off-topic (40)
- # pathom (17)
- # pedestal (8)
- # quil (1)
- # re-frame (14)
- # reitit (19)
- # shadow-cljs (34)
- # sql (8)
- # tools-deps (6)
- # vim (1)
- # xtdb (8)
- # yada (18)
As I'm a few episodes behind, I'm not sure whether you still do there questions or whether my question is worth an episode, but I still wanted to ask: What do you think about about defining small helpers in a let
inside of the function? I quite like it, as they are private to the function (cannot be used outside) and I can initialize them with arguments to the outer function or other values calculated in the let. Otherwise I need to always pass these values as arguments to these inner functions. A colleague thinks that the function becomes too big that way. I actually only consider the let body to be the function body.
(defn- xxx [{:keys [account-codes]}]
(let [relevant-line? (fn [line]
(contains? account-codes (:account-code line)))
# ...
Regarding too big. I often define these outside the let and then bind them there. It makes them easier to test as well. They are not as private, granted, but if my modules are small enough, that is not too much of a trade off.
It will depend on what my needs are... If I need the closure, or not, or if it is just a way to keep the let box shorter, or ability to test the utility function. In its simplest form it is just a (defn- foo ...)
and then a (let [f foo])
.
@igel I'm not sure if you've come across it, but letfn
also exists for this purpose. I think it can be a really handy technique if you want to close over a value passed into the function, or if you want some sort of mutual recursion where the functions reference each other.
I really do like make short little predicate functions and using those instead of long conditional statements. Since the condition can be pretty specific to the enclosing function, I like having the function definition inside the function.
On the other hand, sometimes I find myself needing the same little predicates in more than one place, so then I would promote it up to make it is own function.