Advertisement
CLooker

Untitled

Dec 31st, 2017
1,982
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;;http://www.4clojure.com/problem/41
  2. ;;Write a function which drops every Nth item from a sequence.
  3. ;;(= (__ [1 2 3 4 5 6 7 8] 3) [1 2 4 5 7 8])
  4. ;;(= (__ [:a :b :c :d :e :f] 2) [:a :c :e])
  5. ;;(= (__ [1 2 3 4 5 6] 4) [1 2 3 5 6])
  6. ;;(= (__ [1 2 3 4 5 6 7 8] 3) [1 2 4 5 7 8])
  7. (fn
  8.   [base-seq remove-nth]
  9.   (let [g (fn
  10.             [base-seq remove-nth]
  11.             (loop [x (- remove-nth 1)
  12.                   result []]
  13.               (if (< x (count base-seq))
  14.                 (recur (+ x remove-nth) (conj result x))
  15.                 result)))
  16.         f (fn
  17.             [base-seq remove-nth]
  18.               (keep-indexed
  19.                 (fn
  20.                   [index item]
  21.                   (if (contains? (set remove-nth) index)
  22.                     nil
  23.                     item))
  24.                 base-seq))]
  25.     (f base-seq (g base-seq remove-nth))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement