Last active
March 6, 2019 20:11
-
-
Save JulienRouse/3266572b1bb93d559a662f146d8b447d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;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))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Old version was using
keep-indexed
but I couldn't make it work with nil values.