r/mit6824clojure • u/ErnestoGarciaGarcia • Oct 13 '20
Implementing find-first-conflict Spoiler
Reading https://phillippe.siclait.com/blog/raft-in-clojure by /u/siclait, I found function find-first-conflict. I'm proposing an alternative implementation here:
(defn find-first-conflict
[prev-index log entries]
(assert (>= prev-index -1))
(let [log-entries (drop (inc prev-index) log)
indexes (iterate inc (inc prev-index))
non-equal-indexes (map #(when (not= %1 %2) %3)
entries log-entries indexes)]
(some identity non-equal-indexes)))
4
Upvotes
1
u/TheLastSock Nov 14 '20
Seems reasonable. Why do you need the assert? What happens if that triggers? I only like to throw assertitions when i expect a caller/user of the library to have to deal with a potential error.