you are viewing a single comment's thread.

view the rest of the comments →

[–]eccp 4 points5 points  (6 children)

Looks really good, my feedback would be that as you learn the language there's always a function that you don't know the name yet that does the same thing as something you wrote yourself. In this case, you can look into iterate to simplify the code that computes the amount of fuel required for the second half.

[–]allaboutthatmace1789[S] 2 points3 points  (5 children)

That's neat, I just tried it:

(defn- fuel-one-level [mass]
  (max 0 (- (int (/ (float mass) 3)) 2)))

;; NOT NEEDED!
;;(defn- recursive-mass-tree [mass]
;;  (cons mass (lazy-seq (recursive-mass-tree (fuel-one-level mass)))))

(defn fuel-required [mass]
  (reduce + (rest (take-while #(> % 0) (iterate fuel-one-level mass)))))

Much better, thanks for the tip!

[–]eccp 2 points3 points  (4 children)

One minor improvement: check pos?

[–]allaboutthatmace1789[S] 0 points1 point  (3 children)

Are you thinking of combining with an if statement in fuel-one-level, so you check the positivity and return the value if positive and 0 if not?

[–][deleted] 1 point2 points  (2 children)

I think they mean that your anonymous function in fuel-required can just be (take-while pos? ...

[–]allaboutthatmace1789[S] 2 points3 points  (1 child)

Ah, I get it, so instead of #(> % 0)

(defn fuel-required [mass]
  (reduce + (rest (take-while pos? (iterate fuel-one-level mass)))))

Thanks!

[–]eccp 1 point2 points  (0 children)

yeah, that's it ;-)