This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-11-12
Channels
- # 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?
And is there any github repo I can study where it abuses transducer?
When is it appropriate to create a closure over a function? e.g
(defn foo
[x]
(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 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
ie, (let [f (fn [x] x)] (defn thing [y] (f y))
@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 ()
?
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?
I would want it to be (2)
- so yes, I want to filter our the nils
- but leave other values
Oh, just saw that @adityaathalye is replying in parallel 😆
Thanks @adityaathalye and @eggsyntax - big help. Thinking in functional paradigm colours is kicking my ass.
Hey guys, I'm having trouble returning something from loop /recur
(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?
@derpocious what is the last form in your loop?