Christoph wants to teach filter some vocabulary.
filter
out irrelevant entries then reduce
on just thosereduce
becomes trivial.filter
.?
. Eg. some?
, contains?
, every?
(spans-midnight? start-timestamp end-timestamp)
(filter #(spans-midnight? (:start %) (:end %)) entries)
filter
.
(spans-midnight? entry)
(filter spans-midnight? entries)
(weekend? entry)
(filter weekend? entries)
weekend?
with a simpler predicate: (day-of-week? weekday entry)
partial
.weekend?
function is a simple or
of calls to day-of-week?
(day-of-week entry)
that returns the day.day-of-week?
but also for any other logic that needs to pull out the day.weekday?
predicate becomes trivial: (not (weekend? entry))
(partial day-of-week? :sunday)
, etc.(filter (partial day-of-week? :sunday) entries)
:date
key, so the same day-of-week?
predicate works on both.Related episodes:
Clojure in this episode:
filter
reduce
partial
or
Code sample from this episode:
(ns time.week-04
(:require
[time.week-03 :as week-03]
[java-time :as jt]))
; Helper for loading up time entries
(defn log-times
[filename]
(->> (week-03/lines filename)
(week-03/times)))
; Extractors
(defn day-of-week
[entry]
(jt/day-of-week (-> entry :date)))
; Predicates
(defn spans-midnight?
[entry]
(not= (jt/local-date (:start entry)) (jt/local-date (:end entry))))
(defn day-of-week?
[day entry]
(= (day-of-week entry) (jt/day-of-week day)))
(defn weekend?
[entry]
(or (day-of-week? :saturday entry)
(day-of-week? :sunday entry)))
(defn weekday?
[entry]
(not (weekend? entry)))
; Aggregations
(defn total-minutes
[entries]
(->> entries
(map :minutes)
(reduce +)))
(comment
(->> (log-times "time-log.txt")
(filter spans-midnight?))
(->> (log-times "time-log.txt")
(filter (partial day-of-week? :wednesday)))
(->> (log-times "time-log.txt")
(filter weekend?))
(->> (log-times "time-log.txt")
(filter weekday?)
(total-minutes))
)