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.
master
Basil L. Contovounesios 4 years ago
parent 0c49a33a61
commit c62dfb0b77
No known key found for this signature in database
GPG Key ID: 205AB54A5D5D8CFF
  1. 1
      NEWS.md
  2. 11
      README.md
  3. 14
      dash.el
  4. 15
      dash.texi
  5. 11
      dev/examples.el

@ -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

@ -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)`

@ -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

@ -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

@ -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))

Loading…
Cancel
Save