Fork me on GitHub

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?


And is there any github repo I can study where it abuses transducer?

Drew Verlee00:11:03

When is it appropriate to create a closure over a function? e.g

(defn foo
  (let [bar (fn [y] y)]
   (bar x)))
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?


@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 defn instead.


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, 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 ie, (let [f (fn [x] x)] (defn thing [y] (f y))

Drew Verlee02:11:38

@dpsutton i see how thats useful. @seancorfield That makes sense, i appreciate the jdbc example. I’m glad the advice matches my intuition. I really appreciate the feedback


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))
The function compare-stuff is 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-stuff is going to return nil if I do not explicitly return a value, but is there a way to have (compare-values [1 2 3]) return () instead of (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?


(remove nil? 
   (for [x v 
         y v] ...))


Looks like you've got a stray paren there:

(for [x v )
            y v ]


Sorry, messed up the cleanup there. Fixed


Depends on why you want it to be () in particular. Do you just want to filter out nil results?


What if it came back, say, (nil 2 nil) -- what would be the output you wanted?


I would want it to be (2) - so yes, I want to filter our the nils - but leave other values


There are a few ways to do that; one is (filter identity <result>)


Oh, just saw that @adityaathalye is replying in parallel 😆


Another is just (keep identity <result>)


Thanks @adityaathalye and @eggsyntax - big help. Thinking in functional paradigm colours is kicking my ass.


It takes some getting used to, for sure 🙂


Comes naturally after a while, I promise.


check out keep


Hey guys, I'm having trouble returning something from loop /recur


(defn integersLessThan 
  "Returns a vector of integers less than
  the number passed in."
(loop [x 0
       acc []]
  (if (= x 0)
    (println "starting with: " x acc))

  (when (> x n)
    (println "Done looping! " x 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?


@derpocious what is the last form in your loop?


When x is more than n, (when (> x n) acc) returns acc, and then (when (<= x n) ,,,) runs and returns nil. loop returns the value of the last form.


To fix it put them both inside different branches inside one if