Skip to content

Instantly share code, notes, and snippets.

@PetrGlad
Created September 7, 2017 10:50
Show Gist options
  • Save PetrGlad/7a5b3bda2adcc16ebc1314cb94237d04 to your computer and use it in GitHub Desktop.
Save PetrGlad/7a5b3bda2adcc16ebc1314cb94237d04 to your computer and use it in GitHub Desktop.
(defn page-offset
"Return [next-group, next-offset] in sequence
given offset of first item in required range.
group-fn should not return nil."
[group-fn offset items]
{:pre [(<= 0 offset)]}
;; (Do not force too much items from source sequence here)
(let [split-pos offset]
(when (seq (drop split-pos items))
(->> (take (inc split-pos) items)
(map group-fn)
(reduce
(fn [[v idx] x]
(if (= v x)
[v (inc idx)]
[x 0]))
[nil 0])))))
(facts "page-offset basic"
(fact (api/page-offset div10 2 [0 1])
=> nil)
(fact (api/page-offset div10 1 [])
=> nil)
(fact (api/page-offset div10 2 [0 1 10 11])
=> [1 0])
(fact (api/page-offset div10 3 [0 11 22 23 33])
=> [2 1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment