diff --git a/README.md b/README.md index b91cb39..e98f711 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ Or you can just dump `dash.el` in your load path somewhere. * [-take-while](#-take-while-pred-list) `(pred list)` * [-drop-while](#-drop-while-pred-list) `(pred list)` * [-split-at](#-split-at-n-list) `(n list)` +* [-insert-at](#-insert-at-n-x-list) `(n x list)` * [-split-with](#-split-with-pred-list) `(pred list)` * [-separate](#-separate-pred-list) `(pred list)` * [-partition](#-partition-n-list) `(n list)` @@ -392,6 +393,15 @@ Returns a list of ((-take `n` `list`) (-drop `n` `list`)), in no more than one p (-split-at 17 '(1 2 3 4 5)) ;; => '((1 2 3 4 5) nil) ``` +### -insert-at `(n x list)` + +Returns a list with `x` inserted into `list` at position `n`. + +```cl +(-insert-at 1 'x '(a b c)) ;; => '(a x b c) +(-insert-at 12 'x '(a b c)) ;; => '(a b c x) +``` + ### -split-with `(pred list)` Returns a list of ((-take-while `pred` `list`) (-drop-while `pred` `list`)), in no more than one pass through the list. diff --git a/dash.el b/dash.el index 81748ac..58fba98 100644 --- a/dash.el +++ b/dash.el @@ -411,6 +411,11 @@ FROM or TO may be negative." (!cdr list))) (list (nreverse result) list))) +(defun -insert-at (n x list) + "Returns a list with X inserted into LIST at position N." + (let ((split-list (-split-at n list))) + (append (car split-list) (cons x (cadr split-list))))) + (defmacro --split-with (pred list) "Anaphoric form of `-split-with'." (let ((l (make-symbol "list")) @@ -843,6 +848,7 @@ Returns nil if N is less than 1." "--drop-while" "-drop-while" "-split-at" + "-insert-at" "--split-with" "-split-with" "-partition" diff --git a/dev/examples.el b/dev/examples.el index 4482700..45ad8bf 100644 --- a/dev/examples.el +++ b/dev/examples.el @@ -150,6 +150,10 @@ (-split-at 3 '(1 2 3 4 5)) => '((1 2 3) (4 5)) (-split-at 17 '(1 2 3 4 5)) => '((1 2 3 4 5) nil)) +(defexamples -insert-at + (-insert-at 1 'x '(a b c)) => '(a x b c) + (-insert-at 12 'x '(a b c)) => '(a b c x)) + (defexamples -split-with (-split-with 'even? '(1 2 3 4)) => '(() (1 2 3 4)) (-split-with 'even? '(2 4 5 6)) => '((2 4) (5 6))