Skip to content

Instantly share code, notes, and snippets.

@PEZ
Last active October 28, 2024 21:56
Show Gist options
  • Save PEZ/92a4bd13aaa6bffb80d7724de2c8e64d to your computer and use it in GitHub Desktop.
Save PEZ/92a4bd13aaa6bffb80d7724de2c8e64d to your computer and use it in GitHub Desktop.
Compress a Sequence – Rich 4Clojure Problem 30 – See: https://github.com/PEZ/rich4clojure
(ns rich4clojure.easy.problem-030
(:require [hyperfiddle.rcf :refer [tests]]))
;; = Compress a Sequence =
;; By 4Clojure user: dbyrne
;; Difficulty: Easy
;; Tags: [seqs]
;;
;; Write a function which removes consecutive duplicates
;; from a sequence.
(def __ :tests-will-fail)
(comment
)
(tests
(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]))
;; To participate, fork:
;; https://github.com/PEZ/rich4clojure
;; Post your solution below, please!
@amiskov
Copy link

amiskov commented Aug 28, 2021

(def __ (fn remove-consecutive
          ([s] (remove-consecutive s '()))
          ([s acc]
           (if (empty? s)
             (reverse acc)
             (if (= (first s) (first acc))
               (remove-consecutive (rest s) acc)
               (remove-consecutive (rest s) (conj acc (first s))))))))

(def __' (fn [s]
  (->> s
       (reduce
         #(if (= %2 (first %1))
            %1
            (conj %1 %2))
         '())
       (reverse))))

(def __'' #(map first (partition-by identity %)))

@szalai1
Copy link

szalai1 commented Sep 6, 2021

(fn [s]
          (loop [prev-char (first s)
                 current-char (second s)
                 s (nthrest s 2)
                 result [prev-char]]
            (if (empty? s)
              (if (= prev-char current-char)
                result
                (conj result current-char))
                (if (= prev-char current-char)
                  (recur prev-char (first s) (rest s) result)
                  (recur current-char (first s) (rest s) (conj result current-char))))))

@status203
Copy link

(defn dedup
  [s]
  (if (empty? s)
    '()
    (cons (first s)
          (->> s
               (partition 2 1)
               (filter #(not= (first %) (second %)))
               (map second)))))

(def __ dedup)

@StanTheBear
Copy link

(def __ #(map first (partition-by
identity %)))

with some reference to ClojureDocs.

Copy link

ghost commented Nov 2, 2023

(def __ #(loop [l (seq %)
                res []]
           (if (empty? l)
             (seq res)
             (if (and (not-empty res) (= (last res) (first l)))
               (recur (rest l) res)
               (recur (rest l) (conj res (first l)))))))

@Herrmelit
Copy link

  (defn __ [l]
    (filter some?
            (map-indexed
             (fn [idx value] (if (not= value (get l (+ idx 1))) value)) l)))

@jsebdev
Copy link

jsebdev commented May 27, 2024

(def __ (fn [s]
          (->> s
               (reduce
                (fn [acc curr]
                  (if (= (last acc) curr)
                    acc
                    (concat acc (list curr))))
                (list)))))

@oezg
Copy link

oezg commented Oct 28, 2024

(defn consecutive-duplicates [coll]
  (partition-by identity coll))

(defn get-firsts [coll]
  (map first coll))

(def __ #(-> % consecutive-duplicates get-firsts))

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