r/mit6824clojure • • 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

2 comments sorted by

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.

1

u/ErnestoGarciaGarcia Nov 21 '20

The assert serves both as a runtime check as well as precondition doc. If the precondition is not met, the function will not behave well.

I wouldn't expect client code to deal specifically with an assertion failure. Client code must take care of always fulfilling the assertion.