diff --git a/README.md b/README.md index 3b030b2..dc25b20 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ Or you can just dump `dash.el` in your load path somewhere. * [-->](#---x-form-rest-more) `(x form &rest more)` * [!cons](#-cons-car-cdr) `(car cdr)` * [!cdr](#-cdr-list) `(list)` +* [-repeat](#-repeat-n-x) `(n x)` There are also anaphoric versions of these functions where that makes sense, prefixed with two dashes instead of one. @@ -586,6 +587,17 @@ Destructive: Sets `list` to the cdr of `list`. (let ((l '(3 5))) (!cdr l) l) ;; => '(5) ``` +### -repeat `(n x)` + +Return a list of `n` Xs. +Attempts of retrieving a non-positive amount of Xs will return nil. + +```cl +(-repeat 3 :a) ;; => '(:a :a :a) +(-repeat 1 :a) ;; => '(:a) +(-repeat 0 :a) ;; => nil +``` + ## Contribute diff --git a/dash.el b/dash.el index b1b5896..a67b957 100644 --- a/dash.el +++ b/dash.el @@ -587,15 +587,16 @@ or with `-compare-fn' if that's non-nil." (setq lst (cdr lst))) lst)))))) +(defalias '-contains-p '-contains?) + (defun -repeat (n x) - "Return a list of N Xs." + "Return a list of N Xs. +Attempts of retrieving a non-positive amount of Xs will return nil." (let ((ret nil)) - (loop for i from 0 to (dec n) do - (setq ret (cons x ret))) + (while (not (minusp (setq n (1- n)))) + (!cons x ret)) ret)) -(defalias '-contains-p '-contains?) - (eval-after-load "lisp-mode" '(progn (let ((new-keywords '( @@ -678,7 +679,7 @@ or with `-compare-fn' if that's non-nil." "-difference" "-contains?" "-contains-p" - )) + "-repeat")) (special-variables '( "it" "it-index" diff --git a/dev/examples.el b/dev/examples.el index e9686ac..d5d5b80 100644 --- a/dev/examples.el +++ b/dev/examples.el @@ -239,3 +239,9 @@ (defexamples !cdr (let ((l '(3))) (!cdr l) l) => '() (let ((l '(3 5))) (!cdr l) l) => '(5)) + +(defexamples -repeat + (-repeat 3 :a) => '(:a :a :a) + (-repeat 1 :a) => '(:a) + (-repeat 0 :a) => nil + (-repeat -1 :a) => nil)