Fix -repeat

master
vemv 13 years ago
parent e4521af667
commit a196ac1166
  1. 12
      README.md
  2. 13
      dash.el
  3. 6
      dev/examples.el

@ -57,6 +57,7 @@ Or you can just dump `dash.el` in your load path somewhere.
* [-->](#---x-form-rest-more) `(x form &rest more)` * [-->](#---x-form-rest-more) `(x form &rest more)`
* [!cons](#-cons-car-cdr) `(car cdr)` * [!cons](#-cons-car-cdr) `(car cdr)`
* [!cdr](#-cdr-list) `(list)` * [!cdr](#-cdr-list) `(list)`
* [-repeat](#-repeat-n-x) `(n x)`
There are also anaphoric versions of these functions where that makes sense, There are also anaphoric versions of these functions where that makes sense,
prefixed with two dashes instead of one. 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) (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 ## Contribute

@ -587,15 +587,16 @@ or with `-compare-fn' if that's non-nil."
(setq lst (cdr lst))) (setq lst (cdr lst)))
lst)))))) lst))))))
(defalias '-contains-p '-contains?)
(defun -repeat (n x) (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)) (let ((ret nil))
(loop for i from 0 to (dec n) do (while (not (minusp (setq n (1- n))))
(setq ret (cons x ret))) (!cons x ret))
ret)) ret))
(defalias '-contains-p '-contains?)
(eval-after-load "lisp-mode" (eval-after-load "lisp-mode"
'(progn '(progn
(let ((new-keywords '( (let ((new-keywords '(
@ -678,7 +679,7 @@ or with `-compare-fn' if that's non-nil."
"-difference" "-difference"
"-contains?" "-contains?"
"-contains-p" "-contains-p"
)) "-repeat"))
(special-variables '( (special-variables '(
"it" "it"
"it-index" "it-index"

@ -239,3 +239,9 @@
(defexamples !cdr (defexamples !cdr
(let ((l '(3))) (!cdr l) l) => '() (let ((l '(3))) (!cdr l) l) => '()
(let ((l '(3 5))) (!cdr l) l) => '(5)) (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)

Loading…
Cancel
Save