Last active
April 20, 2022 12:02
-
-
Save Crowbrammer/93c8c0c2291bb74b3bde18d71850e41e to your computer and use it in GitHub Desktop.
Eviscerate sequences
This file contains 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
;; drop-take | |
(defn eviscerate-sequence | |
([c i] (eviscerate-sequence c i i)) | |
([c st end] (concat (take st c) (drop (inc end) c)))) | |
;; loop-recur | |
(defn eviscerate-sequence | |
([c i] (eviscerate-sequence c i i)) | |
([c start end] | |
(let [c (apply list c)] | |
(loop [new-c [] i 0] | |
; base case: i exceeds the length of c, 0-index | |
(if (>= i (count c)) | |
new-c | |
; recursive case: i less than length of c | |
; is the index = an s or e, or between the s or e? skip | |
; not? Add the element. | |
(if (or (< i start) (> i end)) | |
(recur (conj new-c (nth c i)) (inc i)) | |
(recur new-c (inc i)))))))) | |
;; lazy recursion | |
(defn eviscerate-sequence | |
([c i] (eviscerate-sequence c i i)) | |
([c start end] (eviscerate-sequence c start end 0)) | |
([c start end i] | |
; is the index greater than the count of c? end it | |
(if (>= i (count c)) | |
nil | |
; is the index = an s or e, or between the s or e? call this again with an inc'ed end. | |
; else return the current thing then everything else | |
(if (or (< i (dec start)) (> i end)) | |
(lazy-seq (cons (nth c i) (eviscerate-sequence c start end (inc i)))) | |
(lazy-seq (cons (nth c i) (eviscerate-sequence c start end (inc end)))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment