; 30 - Write a function which removes consecutive duplicates from a sequence.
;; * *
; (= (apply str (__ "Leeeeeerrroyyy")) "Leroy")
; (= (__ [1 1 2 3 3 2 2 3]) '(1 2 3 2 3))
; (= (__ [[1 2] [1 2] [3 4] [1 2]]) '([1 2] [3 4] [1 2]))
#_(fn [s]
(loop [remainder s, compressed [], final nil]
(if-let [[x & xs] remainder]
(if (not= x final)
(recur xs (conj compressed x) x)
(recur xs compressed final))
compressed)))
#_(defn fc[l]
(loop [l l acc [] final nil]
(if-let [[x & xs] l]
(cond
(not= x final)
(recur xs (conj acc x) x)
:else
(recur xs acc final))
acc)))
#_(defn fc [l]
(loop [l l acc [] final nil]
(if-let [[x & xs] l]
(cond
(not= x final)
(recur xs (conj acc x) x)
:else
(recur xs acc final))
acc)))
;; 31 * - Write a function which packs consecutive duplicates into sub-lists.
; (= (__ [1 1 2 1 1 1 3 3]) '((1 1) (2) (1 1 1) (3 3)))
; (= (__ [:a :a :b :b :c]) '((:a :a) (:b :b) (:c)))
; (= (__ [[1 2] [1 2] [3 4]]) '(([1 2] [1 2]) ([3 4])))
#_(#(partition-by identity %))
;; 41 ** - Write a function which drops every Nth item from a sequence.
; (= (__ [1 2 3 4 5 6 7 8] 3) [1 2 4 5 7 8])
; (= (__ [:a :b :c :d :e :f] 2) [:a :c :e])
; (= (__ [1 2 3 4 5 6] 4) [1 2 3 5 6])
(def l [1 2 3 4 5 6 7 8])
#_(fn fc [l n]
(defn helper
([l n] (helper l n 1))
([l n i]
(cond
(empty? l) '()
(zero? (rem i n)) (helper (rest l) n (inc i))
:else
(conj (helper (rest l) n (inc i)) (first l))
)))
(helper l n))
#_(defn fc[l n]
(loop [l l acc [] i 1]
(cond (empty? l) acc
(== 0 (rem i n))
(recur (rest l) acc (inc i))
:else
(recur (rest l) (conj acc (first l)) (inc i)))))
#_(defn fc
([l n](fc l n 1))
([l n i]
(cond
(empty? l) '()
(== 0 (rem i n))
(fc (rest l) n (inc i))
:else
(cons (first l) (fc (rest l) n (inc i))))))
; (println
; (fc l 3)
; )
;; 33 **- Write a function which replicates each element of a sequence a variable number of times.
; (= (__ [1 2 3] 2) '(1 1 2 2 3 3))
; (= (__ [:a :b] 4) '(:a :a :a :a :b :b :b :b))
; (= (__ [4 5 6] 1) '(4 5 6))
; (= (__ [[1 2] [3 4]] 2) '([1 2] [1 2] [3 4] [3 4]))
; (= (__ [44 33] 2) [44 44 33 33])
;;**
#_(mapcat #(repeat 2 %) l2)
#_(defn fn[l n]
(mapcat #(repeat n %) l2))
#_(defn fc[l n]
(if (empty? l) '()
(concat (repeat n (first l))
(fc (rest l) n))))
#_(fn rec [ls n]
(lazy-seq
(if (empty? ls) ()
(concat
(repeat n (first ls))
(rec (rest ls) n)))))
To embed this program on your website, copy the following code and paste it into your website's HTML: