[#99] Add -take-last and -drop-last

master
Matus Goljer 10 years ago
parent 3e7163a24e
commit 7cc01498a2
  1. 29
      README.md
  2. 22
      dash.el
  3. 12
      dev/examples.el

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

@ -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'."

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

Loading…
Cancel
Save