refactor(wpcarro/emacs): Remove list.el's dep on dash.el 🎉

Still pruning the dependency tree. Thank you, seq.el, from DWIMing.

Change-Id: I797f08abe44853b9d297a99d5ba9e9bde3dcfeec
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6040
Reviewed-by: wpcarro <wpcarro@gmail.com>
Autosubmit: wpcarro <wpcarro@gmail.com>
Tested-by: BuildkiteCI
This commit is contained in:
William Carroll 2022-08-05 11:41:32 -07:00 committed by clbot
parent 42e7254ad0
commit b880fc4a14
3 changed files with 64 additions and 46 deletions

View file

@ -8,9 +8,6 @@ let
version = "1.0.0"; version = "1.0.0";
src = ./list.el; src = ./list.el;
packageRequires = packageRequires =
(with emacsPackages; [
dash
]) ++
(with depot.users.wpcarro.emacs.pkgs; [ (with depot.users.wpcarro.emacs.pkgs; [
maybe maybe
set set

View file

@ -52,9 +52,9 @@
;; Dependencies ;; Dependencies
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(require 'dash)
(require 'maybe) (require 'maybe)
(require 'set) (require 'set)
(require 'set)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Library ;; Library
@ -106,37 +106,31 @@
"Add `X' to the head of `XS'." "Add `X' to the head of `XS'."
(cons x xs)) (cons x xs))
;; map, filter, reduce (defun list-filter (p xs)
"Return a subset of XS where predicate P returned t."
;; TODO: Create function adapters like swap. (list--assert-instance xs)
;; (defun adapter/swap (f) (seq-filter p xs))
;; "Return a new function that wraps `F' and swaps the arguments."
;; (lambda (a b)
;; (funcall f b a)))
;; TODO: Make this function work.
(defun list-reduce (acc f xs)
"Return over `XS' calling `F' on an element in `XS'and `ACC'."
(-reduce-from (lambda (acc x) (funcall f x acc)) acc xs))
(defun list-map (f xs) (defun list-map (f xs)
"Call `F' on each element of `XS'." "Call `F' on each element of `XS'."
(-map f xs)) (list--assert-instance xs)
(seq-map f xs))
(defun list-reduce (acc f xs)
"Return over `XS' calling `F' on an element in `XS'and `ACC'."
(list--assert-instance xs)
(seq-reduce (lambda (acc x) (funcall f x acc)) xs acc))
(defun list-map-indexed (f xs) (defun list-map-indexed (f xs)
"Call `F' on each element of `XS' along with its index." "Call `F' on each element of `XS' along with its index."
(-map-indexed (lambda (i x) (funcall f x i)) xs))
(defun list-filter (p xs)
"Return a subset of XS where predicate P returned t."
(list-reverse (list-reverse
(list-reduce (cdr
'() (list-reduce '(0 . nil)
(lambda (x acc) (lambda (x acc)
(if (funcall p x) (let ((i (car acc))
(list-cons x acc) (result (cdr acc)))
acc)) `(,(+ 1 i) . ,(cons (funcall f x i) result))))
xs))) xs))))
(defun list-reject (p xs) (defun list-reject (p xs)
"Return a subset of XS where predicate of P return nil." "Return a subset of XS where predicate of P return nil."
@ -144,7 +138,8 @@
(defun list-find (p xs) (defun list-find (p xs)
"Return the first x in XS that passes P or nil." "Return the first x in XS that passes P or nil."
(-find p xs)) (list--assert-instance xs)
(seq-find p xs))
;; TODO: Support dedupe. ;; TODO: Support dedupe.
;; TODO: Should we call this unique? Or distinct? ;; TODO: Should we call this unique? Or distinct?
@ -164,19 +159,17 @@
"Chunk XS into lists of size N." "Chunk XS into lists of size N."
(if (> n (length xs)) (if (> n (length xs))
(list xs) (list xs)
(->> xs (let* ((xs (list-reduce '(:curr () :result ())
(list-reduce '(:curr () :result ()) (lambda (x acc)
(lambda (x acc) (let ((curr (plist-get acc :curr))
(let ((curr (plist-get acc :curr)) (result (plist-get acc :result)))
(result (plist-get acc :result))) (if (= (- n 1) (length curr))
(if (= (- n 1) (length curr)) `(:curr () :result ,(list-cons (list-reverse (list-cons x curr)) result))
`(:curr () :result ,(list-cons (list-reverse (list-cons x curr)) result)) `(:curr ,(list-cons x curr) :result
`(:curr ,(list-cons x curr) :result ,result))))) ,result)))) xs))
(funcall (lambda (xs) (curr (plist-get xs :curr))
(let ((curr (plist-get xs :curr)) (result (plist-get xs :result)))
(result (plist-get xs :result))) (list-reverse (if curr (list-cons curr result) result)))))
(if curr (list-cons curr result)) result)))
list-reverse)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Predicates ;; Predicates
@ -194,20 +187,35 @@ Be leery of using this with things like alists. Many data structures in Elisp
(defun list-all? (p xs) (defun list-all? (p xs)
"Return t if all `XS' pass the predicate, `P'." "Return t if all `XS' pass the predicate, `P'."
(-all? p xs)) (if (list-empty? xs)
t
(and (maybe-some? (funcall p (car xs)))
(list-all? p (cdr xs)))))
(defun list-any? (p xs) (defun list-any? (p xs)
"Return t if any `XS' pass the predicate, `P'." "Return t if any `XS' pass the predicate, `P'."
(-any? p xs)) (if (list-empty? xs)
nil
(or (maybe-some? (funcall p (car xs)))
(list-any? p (cdr xs)))))
(defun list-contains? (x xs) (defun list-contains? (x xs)
"Return t if X is in XS using `equal'." "Return t if X is in XS using `equal'."
(maybe-some? (-contains? xs x))) (list--assert-instance xs)
(maybe-some? (seq-contains xs x)))
(defun list-xs-distinct-by? (f xs) (defun list-xs-distinct-by? (f xs)
"Return t if all elements in XS are distinct after applying F to each." "Return t if all elements in XS are distinct after applying F to each."
(= (length xs) (= (length xs)
(->> xs (-map f) set-from-list set-count))) (set-count (set-from-list (list-map f xs)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Helpers
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun list--assert-instance (xs)
(unless (list-instance? xs)
(error (format "Assertion failed: argument is not a list: %s" xs))))
(provide 'list) (provide 'list)
;;; list.el ends here ;;; list.el ends here

View file

@ -52,3 +52,16 @@
(list-chunk 3 '(1 2 3 4 5 6)))) (list-chunk 3 '(1 2 3 4 5 6))))
(should (equal '((1 2) (3 4) (5 6)) (should (equal '((1 2) (3 4) (5 6))
(list-chunk 2 '(1 2 3 4 5 6))))) (list-chunk 2 '(1 2 3 4 5 6)))))
(ert-deftest list-find ()
(should (equal 2 (list-find (lambda (x) (= 2 x)) '(1 2 3 4)))))
(ert-deftest list-all? ()
(should (equal t (list-all? (lambda (x) (= 2 x)) nil)))
(should (null (list-all? (lambda (x) (= 2 x)) '(1 2 3))))
(should (equal t (list-all? (lambda (x) (= 2 x)) '(2 2 2 2)))))
(ert-deftest list-any? ()
(should (null (list-any? (lambda (x) (= 2 x)) nil)))
(should (equal t (list-any? (lambda (x) (= 2 x)) '(1 2 3))))
(should (null (list-any? (lambda (x) (= 4 x)) '(1 2 3)))))