about summary refs log tree commit diff
path: root/emacs/.emacs.d/wpc
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2020-02-14T15·34+0000
committerWilliam Carroll <wpcarro@gmail.com>2020-02-14T15·34+0000
commitfa779ab7bfd7bd0b9decaf2501265dbf2e28930b (patch)
treebfd873b96e062d9f1e2d0c6712ed843ef0f1869b /emacs/.emacs.d/wpc
parent3677a7a39e47bd9d295ae63e3a40e6cf98947506 (diff)
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.
Diffstat (limited to 'emacs/.emacs.d/wpc')
-rw-r--r--emacs/.emacs.d/wpc/list.el5
1 files changed, 5 insertions, 0 deletions
diff --git a/emacs/.emacs.d/wpc/list.el b/emacs/.emacs.d/wpc/list.el
index 5a63c8bd94e0..83c4c5ccdc42 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?