This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
- # beginners (34)
- # boot (14)
- # cider (2)
- # cljs-dev (21)
- # cljsrn (1)
- # clojure (31)
- # clojure-android (10)
- # clojure-spec (12)
- # clojure-uk (3)
- # clojurescript (64)
- # cursive (31)
- # data-science (9)
- # datomic (27)
- # fulcro (11)
- # graphql (14)
- # jobs (1)
- # leiningen (1)
- # lumo (27)
- # off-topic (65)
- # om (2)
- # onyx (77)
- # pedestal (1)
- # re-frame (4)
- # shadow-cljs (6)
- # vim (1)
- # yada (3)
Even for a single operation, depending on what you are doing, the transducer version can be faster.
Thanks, I take it transducer is always a better solution than threading macro. So does all of the benefits still applied to clojurescript?
When is it appropriate to create a closure over a function? e.g
The trade off seems obvious, the closure is only usable inside the function. Which is not the default i would assume most ppl would lean towards. What are some good reasons to do that though?
(defn foo [x] (let [bar (fn [y] y)] (bar x)))
@drewverlee The question I would ask myself is: could this new function be useful to code outside this (enclosing) function? If you think it could be (and, often, it could) then make it a
OTOH, if it closes over a number of local bindings or arguments, and you need its arity to match some particular usage within the (enclosing) function, then a closure is probably the right thing.
For example, in
clojure.java.jdbc, there are a number of "helper" closures defined locally, because they close over local bindings and arguments and don't make sense outside that context. But there are other small "helper" functions defined at the top-level because they only depend on their arguments and they might be reusable elsewhere.
one other consideration is that you may want the function defined outside of the defn form
(let [f (fn [x] x)] (defn thing [y] (f y))
I have a function like this:
(def v [1 2 3 4 5]) (defn compare-values [v] (for [x v y v ] (compare-stuff x y))
compare-stuffis going to perform some logic and return a vector if said logic is met. Otherwise, I return nothing. I am currently going through the process of writing tests and noticed that when I pass the above
(compare-values [ 1 2 3])I will get
(nil nil nil). I see that this is because
compare-stuffis going to return
nilif I do not explicitly return a value, but is there a way to have
(compare-values [1 2 3])return
(nil nil nil)?
if compare-stuff returns nothing when the logic is not met, then
(nil nil nil) seems to be the right result... fits the shape of the input collection
Correct. Is there a way to control the output so I can get something more in-line to what I would like to see returned which would be
if you wish to discard
nil values from the result of
for, then maybe
filter out nils?
Depends on why you want it to be
() in particular. Do you just want to filter out nil results?
I would want it to be
(2) - so yes, I want to filter our the
nils - but leave other values
(defn integersLessThan "Returns a vector of integers less than the number passed in." [n] (loop [x 0 acc ] (if (= x 0) (println "starting with: " x acc)) (when (> x n) (println "Done looping! " x acc) acc) (when (<= x n) (let [newNum (+ x 1)] (println "looping!" x) (println acc) (recur newNum (conj acc newNum)))))) (integersLessThan 8)
I would think that line 12 should return the "acc" vector, but for some reason my function always returns nil. I'm really confused by why this is. Is the loop /recur one of those things that is meant only for side effects and is not supposed to ever return something?
When x is more than n,
(when (> x n) acc) returns acc, and then
(when (<= x n) ,,,) runs and returns
loop returns the value of the last form.