emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/dash c62dfb0b77 1/3: Fix -pad when fed zero input lists


From: ELPA Syncer
Subject: [elpa] externals/dash c62dfb0b77 1/3: Fix -pad when fed zero input lists
Date: Wed, 8 Jun 2022 15:57:34 -0400 (EDT)

branch: externals/dash
commit c62dfb0b77c6552a2534985ecb1cebb8b783076d
Author: Basil L. Contovounesios <contovob@tcd.ie>
Commit: Basil L. Contovounesios <contovob@tcd.ie>

    Fix -pad when fed zero input lists
    
    * NEWS.md (2.20.0): Mention fix.
    * README.md:
    * dash.texi: Regenerate docs.
    
    * dash.el (-pad): Don't barf on zero input lists.  Fix docstring.
    Mark as pure and side-effect-free.  Simplify.
    * dev/examples.el (-pad): Extend tests.
---
 NEWS.md         |  1 +
 README.md       | 11 +++++++----
 dash.el         | 14 +++++++++-----
 dash.texi       | 15 +++++++++------
 dev/examples.el | 11 +++++++++--
 5 files changed, 35 insertions(+), 17 deletions(-)

diff --git a/NEWS.md b/NEWS.md
index 6d2553f7c7..ab08c1ee76 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -12,6 +12,7 @@ See the end of the file for license conditions.
 
 - Fixed a regression from `2.18` in `-take` that caused it to
   prematurely signal an error on improper lists (#393).
+- The function `-pad` can now be called with zero lists as arguments.
 - The functions `-union`, `-intersection`, and `-difference` now
   return proper sets, without duplicate elements (#397).
 - The functions `-same-items?` and `-permutations` now work on
diff --git a/README.md b/README.md
index f98ae44829..64789b69d1 100644
--- a/README.md
+++ b/README.md
@@ -2063,13 +2063,16 @@ from the beginning.
 
 #### -pad `(fill-value &rest lists)`
 
-Appends `fill-value` to the end of each list in `lists` such that they
-will all have the same length.
+Pad each of `lists` with `fill-value` until they all have equal lengths.
+
+Ensure all `lists` are as long as the longest one by repeatedly
+appending `fill-value` to the shorter lists, and return the
+resulting `lists`.
 
 ```el
 (-pad 0 ()) ;; => (nil)
-(-pad 0 '(1)) ;; => ((1))
-(-pad 0 '(1 2 3) '(4 5)) ;; => ((1 2 3) (4 5 0))
+(-pad 0 '(1 2) '(3 4)) ;; => ((1 2) (3 4))
+(-pad 0 '(1 2) '(3 4 5 6) '(7 8 9)) ;; => ((1 2 0 0) (3 4 5 6) (7 8 9 0))
 ```
 
 #### -table `(fn &rest lists)`
diff --git a/dash.el b/dash.el
index 04c3ca1d5e..ed3a15131f 100644
--- a/dash.el
+++ b/dash.el
@@ -1704,11 +1704,15 @@ from the beginning."
     (nconc newlist newlist)))
 
 (defun -pad (fill-value &rest lists)
-  "Appends FILL-VALUE to the end of each list in LISTS such that they
-will all have the same length."
-  (let* ((annotations (-annotate 'length lists))
-         (n (-max (-map 'car annotations))))
-    (--map (append (cdr it) (-repeat (- n (car it)) fill-value)) annotations)))
+  "Pad each of LISTS with FILL-VALUE until they all have equal lengths.
+
+Ensure all LISTS are as long as the longest one by repeatedly
+appending FILL-VALUE to the shorter lists, and return the
+resulting LISTS."
+  (declare (pure t) (side-effect-free t))
+  (let* ((lens (mapcar #'length lists))
+         (maxlen (apply #'max 0 lens)))
+    (--map (append it (make-list (- maxlen (pop lens)) fill-value)) lists)))
 
 (defun -annotate (fn list)
   "Return a list of cons cells where each cell is FN applied to each
diff --git a/dash.texi b/dash.texi
index 9f2f05a5c4..a38f20fd3a 100644
--- a/dash.texi
+++ b/dash.texi
@@ -3048,8 +3048,11 @@ from the beginning.
 
 @anchor{-pad}
 @defun -pad (fill-value &rest lists)
-Appends @var{fill-value} to the end of each list in @var{lists} such that they
-will all have the same length.
+Pad each of @var{lists} with @var{fill-value} until they all have equal 
lengths.
+
+Ensure all @var{lists} are as long as the longest one by repeatedly
+appending @var{fill-value} to the shorter lists, and return the
+resulting @var{lists}.
 
 @example
 @group
@@ -3057,12 +3060,12 @@ will all have the same length.
     @result{} (nil)
 @end group
 @group
-(-pad 0 '(1))
-    @result{} ((1))
+(-pad 0 '(1 2) '(3 4))
+    @result{} ((1 2) (3 4))
 @end group
 @group
-(-pad 0 '(1 2 3) '(4 5))
-    @result{} ((1 2 3) (4 5 0))
+(-pad 0 '(1 2) '(3 4 5 6) '(7 8 9))
+    @result{} ((1 2 0 0) (3 4 5 6) (7 8 9 0))
 @end group
 @end example
 @end defun
diff --git a/dev/examples.el b/dev/examples.el
index d858e203ce..0b1ff173c5 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -1470,10 +1470,17 @@ related predicates."
 
   (defexamples -pad
     (-pad 0 '()) => '(())
+    (-pad 0 '(1 2) '(3 4)) => '((1 2) (3 4))
+    (-pad 0 '(1 2) '(3 4 5 6) '(7 8 9)) => '((1 2 0 0) (3 4 5 6) (7 8 9 0))
+    (-pad 0) => ()
+    (-pad 0 () ()) => '(() ())
     (-pad 0 '(1)) => '((1))
+    (-pad 0 '(1) '(1)) => '((1) (1))
     (-pad 0 '(1 2 3) '(4 5)) => '((1 2 3) (4 5 0))
-    (-pad nil '(1 2 3) '(4 5) '(6 7 8 9 10)) => '((1 2 3 nil nil) (4 5 nil nil 
nil) (6 7 8 9 10))
-    (-pad 0 '(1 2) '(3 4)) => '((1 2) (3 4)))
+    (-pad nil ()) => '(())
+    (-pad nil () ()) => '(() ())
+    (-pad nil '(nil nil) '(nil) '(nil nil nil nil nil))
+    => '((nil nil nil nil nil) (nil nil nil nil nil) (nil nil nil nil nil)))
 
   (defexamples -table
     (-table '* '(1 2 3) '(1 2 3)) => '((1 2 3) (2 4 6) (3 6 9))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]