« Back to Index

Clojure Homework

View original Gist on GitHub

Clojure Homework.clj

; Output required...
; [[1] [2 3] [4 5]]

(def coll [1 2 3 4 5])

(defn is_even? [collection iteration]
  (if (even? (get collection iteration))
    true
    false))

(defn solution [collection starting_point]
  (loop [acc [] iteration 0 prev_even starting_point]
    (if (> iteration (count collection))
      acc
      (recur
        (if prev_even
          (assoc-in acc [(dec iteration) (dec (count (dec iteration)))] (get collection iteration))
          (assoc acc iteration [(get collection iteration)]))
        (inc iteration)
        (is_even? collection iteration)))))

(solution coll (is_even? coll 0))