This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-04-07
Channels
- # announcements (2)
- # beginners (25)
- # calva (16)
- # cljdoc (37)
- # cljsrn (24)
- # clojure (204)
- # clojure-nl (4)
- # clojure-spec (34)
- # clojure-uk (3)
- # clojurescript (13)
- # cursive (8)
- # data-science (3)
- # dirac (21)
- # figwheel-main (1)
- # luminus (3)
- # off-topic (45)
- # pedestal (3)
- # planck (2)
- # re-frame (5)
- # shadow-cljs (270)
- # spacemacs (5)
determine the largest 3 numbers in a vector (defn new_max3s [[max1 max2 max3] x] (cond (> x max1) [x max1 max2] (> x max2) [max1 x max2] (> x max3) [max1 max2 x] :else [max1 max2 max3])) (reduce new_max3s [Integer/MIN_VALUE Integer/MIN_VALUE Integer/MIN_VALUE] [11 80 10 4 5 6 1]) is it ok to create a new list in new_max3s everytime a large number is found
I believe a new vector gets created at each iteration, even if a larger number isn't found. Don't think it's gonna be a huge problem. Ephemeral garbage is pretty cheap on the JVM.
And I believe that because destructuring pulls out and binds max1
, max2
and max3
to values and in the :else
branch a new vector gets constructed.
You could avoid that by explicitly binding the first arg like this:
(defn new_max3s [[max1 max2 max3 :as current_maxima] x]
(cond
(> x max1) [x max1 max2]
(> x max2) [max1 x max2]
(> x max3) [max1 max2 x]
:else current_maxima))
I'm sure there are better ways to write this fn though.@jaihindh.reddy thanks. It looks like i cant recur at every cond predicate , So to avoid the list , i tried this - (defn -max3s [xs max1 max2 max3] (if (empty? xs) [max1 max2 max3] (let [x (first xs)] (cond (> x max1) (-max3s (rest xs) x max1 max2) (> x max2) (-max3s (rest xs) max1 x max2) (> x max3) (-max3s (rest xs) max1 max2 x) :else (-max3s (rest xs) max1 max2 max3))))) (defn max3s [xs] (-max3s xs Integer/MIN_VALUE Integer/MIN_VALUE Integer/MIN_VALUE))
i liked the reduce better, since it frees me from thinking about recursion etc and just worry about the function that i pass to reduce. That is just deal with the current iteration.
which seems like a simple problem to solve. But its not quite clear what is clojure "style"
is this an exercise where you're supposed to avoid using parts of the standard library?
@qythium thanks. Yep your solution looks cool. Yes am laboring through Structure and Interpretation of Computer Programs and trying to solve the exercise in clojure
eg. your -max3s function above might blow the call stack on a long list because there's no automatic tail-call optimization
@gayathrik Something to bear in mind about SICP is that ... hahaha, yeah, I was about to say that ... and there's also quite a bit of mutation in SICP as I recall?
More likely to get help quicker there
I'm doing this example https://github.com/exoscale/exopaste
still don't know how to use reloaded in exopaste project 😥 https://github.com/exoscale/exopaste/blob/0.1.0/src/exopaste/system.clj
@minhnhat10bk before you do all that, make sure you read the REPL guide at https://clojure.org/guides/repl/introduction