Add -tails and -inits

master
Matus Goljer 8 years ago
parent e7764ea722
commit f8c8dc002c
  1. 22
      README.md
  2. 11
      dash.el
  3. 40
      dash.texi
  4. 10
      dev/examples.el

@ -208,6 +208,8 @@ Operations pretending lists are sets.
* [-intersection](#-intersection-list-list2) `(list list2)`
* [-powerset](#-powerset-list) `(list)`
* [-permutations](#-permutations-list) `(list)`
* [-inits](#-inits-list) `(list)`
* [-tails](#-tails-list) `(list)`
* [-distinct](#-distinct-list) `(list)`
### Other list operations
@ -1481,6 +1483,26 @@ Return the permutations of `list`.
(-permutations '(a b c)) ;; => '((a b c) (a c b) (b a c) (b c a) (c a b) (c b a))
```
#### -inits `(list)`
Return all non-empty prefixes of `list`.
```el
(-inits '(1 2 3 4)) ;; => '((1) (1 2) (1 2 3) (1 2 3 4))
(-inits nil) ;; => nil
(-inits '(1)) ;; => '((1))
```
#### -tails `(list)`
Return all non-empty suffixes of `list`
```el
(-tails '(1 2 3 4)) ;; => '((1 2 3 4) (2 3 4) (3 4) (4))
(-tails nil) ;; => nil
(-tails '(1)) ;; => '((1))
```
#### -distinct `(list)`
Return a new list with all duplicates removed.

@ -2075,6 +2075,17 @@ or with `-compare-fn' if that's non-nil."
(-permutations (remove x list))))
list))))
(defun -inits (list)
"Return all non-empty prefixes of LIST."
(nreverse (-map 'reverse (-tails (nreverse list)))))
(defun -tails (list)
"Return all non-empty suffixes of LIST"
(-reduce-r-from
(lambda (it acc)
(cons (cons it (car acc)) acc))
nil list))
(defun -contains? (list element)
"Return non-nil if LIST contains ELEMENT.

@ -2209,6 +2209,46 @@ Return the permutations of @var{list}.
@end example
@end defun
@anchor{-inits}
@defun -inits (list)
Return all non-empty prefixes of @var{list}.
@example
@group
(-inits '(1 2 3 4))
@result{} '((1) (1 2) (1 2 3) (1 2 3 4))
@end group
@group
(-inits nil)
@result{} nil
@end group
@group
(-inits '(1))
@result{} '((1))
@end group
@end example
@end defun
@anchor{-tails}
@defun -tails (list)
Return all non-empty suffixes of @var{list}
@example
@group
(-tails '(1 2 3 4))
@result{} '((1 2 3 4) (2 3 4) (3 4) (4))
@end group
@group
(-tails nil)
@result{} nil
@end group
@group
(-tails '(1))
@result{} '((1))
@end group
@end example
@end defun
@anchor{-distinct}
@defun -distinct (list)
Return a new list with all duplicates removed.

@ -607,6 +607,16 @@ new list."
(-permutations '(1 2)) => '((1 2) (2 1))
(-permutations '(a b c)) => '((a b c) (a c b) (b a c) (b c a) (c a b) (c b a)))
(defexamples -inits
(-inits '(1 2 3 4)) => '((1) (1 2) (1 2 3) (1 2 3 4))
(-inits nil) => nil
(-inits '(1)) => '((1)))
(defexamples -tails
(-tails '(1 2 3 4)) => '((1 2 3 4) (2 3 4) (3 4) (4))
(-tails nil) => nil
(-tails '(1)) => '((1)))
(defexamples -distinct
(-distinct '()) => '()
(-distinct '(1 2 2 4)) => '(1 2 4)))

Loading…
Cancel
Save