[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash 87c8b91 232/439: Add indexing operations
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash 87c8b91 232/439: Add indexing operations |
Date: |
Tue, 04 Aug 2015 20:28:21 +0000 |
branch: externals/dash
commit 87c8b91e1bf439bcea65fa96f9253b59b5ec52bc
Author: Matus Goljer <address@hidden>
Commit: Matus Goljer <address@hidden>
Add indexing operations
---
README.md | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
dash.el | 68 ++++++++++++++++++++++++++++++++++++++++
dev/examples.el | 34 ++++++++++++++++++++
3 files changed, 194 insertions(+), 0 deletions(-)
diff --git a/README.md b/README.md
index 7dcb490..ba03abe 100644
--- a/README.md
+++ b/README.md
@@ -101,6 +101,16 @@ To get function combinators:
* [-partition-by-header](#-partition-by-header-fn-list) `(fn list)`
* [-group-by](#-group-by-fn-list) `(fn list)`
+### Indexing
+
+* [-elem-index](#-elem-index-elem-list) `(elem list)`
+* [-elem-indices](#-elem-indices-elem-list) `(elem list)`
+* [-find-index](#-find-index-pred-list) `(pred list)`
+* [-find-indices](#-find-indices-pred-list) `(pred list)`
+* [-select-by-indices](#-select-by-indices-indices-list) `(indices list)`
+* [-grade-up](#-grade-up-comparator-list) `(comparator list)`
+* [-grade-down](#-grade-down-comparator-list) `(comparator list)`
+
### Set operations
* [-union](#-union-list-list2) `(list list2)`
@@ -678,6 +688,88 @@ elements of `list`. Keys are compared by `equal`.
```
+## Indexing
+
+#### -elem-index `(elem list)`
+
+Return the index of the first element in the given `list` which
+is equal to the query element `elem`, or nil if there is no
+such element.
+
+```cl
+(-elem-index 2 '(6 7 8 2 3 4)) ;; => 3
+(-elem-index "bar" '("foo" "bar" "baz")) ;; => 1
+(-elem-index '(1 2) '((3) (5 6) (1 2) nil)) ;; => 2
+```
+
+#### -elem-indices `(elem list)`
+
+Return the indices of all elements in `list` equal to the query
+element `elem`, in ascending order.
+
+```cl
+(-elem-indices 2 '(6 7 8 2 3 4 2 1)) ;; => '(3 6)
+(-elem-indices "bar" '("foo" "bar" "baz")) ;; => '(1)
+(-elem-indices '(1 2) '((3) (1 2) (5 6) (1 2) nil)) ;; => '(1 3)
+```
+
+#### -find-index `(pred list)`
+
+Return the indices of all elements in `list` satisfying the
+predicate `pred`, in ascending order.
+
+```cl
+(-find-index 'even? '(2 4 1 6 3 3 5 8)) ;; => 0
+(--find-index (< 5 it) '(2 4 1 6 3 3 5 8)) ;; => 3
+(-find-index (-partial 'string-lessp "baz") '("bar" "foo" "baz")) ;; => 1
+```
+
+#### -find-indices `(pred list)`
+
+Take a predicate `pred` and a `list` and return the index of the
+first element in the list satisfying the predicate, or nil if
+there is no such element.
+
+```cl
+(-find-indices 'even? '(2 4 1 6 3 3 5 8)) ;; => '(0 1 3 7)
+(--find-indices (< 5 it) '(2 4 1 6 3 3 5 8)) ;; => '(3 7)
+(-find-indices (-partial 'string-lessp "baz") '("bar" "foo" "baz")) ;; => '(1)
+```
+
+#### -select-by-indices `(indices list)`
+
+Return a list whose elements are elements from `list` selected
+as `(nth i list)` for all i from `indices`.
+
+```cl
+(-select-by-indices '(4 10 2 3 6) '("v" "e" "l" "o" "c" "i" "r" "a" "p" "t"
"o" "r")) ;; => '("c" "o" "l" "o" "r")
+(-select-by-indices '(2 1 0) '("a" "b" "c")) ;; => '("c" "b" "a")
+(-select-by-indices '(0 1 2 0 1 3 3 1) '("f" "a" "r" "l")) ;; => '("f" "a" "r"
"f" "a" "l" "l" "a")
+```
+
+#### -grade-up `(comparator list)`
+
+Grades elements of `list` using `comparator` relation, yielding a
+permutation vector such that applying this permutation to `list`
+sorts it in ascending order.
+
+```cl
+(-grade-up '< '(3 1 4 2 1 3 3)) ;; => '(1 4 3 0 5 6 2)
+(let ((l '(3 1 4 2 1 3 3))) (-select-by-indices (-grade-up '< l) l)) ;; => '(1
1 2 3 3 3 4)
+```
+
+#### -grade-down `(comparator list)`
+
+Grades elements of `list` using `comparator` relation, yielding a
+permutation vector such that applying this permutation to `list`
+sorts it in descending order.
+
+```cl
+(-grade-down '< '(3 1 4 2 1 3 3)) ;; => '(2 0 5 6 3 1 4)
+(let ((l '(3 1 4 2 1 3 3))) (-select-by-indices (-grade-down '< l) l)) ;; =>
'(4 3 3 3 2 1 1)
+```
+
+
## Set operations
#### -union `(list list2)`
diff --git a/dash.el b/dash.el
index 970b743..42c741a 100644
--- a/dash.el
+++ b/dash.el
@@ -697,6 +697,65 @@ When called, the returned function calls FN with ARGS
first and
then additional args."
(apply 'apply-partially fn args))
+(defun -elem-index (elem list)
+ "Return the index of the first element in the given LIST which
+is equal to the query element ELEM, or nil if there is no
+such element."
+ (car (-elem-indices elem list)))
+
+(defun -elem-indices (elem list)
+ "Return the indices of all elements in LIST equal to the query
+element ELEM, in ascending order."
+ (-find-indices (-partial 'equal elem) list))
+
+(defun -find-indices (pred list)
+ "Take a predicate PRED and a LIST and return the index of the
+first element in the list satisfying the predicate, or nil if
+there is no such element."
+ (let ((i 0))
+ (apply 'append (--map-indexed (when (funcall pred it) (list it-index))
list))))
+
+(defmacro --find-indices (form list)
+ "Anaphoric version of `-find-indices'."
+ `(-find-indices (lambda (it) ,form) ,list))
+
+(defun -find-index (pred list)
+ "Return the indices of all elements in LIST satisfying the
+predicate PRED, in ascending order."
+ (car (-find-indices pred list)))
+
+(defmacro --find-index (form list)
+ "Anaphoric version of `-find-index'."
+ `(-find-index (lambda (it) ,form) ,list))
+
+(defun -select-by-indices (indices list)
+ "Return a list whose elements are elements from LIST selected
+as `(nth i list)` for all i from INDICES."
+ (let (r)
+ (--each indices
+ (!cons (nth it list) r))
+ (nreverse r)))
+
+(defun -grade-up (comparator list)
+ "Grades elements of LIST using COMPARATOR relation, yielding a
+permutation vector such that applying this permutation to LIST
+sorts it in ascending order."
+ ;; ugly hack to "fix" lack of lexical scope
+ (let ((comp `(lambda (it other) (funcall ',comparator (car it) (car
other)))))
+ (->> (--map-indexed (cons it it-index) list)
+ (-sort comp)
+ (-map 'cdr))))
+
+(defun -grade-down (comparator list)
+ "Grades elements of LIST using COMPARATOR relation, yielding a
+permutation vector such that applying this permutation to LIST
+sorts it in descending order."
+ ;; ugly hack to "fix" lack of lexical scope
+ (let ((comp `(lambda (it other) (funcall ',comparator (car other) (car
it)))))
+ (->> (--map-indexed (cons it it-index) list)
+ (-sort comp)
+ (-map 'cdr))))
+
(defmacro -> (x &optional form &rest more)
"Threads the expr through the forms. Inserts X as the second
item in the first form, making a list of it if it is not a list
@@ -1010,6 +1069,15 @@ The items for the comparator form are exposed as \"it\"
and \"other\"."
"-cut"
"-orfn"
"-andfn"
+ "-elem-index"
+ "-elem-indices"
+ "-find-indices"
+ "--find-indices"
+ "-find-index"
+ "--find-index"
+ "-select-by-indices"
+ "-grade-up"
+ "-grade-down"
"->"
"->>"
"-->"
diff --git a/dev/examples.el b/dev/examples.el
index 5430057..6c3868e 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -238,6 +238,40 @@
(-group-by 'even? '(1 1 2 2 2 3 4 6 8)) => '((nil . (1 1 3)) (t . (2 2 2 4
6 8)))
(--group-by (car (split-string it "/")) '("a/b" "c/d" "a/e")) => '(("a" .
("a/b" "a/e")) ("c" . ("c/d")))))
+(def-example-group "Indexing" nil
+ (defexamples -elem-index
+ (-elem-index 2 '(6 7 8 2 3 4)) => 3
+ (-elem-index "bar" '("foo" "bar" "baz")) => 1
+ (-elem-index '(1 2) '((3) (5 6) (1 2) nil)) => 2)
+
+ (defexamples -elem-indices
+ (-elem-indices 2 '(6 7 8 2 3 4 2 1)) => '(3 6)
+ (-elem-indices "bar" '("foo" "bar" "baz")) => '(1)
+ (-elem-indices '(1 2) '((3) (1 2) (5 6) (1 2) nil)) => '(1 3))
+
+ (defexamples -find-index
+ (-find-index 'even? '(2 4 1 6 3 3 5 8)) => 0
+ (--find-index (< 5 it) '(2 4 1 6 3 3 5 8)) => 3
+ (-find-index (-partial 'string-lessp "baz") '("bar" "foo" "baz")) => 1)
+
+ (defexamples -find-indices
+ (-find-indices 'even? '(2 4 1 6 3 3 5 8)) => '(0 1 3 7)
+ (--find-indices (< 5 it) '(2 4 1 6 3 3 5 8)) => '(3 7)
+ (-find-indices (-partial 'string-lessp "baz") '("bar" "foo" "baz")) =>
'(1))
+
+ (defexamples -select-by-indices
+ (-select-by-indices '(4 10 2 3 6) '("v" "e" "l" "o" "c" "i" "r" "a" "p"
"t" "o" "r")) => '("c" "o" "l" "o" "r")
+ (-select-by-indices '(2 1 0) '("a" "b" "c")) => '("c" "b" "a")
+ (-select-by-indices '(0 1 2 0 1 3 3 1) '("f" "a" "r" "l")) => '("f" "a"
"r" "f" "a" "l" "l" "a"))
+
+ (defexamples -grade-up
+ (-grade-up '< '(3 1 4 2 1 3 3)) => '(1 4 3 0 5 6 2)
+ (let ((l '(3 1 4 2 1 3 3))) (-select-by-indices (-grade-up '< l) l)) =>
'(1 1 2 3 3 3 4))
+
+ (defexamples -grade-down
+ (-grade-down '< '(3 1 4 2 1 3 3)) => '(2 0 5 6 3 1 4)
+ (let ((l '(3 1 4 2 1 3 3))) (-select-by-indices (-grade-down '< l) l)) =>
'(4 3 3 3 2 1 1)))
+
(def-example-group "Set operations" nil
(defexamples -union
(-union '(1 2 3) '(3 4 5)) => '(1 2 3 4 5)
- [elpa] externals/dash 6d0a8d6 225/439: Add Version and Package-Requires to files #45, (continued)
- [elpa] externals/dash 6d0a8d6 225/439: Add Version and Package-Requires to files #45, Phillip Lord, 2015/08/04
- [elpa] externals/dash d365f22 222/439: Fix byte compile warnings, Phillip Lord, 2015/08/04
- [elpa] externals/dash 1812a17 226/439: Complete dash and dash-functional split, Phillip Lord, 2015/08/04
- [elpa] externals/dash f1049f1 216/439: Add dash-functional back, now dependent on dash.el, Phillip Lord, 2015/08/04
- [elpa] externals/dash 39d20f2 228/439: Release 2.0.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash 81e43ad 229/439: Add -union to list of highlighted symbols., Phillip Lord, 2015/08/04
- [elpa] externals/dash 515e2d3 230/439: Use `comparator` instead of `predicate` for sort., Phillip Lord, 2015/08/04
- [elpa] externals/dash 6cc7727 234/439: Release 2.1.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash 6eeb692 236/439: Provide an implementation of function composition., Phillip Lord, 2015/08/04
- [elpa] externals/dash 23ab726 227/439: Improve formatting of docs, Phillip Lord, 2015/08/04
- [elpa] externals/dash 87c8b91 232/439: Add indexing operations,
Phillip Lord <=
- [elpa] externals/dash 1e2bd96 233/439: Merge pull request #48 from Fuco1/index-operations, Phillip Lord, 2015/08/04
- [elpa] externals/dash 5232399 238/439: Merge pull request #50 from kurisuwhyte/master, Phillip Lord, 2015/08/04
- [elpa] externals/dash 20eb331 237/439: Updates the documentation., Phillip Lord, 2015/08/04
- [elpa] externals/dash 6d43c4f 235/439: Fix switched around doc strings for -find-index/indices, Phillip Lord, 2015/08/04
- [elpa] externals/dash 13e0a35 240/439: Release 2.2.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash dbcb3ef 231/439: Update README, Phillip Lord, 2015/08/04
- [elpa] externals/dash bf85b21 242/439: Change &optional branches to &rest branches in `-if-let`s, Phillip Lord, 2015/08/04
- [elpa] externals/dash 8e2422a 239/439: Add Christina Whyte (@kurisuwhyte) to list of contributors, Phillip Lord, 2015/08/04
- [elpa] externals/dash b4d84de 243/439: Add debug declarations for `-when-let`s and `-if-let`s, Phillip Lord, 2015/08/04
- [elpa] externals/dash 6d20aa0 241/439: If has an indent function 2, not 1, Phillip Lord, 2015/08/04