diff --git a/README.md b/README.md index 0f42e38..17b9b24 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/dash.el b/dash.el index 773f79a..aee31af 100644 --- a/dash.el +++ b/dash.el @@ -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. diff --git a/dash.texi b/dash.texi index 909fc7e..6a60f65 100644 --- a/dash.texi +++ b/dash.texi @@ -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. diff --git a/dev/examples.el b/dev/examples.el index 6216ee1..796d933 100644 --- a/dev/examples.el +++ b/dev/examples.el @@ -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)))