From 386ec0a14b9c20b2444855a08db8ca55cc45065a Mon Sep 17 00:00:00 2001 From: Johan Andersson Date: Sun, 19 Jan 2014 18:08:12 +0100 Subject: [PATCH] Add -same-items? function. The function returns true if both lists includes the same items, no matter of the order of the items. --- README.md | 13 +++++++++++++ dash.el | 14 ++++++++++++++ dev/examples.el | 9 ++++++++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 83ca63e..fd3c632 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,7 @@ Include this in your emacs settings to get syntax highlighting: * [-none?](#-none-pred-list) `(pred list)` * [-only-some?](#-only-some-pred-list) `(pred list)` * [-contains?](#-contains-list-element) `(list element)` +* [-same-items?](#-same-items-list-list2) `(list list2)` ### Partitioning @@ -620,6 +621,18 @@ or with `-compare-fn` if that's non-nil. (-contains? '(1 2 3) 4) ;; => nil ``` +#### -same-items? `(list list2)` + +Return true if `list` and `list2` has the same items. + +The order of the elements in the lists does not matter. + +```cl +(-same-items? '(1 2 3) '(1 2 3)) ;; => t +(-same-items? '(1 2 3) '(3 2 1)) ;; => t +(-same-items? '(1 2 3) '(1 2 3 4)) ;; => nil +``` + ## Partitioning diff --git a/dash.el b/dash.el index b999df5..ae73bf3 100644 --- a/dash.el +++ b/dash.el @@ -964,6 +964,18 @@ or with `-compare-fn' if that's non-nil." (defalias '-contains-p '-contains?) +(defun -same-items? (list list2) + "Return true if LIST and LIST2 has the same items. + +The order of the elements in the lists does not matter." + (let ((length-a (length list)) + (length-b (length list2))) + (and + (= length-a length-b) + (= length-a (length (-intersection list list2)))))) + +(defalias '-same-items-p '-same-items?) + (defun -sort (comparator list) "Sort LIST, stably, comparing elements using COMPARATOR. Returns the sorted list. LIST is NOT modified by side effects. @@ -1284,6 +1296,8 @@ structure such as plist or alist." "-difference" "-contains?" "-contains-p" + "-same-items?" + "-same-items-p" "-sort" "--sort" "-repeat" diff --git a/dev/examples.el b/dev/examples.el index 792883a..f1b85bb 100644 --- a/dev/examples.el +++ b/dev/examples.el @@ -226,7 +226,14 @@ (-contains? '(1 2 3) 2) => t (-contains? '(1 2 3) 4) => nil (-contains? '() 1) => nil - (-contains? '() '()) => nil)) + (-contains? '() '()) => nil) + + (defexamples -same-items? + (-same-items? '(1 2 3) '(1 2 3)) => t + (-same-items? '(1 2 3) '(3 2 1)) => t + (-same-items? '(1 2 3) '(1 2 3 4)) => nil + (-same-items? '((a . 1) (b . 2)) '((a . 1) (b . 2))) => t + (-same-items? '(1 2 3) '(2 3 1)) => t)) (def-example-group "Partitioning" nil (defexamples -split-at