[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash de67c4b 188/439: Add `-sort`
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash de67c4b 188/439: Add `-sort` |
Date: |
Tue, 04 Aug 2015 20:27:47 +0000 |
branch: externals/dash
commit de67c4b0a71e73fe4bbfb587f42a6c8845808f7f
Author: Magnar Sveen <address@hidden>
Commit: Magnar Sveen <address@hidden>
Add `-sort`
Fixes #34
---
README.md | 14 ++++++++++++++
dash.el | 12 ++++++++++++
dev/examples.el | 6 ++++++
3 files changed, 32 insertions(+), 0 deletions(-)
diff --git a/README.md b/README.md
index 98954d7..b08f657 100644
--- a/README.md
+++ b/README.md
@@ -62,6 +62,7 @@ Or you can just dump `dash.el` in your load path somewhere.
* [-intersection](#-intersection-list-list2) `(list list2)`
* [-distinct](#-distinct-list) `(list)`
* [-contains?](#-contains-list-element) `(list element)`
+* [-sort](#-sort-predicate-list) `(predicate list)`
* [-partial](#-partial-fn-rest-args) `(fn &rest args)`
* [-rpartial](#-rpartial-fn-rest-args) `(fn &rest args)`
* [-applify](#-applify-fn) `(fn)`
@@ -675,6 +676,19 @@ or with `-compare-fn` if that's non-nil.
(-contains? '(1 2 3) 4) ;; => nil
```
+### -sort `(predicate list)`
+
+Sort `list`, stably, comparing elements using `predicate`.
+Returns the sorted list. `list` is `not` modified by side effects.
+`predicate` is called with two elements of `list`, and should return non-nil
+if the first element should sort before the second.
+
+```cl
+(-sort '< '(3 1 2)) ;; => '(1 2 3)
+(-sort '> '(3 1 2)) ;; => '(3 2 1)
+(--sort (< it other) '(3 1 2)) ;; => '(1 2 3)
+```
+
### -partial `(fn &rest args)`
Takes a function `fn` and fewer than the normal arguments to `fn`,
diff --git a/dash.el b/dash.el
index fc7dded..5994eaa 100644
--- a/dash.el
+++ b/dash.el
@@ -853,6 +853,18 @@ or with `-compare-fn' if that's non-nil."
(defalias '-contains-p '-contains?)
+(defun -sort (predicate list)
+ "Sort LIST, stably, comparing elements using PREDICATE.
+Returns the sorted list. LIST is NOT modified by side effects.
+PREDICATE is called with two elements of LIST, and should return non-nil
+if the first element should sort before the second."
+ (sort (copy-sequence list) predicate))
+
+(defmacro --sort (form list)
+ "Anaphoric form of `-sort'."
+ (declare (debug t))
+ `(-sort (lambda (it other) ,form) ,list))
+
(defun -repeat (n x)
"Return a list with X repeated N times.
Returns nil if N is less than 1."
diff --git a/dev/examples.el b/dev/examples.el
index 0bb9710..48561ba 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -275,6 +275,12 @@
(-contains? '() 1) => nil
(-contains? '() '()) => nil)
+(defexamples -sort
+ (-sort '< '(3 1 2)) => '(1 2 3)
+ (-sort '> '(3 1 2)) => '(3 2 1)
+ (--sort (< it other) '(3 1 2)) => '(1 2 3)
+ (let ((l '(3 1 2))) (-sort '> l) l) => '(3 1 2))
+
(defexamples -partial
(funcall (-partial '- 5) 3) => 2
(funcall (-partial '+ 5 2) 3) => 10)
- [elpa] externals/dash 3c546ab 178/439: Add -partition-in-steps & -partition-all-in-steps, (continued)
- [elpa] externals/dash 3c546ab 178/439: Add -partition-in-steps & -partition-all-in-steps, Phillip Lord, 2015/08/04
- [elpa] externals/dash e870be8 173/439: Add -when-let* and -if-let*, Phillip Lord, 2015/08/04
- [elpa] externals/dash a3faf7c 183/439: Release 1.3.2, Phillip Lord, 2015/08/04
- [elpa] externals/dash 5614753 182/439: Fix bug with -partition-all-in-steps, Phillip Lord, 2015/08/04
- [elpa] externals/dash 4c8c410 180/439: Add new functions to syntax highlighting, Phillip Lord, 2015/08/04
- [elpa] externals/dash 49de164 184/439: Declare debug-info for some macros, Phillip Lord, 2015/08/04
- [elpa] externals/dash 1fc6679 185/439: Add -map-indexed to syntax highlighting, Phillip Lord, 2015/08/04
- [elpa] externals/dash 50659cc 189/439: Release 1.4.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash ff3d7bd 190/439: Add -sum function., Phillip Lord, 2015/08/04
- [elpa] externals/dash 12291f3 191/439: Add -product function., Phillip Lord, 2015/08/04
- [elpa] externals/dash de67c4b 188/439: Add `-sort`,
Phillip Lord <=
- [elpa] externals/dash 3226100 194/439: Indent according to emacs lisp standard, Phillip Lord, 2015/08/04
- [elpa] externals/dash 962e5b8 187/439: Merge pull request #33 from Fuco1/reduce-r, Phillip Lord, 2015/08/04
- [elpa] externals/dash d556884 186/439: Add reduce-r, the right associative variant of reduce, Phillip Lord, 2015/08/04
- [elpa] externals/dash 7e41bed 192/439: Merge pull request #35 from rejeep/sum-and-product, Phillip Lord, 2015/08/04
- [elpa] externals/dash 2b20088 195/439: Add -juxt, Phillip Lord, 2015/08/04
- [elpa] externals/dash e07cef6 196/439: Adding -first-item and -last-item. [magnars/dash.el#17], Phillip Lord, 2015/08/04
- [elpa] externals/dash a3b2fdb 198/439: Merge pull request #38 from rejeep/min-and-max, Phillip Lord, 2015/08/04
- [elpa] externals/dash 4164908 197/439: Add -min, -max, -min-by and -max-by., Phillip Lord, 2015/08/04
- [elpa] externals/dash 22d2c2d 199/439: Release 1.6.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash 5b5dab5 200/439: Fix typo, Phillip Lord, 2015/08/04