From fa779ab7bfd7bd0b9decaf2501265dbf2e28930b Mon Sep 17 00:00:00 2001 From: William Carroll Date: Fri, 14 Feb 2020 15:34:56 +0000 Subject: [PATCH] Support list/xs-distinct-by? Supporting a predicate to check that all elements in a list are distinct after applying a transformation function to them. --- emacs/.emacs.d/wpc/list.el | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/emacs/.emacs.d/wpc/list.el b/emacs/.emacs.d/wpc/list.el index 5a63c8bd9..83c4c5ccd 100644 --- a/emacs/.emacs.d/wpc/list.el +++ b/emacs/.emacs.d/wpc/list.el @@ -199,6 +199,11 @@ 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)) +(defun list/xs-distinct-by? (f xs) + "Return t if all elements in XS are distinct after applying F to each." + (= (length xs) + (->> xs (-map f) set/from-list set/count))) + ;; TODO: Support dedupe. ;; TODO: Should we call this unique? Or distinct?