From 50f0bd3dadf253edb7a043589263f08b3a3b1f10 Mon Sep 17 00:00:00 2001 From: William Carroll Date: Thu, 16 Jan 2020 01:16:58 +0000 Subject: Support list/dedupe-adjacent Support a function to deduplicate adjacent elements in a list. Also tracks additional work with TODOs. --- configs/shared/.emacs.d/wpc/list.el | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/configs/shared/.emacs.d/wpc/list.el b/configs/shared/.emacs.d/wpc/list.el index bcab3df09cde..1ea5c18a7274 100644 --- a/configs/shared/.emacs.d/wpc/list.el +++ b/configs/shared/.emacs.d/wpc/list.el @@ -177,6 +177,20 @@ Be leery of using this with things like alists. Many data structures in Elisp "Return t if X is in XS using `equal'." (-contains? xs x)) +;; TODO: Support dedupe. +;; TODO: Should we call this unique? Or distinct? + +;; TODO: Add tests. +(defun list/dedupe-adjacent (xs) + "Return XS without adjacent duplicates." + (prelude/assert (not (list/empty? xs))) + (list/reduce (list (list/first xs)) + (lambda (x acc) + (if (equal x (list/first acc)) + acc + (list/cons x acc))) + xs)) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Tests ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -- cgit 1.4.1