Advertisement
Guest User

Untitled

a guest
Dec 16th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns paging-task.ad-hoc)
  2.  
  3. ;;; idea:
  4. ;;; drop fixed number of points (1,2,3, current-1,current,current+1,last-2,last-1,last)
  5. ;;; sort
  6. ;;; remove duplicated
  7. ;;; insert separator
  8.  
  9. (defn- insert-separator [separator coll]
  10.   (loop [result []
  11.          c coll]
  12.     (if (empty? c)
  13.       result
  14.       (if (empty? (rest c))
  15.         (conj result (first c))
  16.         (if (= (inc (first c)) (second c))
  17.           (recur (conj result (first c)) (rest c))
  18.           (recur (conj (conj result (first c)) separator) (rest c)))))))
  19.  
  20. (defn get-pages [total current]
  21.   (let [intervals [[1 (min total 3)]
  22.                    [(max 1 (dec current)) (min total (inc current))]
  23.                    [(max 1 (- total 2)) total]]
  24.         points (mapcat (fn [[a b]] (range a (inc b))) intervals)]
  25.     (->> points
  26.          (sort)
  27.          (distinct)
  28.          (insert-separator "...")
  29.          (map str))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement