Fork me on GitHub
#beginners
<
2019-04-07
>
clojure beginner02:04:49

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

jaihindhreddy03:04:28

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.

jaihindhreddy03:04:45

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.

clojure beginner03:04:11

@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))

clojure beginner03:04:58

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.

clojure beginner03:04:18

which seems like a simple problem to solve. But its not quite clear what is clojure "style"

yuhan03:04:54

is this an exercise where you're supposed to avoid using parts of the standard library?

yuhan03:04:05

otherwise the clearest way would be something like (take 3 (reverse (sort xs)))

yuhan03:04:41

or (take-last 3 (sort xs))

clojure beginner03:04:39

@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

yuhan03:04:37

Ah, that makes sense

yuhan03:04:50

There's a lot of emphasis on recursion there which won't be idiomatic Clojure

yuhan03:04:51

eg. your -max3s function above might blow the call stack on a long list because there's no automatic tail-call optimization

seancorfield03:04:23

@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?

Ivan Koz05:04:08

Is there a way to extract form into a defn in cursive?

jaihindhreddy05:04:52

You might wanna ask in #cursive

👍 4
jaihindhreddy05:04:03

More likely to get help quicker there

Ivan Koz05:04:26

oh i never knew we have so many channels on slack, wow haha

minhnn05:04:10

Everytime I change my code I need to re-run lein run and It take too much time

minhnn05:04:20

How can I fix that ?

minhnn06:04:34

still don't know how to use reloaded in exopaste project 😥 https://github.com/exoscale/exopaste/blob/0.1.0/src/exopaste/system.clj

Ivan Koz05:04:17

must be what you need, also read related article by Stuart Sierra

orestis05:04:16

@minhnhat10bk before you do all that, make sure you read the REPL guide at https://clojure.org/guides/repl/introduction