Skip to content

Instantly share code, notes, and snippets.

@JulienRouse
Last active March 6, 2019 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JulienRouse/3266572b1bb93d559a662f146d8b447d to your computer and use it in GitHub Desktop.
Save JulienRouse/3266572b1bb93d559a662f146d8b447d to your computer and use it in GitHub Desktop.
;Attempt at the puzzle of issue 316 of Eric Normand newsletter.
;https://purelyfunctional.tv/issues/purelyfunctional-tv-newsletter-316-avoid-licensing-and-support-issues-with-the-right-jdk-for-you/
;Drop every nth element from a sequence
;The problem is simple: write a function that takes a number n and a sequence.
;The function returns a new sequence with every nth element removed.
;(drop-every 3 [:a :b :c :d :e :f :g])
;=> (:a :b :d :e :g)
;TODO The :when test is ugly, got a better idea?
(defn drop-every
[n seq]
(let [seq-indexed (map-indexed #(vec [%1 %2]) seq)]
(for [[index val] seq-indexed
:when (or (< (inc index) n)
(not (= 0 (mod (inc index) n))))]
val)))
@JulienRouse
Copy link
Author

Old version was using keep-indexed but I couldn't make it work with nil values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment