branch: externals/dash commit 7cc01498a27d63ff4e0f3cd19ce7a53397fb533d Author: Matus Goljer <matus.gol...@gmail.com> Commit: Matus Goljer <matus.gol...@gmail.com>
[#99] Add -take-last and -drop-last --- README.md | 29 +++++++++++++++++++++++++++++ dash.el | 22 ++++++++++++++++++++-- dev/examples.el | 12 ++++++++++++ 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3f54228..1338ddc 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,9 @@ Functions returning a sublist of the original list. * [-non-nil](#-non-nil-list) `(list)` * [-slice](#-slice-list-from-optional-to-step) `(list from &optional to step)` * [-take](#-take-n-list) `(n list)` +* [-take-last](#-take-last-n-list) `(n list)` * [-drop](#-drop-n-list) `(n list)` +* [-drop-last](#-drop-last-n-list) `(n list)` * [-take-while](#-take-while-pred-list) `(pred list)` * [-drop-while](#-drop-while-pred-list) `(pred list)` * [-select-by-indices](#-select-by-indices-indices-list) `(indices list)` @@ -535,20 +537,47 @@ section is returned. Defaults to 1. Return a new list of the first `n` items in `list`, or all items if there are fewer than `n`. +See also: [`-take-last`](#-take-last-n-list) + ```el (-take 3 '(1 2 3 4 5)) ;; => '(1 2 3) (-take 17 '(1 2 3 4 5)) ;; => '(1 2 3 4 5) ``` +#### -take-last `(n list)` + +Return the last `n` items of `list` in order. + +See also: [`-take`](#-take-n-list) + +```el +(-take-last 3 '(1 2 3 4 5)) ;; => '(3 4 5) +(-take-last 17 '(1 2 3 4 5)) ;; => '(1 2 3 4 5) +(-take-last 1 '(1 2 3 4 5)) ;; => '(5) +``` + #### -drop `(n list)` Return the tail of `list` without the first `n` items. +See also: [`-drop-last`](#-drop-last-n-list) + ```el (-drop 3 '(1 2 3 4 5)) ;; => '(4 5) (-drop 17 '(1 2 3 4 5)) ;; => '() ``` +#### -drop-last `(n list)` + +Remove the last `n` items of `list` and return a copy. + +See also: [`-drop`](#-drop-n-list) + +```el +(-drop-last 3 '(1 2 3 4 5)) ;; => '(1 2) +(-drop-last 17 '(1 2 3 4 5)) ;; => '() +``` + #### -take-while `(pred list)` Return a new list of successive items from `list` while (`pred` item) returns a non-nil value. diff --git a/dash.el b/dash.el index 87983fd..41651a1 100644 --- a/dash.el +++ b/dash.el @@ -673,7 +673,9 @@ section is returned. Defaults to 1." (nreverse new-list))) (defun -take (n list) - "Return a new list of the first N items in LIST, or all items if there are fewer than N." + "Return a new list of the first N items in LIST, or all items if there are fewer than N. + +See also: `-take-last'" (let (result) (--dotimes n (when list @@ -681,7 +683,23 @@ section is returned. Defaults to 1." (!cdr list))) (nreverse result))) -(defalias '-drop 'nthcdr "Return the tail of LIST without the first N items.") +(defun -take-last (n list) + "Return the last N items of LIST in order. + +See also: `-take'" + (copy-sequence (last list n))) + +(defalias '-drop 'nthcdr + "Return the tail of LIST without the first N items. + +See also: `-drop-last'") + +(defun -drop-last (n list) + "Remove the last N items of LIST and return a copy. + +See also: `-drop'" + ;; No alias because we don't want magic optional argument + (butlast list n)) (defmacro --take-while (form list) "Anaphoric form of `-take-while'." diff --git a/dev/examples.el b/dev/examples.el index 8bf6a2e..92f8861 100644 --- a/dev/examples.el +++ b/dev/examples.el @@ -168,10 +168,22 @@ new list." (-take 3 '(1 2 3 4 5)) => '(1 2 3) (-take 17 '(1 2 3 4 5)) => '(1 2 3 4 5)) + (defexamples -take-last + (-take-last 3 '(1 2 3 4 5)) => '(3 4 5) + (-take-last 17 '(1 2 3 4 5)) => '(1 2 3 4 5) + (-take-last 1 '(1 2 3 4 5)) => '(5) + (let ((l '(1 2 3 4 5))) + (setcar (-take-last 2 l) 1) + l) => '(1 2 3 4 5)) + (defexamples -drop (-drop 3 '(1 2 3 4 5)) => '(4 5) (-drop 17 '(1 2 3 4 5)) => '()) + (defexamples -drop-last + (-drop-last 3 '(1 2 3 4 5)) => '(1 2) + (-drop-last 17 '(1 2 3 4 5)) => '()) + (defexamples -take-while (-take-while 'even? '(1 2 3 4)) => '() (-take-while 'even? '(2 4 5 6)) => '(2 4)