[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash d8ccf85 254/439: Add replace/update/remove functio
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash d8ccf85 254/439: Add replace/update/remove functions for index/indices |
Date: |
Tue, 04 Aug 2015 20:28:42 +0000 |
branch: externals/dash
commit d8ccf8559f0b5583d70474b64d90bfa6f0b0dacd
Author: Fuco1 <address@hidden>
Commit: Fuco1 <address@hidden>
Add replace/update/remove functions for index/indices
---
README.md | 46 ++++++++++++++++++++++++++++++++++++++++++++++
dash.el | 37 +++++++++++++++++++++++++++++++++++++
dev/examples.el | 36 ++++++++++++++++++++++++++++++++++++
3 files changed, 119 insertions(+), 0 deletions(-)
diff --git a/README.md b/README.md
index 5d0357e..4c469b8 100644
--- a/README.md
+++ b/README.md
@@ -72,6 +72,10 @@ Include this in your emacs settings to get syntax
highlighting:
* [-drop-while](#-drop-while-pred-list) `(pred list)`
* [-rotate](#-rotate-n-list) `(n list)`
* [-insert-at](#-insert-at-n-x-list) `(n x list)`
+* [-replace-at](#-replace-at-n-x-list) `(n x list)`
+* [-update-at](#-update-at-n-func-list) `(n func list)`
+* [-remove-at](#-remove-at-n-list) `(n list)`
+* [-remove-at-indices](#-remove-at-indices-indices-list) `(indices list)`
### Reductions
@@ -384,6 +388,48 @@ Returns a list with `x` inserted into `list` at position
`n`.
(-insert-at 12 'x '(a b c)) ;; => '(a b c x)
```
+#### -replace-at `(n x list)`
+
+Return a list with element at Nth position in `list` replaced with `x`.
+
+```cl
+(-replace-at 0 9 '(0 1 2 3 4 5)) ;; => '(9 1 2 3 4 5)
+(-replace-at 1 9 '(0 1 2 3 4 5)) ;; => '(0 9 2 3 4 5)
+(-replace-at 4 9 '(0 1 2 3 4 5)) ;; => '(0 1 2 3 9 5)
+```
+
+#### -update-at `(n func list)`
+
+Return a list with element at Nth position in `list` replaced with `(func (nth
n list))`.
+
+```cl
+(-update-at 0 (lambda (x) (+ x 9)) '(0 1 2 3 4 5)) ;; => '(9 1 2 3 4 5)
+(-update-at 1 (lambda (x) (+ x 8)) '(0 1 2 3 4 5)) ;; => '(0 9 2 3 4 5)
+(--update-at 2 (length it) '("foo" "bar" "baz" "quux")) ;; => '("foo" "bar" 3
"quux")
+```
+
+#### -remove-at `(n list)`
+
+Return a list with element at Nth position in `list` removed.
+
+```cl
+(-remove-at 0 '("0" "1" "2" "3" "4" "5")) ;; => '("1" "2" "3" "4" "5")
+(-remove-at 1 '("0" "1" "2" "3" "4" "5")) ;; => '("0" "2" "3" "4" "5")
+(-remove-at 2 '("0" "1" "2" "3" "4" "5")) ;; => '("0" "1" "3" "4" "5")
+```
+
+#### -remove-at-indices `(indices list)`
+
+Return a list whose elements are elements from `list` without
+elements selected as `(nth i list)` for all i
+from `indices`.
+
+```cl
+(-remove-at-indices '(0) '("0" "1" "2" "3" "4" "5")) ;; => '("1" "2" "3" "4"
"5")
+(-remove-at-indices '(0 2 4) '("0" "1" "2" "3" "4" "5")) ;; => '("1" "3" "5")
+(-remove-at-indices '(0 5) '("0" "1" "2" "3" "4" "5")) ;; => '("1" "2" "3" "4")
+```
+
## Reductions
diff --git a/dash.el b/dash.el
index 0245bfe..77cb86f 100644
--- a/dash.el
+++ b/dash.el
@@ -469,6 +469,38 @@ The time complexity is O(n)."
(let ((split-list (-split-at n list)))
(nconc (car split-list) (cons x (cadr split-list)))))
+(defun -replace-at (n x list)
+ "Return a list with element at Nth position in LIST replaced with X."
+ (let ((split-list (-split-at n list)))
+ (nconc (car split-list) (cons x (cdr (cadr split-list))))))
+
+(defun -update-at (n func list)
+ "Return a list with element at Nth position in LIST replaced with `(func
(nth n list))`."
+ (let ((split-list (-split-at n list)))
+ (nconc (car split-list) (cons (funcall func (car (cadr split-list))) (cdr
(cadr split-list))))))
+
+(defmacro --update-at (n form list)
+ "Anaphoric version of `-update-at'."
+ `(-update-at ,n (lambda (it) ,form) ,list))
+
+(defun -remove-at (n list)
+ "Return a list with element at Nth position in LIST removed."
+ (-remove-at-indices (list n) list))
+
+(defun -remove-at-indices (indices list)
+ "Return a list whose elements are elements from LIST without
+elements selected as `(nth i list)` for all i
+from INDICES."
+ (let* ((indices (-sort '< indices))
+ (diffs (cons (car indices) (-map '1- (-zip-with '- (cdr indices)
indices))))
+ r)
+ (--each diffs
+ (let ((split (-split-at it list)))
+ (!cons (car split) r)
+ (setq list (cdr (cadr split)))))
+ (!cons list r)
+ (-flatten (nreverse r))))
+
(defmacro --split-with (pred list)
"Anaphoric form of `-split-with'."
(let ((l (make-symbol "list"))
@@ -1172,6 +1204,11 @@ structure such as plist or alist."
"-split-at"
"-rotate"
"-insert-at"
+ "-replace-at"
+ "-update-at"
+ "--update-at"
+ "-remove-at"
+ "-remove-at-indices"
"--split-with"
"-split-with"
"-partition"
diff --git a/dev/examples.el b/dev/examples.el
index 64b0c9a..e872cce 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -90,6 +90,42 @@
(-insert-at 1 'x '(a b c)) => '(a x b c)
(-insert-at 12 'x '(a b c)) => '(a b c x)))
+(defexamples -replace-at
+ (-replace-at 0 9 '(0 1 2 3 4 5)) => '(9 1 2 3 4 5)
+ (-replace-at 1 9 '(0 1 2 3 4 5)) => '(0 9 2 3 4 5)
+ (-replace-at 4 9 '(0 1 2 3 4 5)) => '(0 1 2 3 9 5)
+ (-replace-at 5 9 '(0 1 2 3 4 5)) => '(0 1 2 3 4 9))
+
+(defexamples -update-at
+ (-update-at 0 (lambda (x) (+ x 9)) '(0 1 2 3 4 5)) => '(9 1 2 3 4 5)
+ (-update-at 1 (lambda (x) (+ x 8)) '(0 1 2 3 4 5)) => '(0 9 2 3 4 5)
+ (--update-at 2 (length it) '("foo" "bar" "baz" "quux")) => '("foo" "bar" 3
"quux")
+ (--update-at 2 (concat it "zab") '("foo" "bar" "baz" "quux")) => '("foo"
"bar" "bazzab" "quux"))
+
+(defexamples -remove-at
+ (-remove-at 0 '("0" "1" "2" "3" "4" "5")) => '("1" "2" "3" "4" "5")
+ (-remove-at 1 '("0" "1" "2" "3" "4" "5")) => '("0" "2" "3" "4" "5")
+ (-remove-at 2 '("0" "1" "2" "3" "4" "5")) => '("0" "1" "3" "4" "5")
+ (-remove-at 3 '("0" "1" "2" "3" "4" "5")) => '("0" "1" "2" "4" "5")
+ (-remove-at 4 '("0" "1" "2" "3" "4" "5")) => '("0" "1" "2" "3" "5")
+ (-remove-at 5 '("0" "1" "2" "3" "4" "5")) => '("0" "1" "2" "3" "4"))
+
+(defexamples -remove-at-indices
+ (-remove-at-indices '(0) '("0" "1" "2" "3" "4" "5")) => '("1" "2" "3" "4"
"5")
+ (-remove-at-indices '(0 2 4) '("0" "1" "2" "3" "4" "5")) => '("1" "3" "5")
+ (-remove-at-indices '(0 5) '("0" "1" "2" "3" "4" "5")) => '("1" "2" "3" "4")
+ (-remove-at-indices '(1 2 3) '("0" "1" "2" "3" "4" "5")) => '("0" "4" "5")
+ (-remove-at-indices '(0 1 2 3 4 5) '("0" "1" "2" "3" "4" "5")) => nil
+ (-remove-at-indices '(2 0 4) '("0" "1" "2" "3" "4" "5")) => '("1" "3" "5")
+ (-remove-at-indices '(5 0) '("0" "1" "2" "3" "4" "5")) => '("1" "2" "3" "4")
+ (-remove-at-indices '(1 3 2) '("0" "1" "2" "3" "4" "5")) => '("0" "4" "5")
+ (-remove-at-indices '(0 3 4 2 5 1) '("0" "1" "2" "3" "4" "5")) => nil
+ (-remove-at-indices '(1) '("0" "1" "2" "3" "4" "5")) => '("0" "2" "3" "4"
"5")
+ (-remove-at-indices '(2) '("0" "1" "2" "3" "4" "5")) => '("0" "1" "3" "4"
"5")
+ (-remove-at-indices '(3) '("0" "1" "2" "3" "4" "5")) => '("0" "1" "2" "4"
"5")
+ (-remove-at-indices '(4) '("0" "1" "2" "3" "4" "5")) => '("0" "1" "2" "3"
"5")
+ (-remove-at-indices '(5) '("0" "1" "2" "3" "4" "5")) => '("0" "1" "2" "3"
"4"))
+
(def-example-group "Reductions" nil
(defexamples -reduce-from
(-reduce-from '- 10 '(1 2 3)) => 4
- [elpa] externals/dash 9936885 245/439: Merge pull request #51 from Fuco1/debug-decl, (continued)
- [elpa] externals/dash 9936885 245/439: Merge pull request #51 from Fuco1/debug-decl, Phillip Lord, 2015/08/04
- [elpa] externals/dash 38307e3 223/439: Partition docs, Phillip Lord, 2015/08/04
- [elpa] externals/dash bf99147 247/439: Fix `-tree-mapreduce-from` test & reformat the tests, Phillip Lord, 2015/08/04
- [elpa] externals/dash 75efb60 246/439: Add tree map/reduce, Phillip Lord, 2015/08/04
- [elpa] externals/dash e7bc8fe 250/439: Make dash-specific font lock optional, Phillip Lord, 2015/08/04
- [elpa] externals/dash 2ee84cb 251/439: Release 2.3.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash f4ba8db 252/439: Add -snoc, Phillip Lord, 2015/08/04
- [elpa] externals/dash 9de1435 248/439: Merge pull request #53 from Fuco1/tree-stuff, Phillip Lord, 2015/08/04
- [elpa] externals/dash 0f4cae9 249/439: Improve docs, Phillip Lord, 2015/08/04
- [elpa] externals/dash ec9afcb 255/439: Merge pull request #54 from Fuco1/index/modify, Phillip Lord, 2015/08/04
- [elpa] externals/dash d8ccf85 254/439: Add replace/update/remove functions for index/indices,
Phillip Lord <=
- [elpa] externals/dash 3bbaed5 256/439: Release 2.4.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash ab727ba 257/439: Correct anchors for links starting with '!', Phillip Lord, 2015/08/04
- [elpa] externals/dash 8052eb9 253/439: Merge pull request #55 from Fuco1/snoc, Phillip Lord, 2015/08/04
- [elpa] externals/dash 8005153 259/439: Add customize option to turn on font-lock for dash, Phillip Lord, 2015/08/04
- [elpa] externals/dash 7357283 258/439: Merge pull request #57 from Silex/master, Phillip Lord, 2015/08/04
- [elpa] externals/dash ba6e3c6 260/439: Add missing keywords, Phillip Lord, 2015/08/04
- [elpa] externals/dash ca9b296 263/439: Add Cask-file., Phillip Lord, 2015/08/04
- [elpa] externals/dash c788474 264/439: Fix bug with nested lists in `-remove-at-indices`, Phillip Lord, 2015/08/04
- [elpa] externals/dash 094fdea 262/439: Merge pull request #58 from Fuco1/fontlock-custom, Phillip Lord, 2015/08/04
- [elpa] externals/dash 47af4d6 265/439: Merge pull request #61 from Fuco1/master, Phillip Lord, 2015/08/04