[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash abff641 290/426: Merge pull request #76 from Fuco1
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash abff641 290/426: Merge pull request #76 from Fuco1/unfold |
Date: |
Tue, 04 Aug 2015 19:38:21 +0000 |
branch: externals/dash
commit abff6418642142694ebb23190eeb3e7cfb7e79c5
Merge: f96196a b075846
Author: Magnar Sveen <address@hidden>
Commit: Magnar Sveen <address@hidden>
Merge pull request #76 from Fuco1/unfold
Add -iterate, -unfold
---
README.md | 47 +++++++++++++++++++++++++++++++++++++++++++++++
dash.el | 43 +++++++++++++++++++++++++++++++++++++++++++
dev/examples.el | 13 +++++++++++++
3 files changed, 103 insertions(+), 0 deletions(-)
diff --git a/README.md b/README.md
index 546c867..bd5a298 100644
--- a/README.md
+++ b/README.md
@@ -72,6 +72,14 @@ Include this in your emacs settings to get syntax
highlighting:
* [-max](#-max-list) `(list)`
* [-max-by](#-max-by-comparator-list) `(comparator list)`
+### Unfolding
+
+
+Operations dual to reductions, building lists from seed value rather than
consuming a list to produce a single value.
+
+* [-iterate](#-iterate-fun-init-n) `(fun init n)`
+* [-unfold](#-unfold-fun-seed) `(fun seed)`
+
### Predicates
* [-any?](#-any-pred-list) `(pred list)`
@@ -567,6 +575,45 @@ comparing them.
```
+## Unfolding
+
+
+Operations dual to reductions, building lists from seed value rather than
consuming a list to produce a single value.
+
+#### -iterate `(fun init n)`
+
+Return a list of iterated applications of `fun` to `init`.
+
+This means a list of form:
+ '(init (fun init) (fun (fun init)) ...)
+
+`n` is the length of the returned list.
+
+```cl
+(-iterate '1+ 1 10) ;; => '(1 2 3 4 5 6 7 8 9 10)
+(-iterate (lambda (x) (+ x x)) 2 5) ;; => '(2 4 8 16 32)
+(--iterate (* it it) 2 5) ;; => '(2 4 16 256 65536)
+```
+
+#### -unfold `(fun seed)`
+
+Build a list from `seed` using `fun`.
+
+This is "dual" operation to `-reduce-r`: while -reduce-r
+consumes a list to produce a single value, `-unfold` takes a
+seed value and builds a (potentially infinite!) list.
+
+`fun` should return `nil` to stop the generating process, or a
+cons (`a` . `b`), where `a` will be prepended to the result and `b` is
+the new seed.
+
+```cl
+(-unfold (lambda (x) (unless (= x 0) (cons x (1- x)))) 10) ;; => '(10 9 8 7 6
5 4 3 2 1)
+(--unfold (when it (cons it (cdr it))) '(1 2 3 4)) ;; => '((1 2 3 4) (2 3 4)
(3 4) (4))
+(--unfold (when it (cons it (butlast it))) '(1 2 3 4)) ;; => '((1 2 3 4) (1 2
3) (1 2) (1))
+```
+
+
## Predicates
#### -any? `(pred list)`
diff --git a/dash.el b/dash.el
index 0022076..c7fd82d 100644
--- a/dash.el
+++ b/dash.el
@@ -1113,6 +1113,45 @@ The items for the comparator form are exposed as \"it\"
and \"other\"."
(declare (debug (sexp form)))
`(-min-by (lambda (it other) ,form) ,list))
+(defun -iterate (fun init n)
+ "Return a list of iterated applications of FUN to INIT.
+
+This means a list of form:
+ '(init (fun init) (fun (fun init)) ...)
+
+N is the length of the returned list."
+ (if (= n 0) nil
+ (let ((r (list init)))
+ (--dotimes (1- n)
+ (push (funcall fun (car r)) r))
+ (nreverse r))))
+
+(defmacro --iterate (form init n)
+ "Anaphoric version of `-iterate'."
+ (declare (debug (sexp form form)))
+ `(-iterate (lambda (it) ,form) ,init ,n))
+
+(defun -unfold (fun seed)
+ "Build a list from SEED using FUN.
+
+This is \"dual\" operation to `-reduce-r': while -reduce-r
+consumes a list to produce a single value, `-unfold' takes a
+seed value and builds a (potentially infinite!) list.
+
+FUN should return `nil' to stop the generating process, or a
+cons (A . B), where A will be prepended to the result and B is
+the new seed."
+ (let ((last (funcall fun seed)) r)
+ (while last
+ (push (car last) r)
+ (setq last (funcall fun (cdr last))))
+ (nreverse r)))
+
+(defmacro --unfold (form seed)
+ "Anaphoric version of `-unfold'."
+ (declare (debug (sexp form)))
+ `(-unfold (lambda (it) ,form) ,seed))
+
(defun -cons-pair? (con)
"Return non-nil if CON is true cons pair.
That is (A . B) where B is not a list."
@@ -1397,6 +1436,10 @@ structure such as plist or alist."
"--max-by"
"-min-by"
"--min-by"
+ "-iterate"
+ "--iterate"
+ "-unfold"
+ "--unfold"
"-cons-pair?"
"-cons-to-list"
"-value-to-list"
diff --git a/dev/examples.el b/dev/examples.el
index 4568f60..7b6d4dc 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -199,6 +199,19 @@
(--max-by (> (car it) (car other)) '((1 2 3) (2) (3 2))) => '(3 2)
(--max-by (> (length it) (length other)) '((1 2 3) (2) (3 2))) => '(1 2
3)))
+(def-example-group "Unfolding"
+ "Operations dual to reductions, building lists from seed value rather than
consuming a list to produce a single value."
+
+ (defexamples -iterate
+ (-iterate '1+ 1 10) => '(1 2 3 4 5 6 7 8 9 10)
+ (-iterate (lambda (x) (+ x x)) 2 5) => '(2 4 8 16 32)
+ (--iterate (* it it) 2 5) => '(2 4 16 256 65536))
+
+ (defexamples -unfold
+ (-unfold (lambda (x) (unless (= x 0) (cons x (1- x)))) 10) => '(10 9 8 7 6
5 4 3 2 1)
+ (--unfold (when it (cons it (cdr it))) '(1 2 3 4)) => '((1 2 3 4) (2 3 4)
(3 4) (4))
+ (--unfold (when it (cons it (butlast it))) '(1 2 3 4)) => '((1 2 3 4) (1 2
3) (1 2) (1))))
+
(def-example-group "Predicates" nil
(defexamples -any?
(-any? 'even? '(1 2 3)) => t
- [elpa] externals/dash 8530742 410/426: Add @wasamasa and @occidens to list of contributors, (continued)
- [elpa] externals/dash 8530742 410/426: Add @wasamasa and @occidens to list of contributors, Phillip Lord, 2015/08/04
- [elpa] externals/dash a803dd5 414/426: Merge readme changes, Phillip Lord, 2015/08/04
- [elpa] externals/dash 1d4881f 413/426: Update README, Phillip Lord, 2015/08/04
- [elpa] externals/dash b075846 287/426: Add -iterate, -unfold, Phillip Lord, 2015/08/04
- [elpa] externals/dash 2db56f5 424/426: [-let] Add support for &as bindings (#115), Phillip Lord, 2015/08/04
- [elpa] externals/dash 47e11ae 378/426: Update docs, Phillip Lord, 2015/08/04
- [elpa] externals/dash 8dfa9b6 292/426: Release 2.6.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash c61113b 420/426: [-let] Update outdated comment, Phillip Lord, 2015/08/04
- [elpa] externals/dash c7b4cff 370/426: Update readme.md, Phillip Lord, 2015/08/04
- [elpa] externals/dash 38cd4d2 284/426: Fix fontification when there are dashes, Phillip Lord, 2015/08/04
- [elpa] externals/dash abff641 290/426: Merge pull request #76 from Fuco1/unfold,
Phillip Lord <=
- [elpa] externals/dash f780322 305/426: Add `-flatten-n`, Phillip Lord, 2015/08/04
- [elpa] externals/dash c952a54 300/426: Merge pull request #90 from tmalsburg/last-item-doc, Phillip Lord, 2015/08/04
- [elpa] externals/dash faeb61a 379/426: Merge pull request #105 from fbergroth/debug-forms, Phillip Lord, 2015/08/04
- [elpa] externals/dash 90056bd 401/426: Merge pull request #121 from holomorph/master, Phillip Lord, 2015/08/04
- [elpa] externals/dash 733274f 398/426: Merge pull request #120 from holomorph/info-manual, Phillip Lord, 2015/08/04
- [elpa] externals/dash c09c0f6 372/426: Merge branch 'more-destructuring' (#103) of https://github.com/fbergroth/dash.el, Phillip Lord, 2015/08/04
- [elpa] externals/dash 7d7a457 402/426: Allow run-tests.sh to skip tests, Phillip Lord, 2015/08/04
- [elpa] externals/dash db784f8 403/426: Merge pull request #124 from occidens/skiptests, Phillip Lord, 2015/08/04
- [elpa] externals/dash 3b63476 412/426: Move anaphoric function intro up into Functions section, Phillip Lord, 2015/08/04
- [elpa] externals/dash 7bd6b3b 400/426: Add dir entry to texinfo template, Phillip Lord, 2015/08/04