This particular problem has already been posted here before, but the community is ok with me to post it again the community is ok with me to post it again, so I can have feedback on my own implementation.
I tried to keep things simple, with each function having its own responsibility so they can be reused in other contexts. Here it is:
(ns marcofiset.euler.problem2)
(defn fib [n]
(if (< n 0)
(throw (Exception. "Can't compute fib of n < 0"))
(loop [i n cur 1 next 1]
(if (zero? i)
next
(recur (dec i) next (+ cur next))))))
(defn fibs []
(map fib (iterate inc 0)))
(defn fibs-until [max]
(take-while #(< % max) (fibs)))
(defn solution []
(apply + (filter even? (fibs-until 4000000))))
(println (solution))
I'm beginning to learn Clojure and this is one of my first exercices. I'd like to have some feedback from more experienced Clojure programmers.
This particular problem has already been posted here before, but the community is ok with me to post it again, so I can have feedback on my own implementation.
I tried to keep things simple, with each function having its own responsibility so they can be reused in other contexts. Here it is:
(ns marcofiset.euler.problem2)
(defn fib [n]
(if (< n 0)
(throw (Exception. "Can't compute fib of n < 0"))
(loop [i n cur 1 next 1]
(if (zero? i)
next
(recur (dec i) next (+ cur next))))))
(defn fibs []
(map fib (iterate inc 0)))
(defn fibs-until [max]
(take-while #(< % max) (fibs)))
(defn solution []
(apply + (filter even? (fibs-until 4000000))))
(println (solution))
I'm beginning to learn Clojure and this is one of my first exercices. I'd like to have some feedback from more experienced Clojure programmers.
This particular problem has already been posted here before, but the community is ok with me to post it again, so I can have feedback on my own implementation.
I tried to keep things simple, with each function having its own responsibility so they can be reused in other contexts. Here it is:
(ns marcofiset.euler.problem2)
(defn fib [n]
(if (< n 0)
(throw (Exception. "Can't compute fib of n < 0"))
(loop [i n cur 1 next 1]
(if (zero? i)
next
(recur (dec i) next (+ cur next))))))
(defn fibs []
(map fib (iterate inc 0)))
(defn fibs-until [max]
(take-while #(< % max) (fibs)))
(defn solution []
(apply + (filter even? (fibs-until 4000000))))
(println (solution))
I'm beginning to learn Clojure and this is one of my first exercices. I'd like to have some feedback from more experienced Clojure programmers.
This particular problem has already been posted here before, but the community is ok with me to post it again, so I can have feedback on my own implementation.
I tried to keep things simple, with each function having its own responsibility so they can be reused in other contexts. Here it is:
(ns marcofiset.euler.problem2)
(defn fib [n]
(if (< n 0)
(throw (Exception. "Can't compute fib of n < 0"))
(loop [i n cur 1 next 1]
(if (zero? i)
next
(recur (dec i) next (+ cur next))))))
(defn fibs []
(map fib (iterate inc 0)))
(defn fibs-until [max]
(take-while #(< % max) (fibs)))
(defn solution []
(apply + (filter even? (fibs-until 4000000))))
(println (solution))
I'm beginning to learn Clojure and this is one of my first exercices. I'd like to have some feedback from more experienced Clojure programmers.
This particular problem has already been posted here before, but the community is ok with me to post it again, so I can have feedback on my own implementation.
I tried to keep things simple, with each function having its own responsibility so they can be reused in other contexts. Here it is:
(ns marcofiset.euler.problem2)
(defn fib [n]
(if (< n 0)
(throw (Exception. "Can't compute fib of n < 0"))
(loop [i n cur 1 next 1]
(if (zero? i)
next
(recur (dec i) next (+ cur next))))))
(defn fibs []
(map fib (iterate inc 0)))
(defn fibs-until [max]
(take-while #(< % max) (fibs)))
(defn solution []
(apply + (filter even? (fibs-until 4000000))))
(println (solution))
This particular problem has already been posted here before, but the community is ok with me to post it again, so I can have feedback on my own implementation.
I tried to keep things simple, with each function having its own responsibility so they can be reused in other contexts. Here it is:
(ns marcofiset.euler.problem2)
(defn fib [n]
(if (< n 0)
(throw (Exception. "Can't compute fib of n < 0"))
(loop [i n cur 1 next 1]
(if (zero? i)
next
(recur (dec i) next (+ cur next))))))
(defn fibs []
(map fib (iterate inc 0)))
(defn fibs-until [max]
(take-while #(< % max) (fibs)))
(defn solution []
(apply + (filter even? (fibs-until 4000000))))
(println (solution))
I'm beginning to learn Clojure and this is one of my first exercices. I'd like to have some feedback from more experienced Clojure programmers.