Ep 032: Call Me Lazy

Play Episode

Christoph finds map doesn't let him be lazy enough.

Related episodes:

Clojure in this episode:

Code sample from this episode:

(ns devops.week-04
  (:require
    [devops.week-01 :refer [parse-line]]
    [devops.week-02 :refer [process-log]]
    [devops.week-03 :refer [sprinkle-errors-by-type]]
    ))

(defn sprinkle-error-seq
  [lines]
  (lazy-seq
    (when (seq lines)
      (let [[first-line second-line & tail] lines
            [_whole donut-id] (some->> first-line :log/message (re-matches #"failed to add sprinkle to donut (\d+)"))
            [_whole error] (some->> second-line :log/message (re-matches #"sprinkle fail reason: (.*)"))]
        (if (and donut-id error)
          (cons (merge first-line
                       {:kind :sprinkle
                        :sprinkle/donut-id donut-id
                        :sprinkle/error error})
                (sprinkle-error-seq tail))
          (sprinkle-error-seq (next lines)))))))


(comment
  (process-log "sample.log" #(->> % (map parse-line) sprinkle-error-seq doall))
  (process-log "sample.log" #(->> % (map parse-line) sprinkle-error-seq sprinkle-errors-by-type))
  )