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!
@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