[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash b075846 287/439: Add -iterate, -unfold
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash b075846 287/439: Add -iterate, -unfold |
Date: |
Tue, 04 Aug 2015 20:29:14 +0000 |
branch: externals/dash
commit b0758469ce6121fbf2fbea72724a5679ac615e37
Author: Matus Goljer <address@hidden>
Commit: Matus Goljer <address@hidden>
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 f52d46d..d57cd9d 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)`
@@ -564,6 +572,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 4d4ef4e..34c797c 100644
--- a/dash.el
+++ b/dash.el
@@ -1089,6 +1089,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."
@@ -1367,6 +1406,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 c0ae5bf..3a932a0 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 6abc35a 277/439: Merge pull request #66 from rejeep/each-indent-level, (continued)
- [elpa] externals/dash 6abc35a 277/439: Merge pull request #66 from rejeep/each-indent-level, Phillip Lord, 2015/08/04
- [elpa] externals/dash 5d25ed2 275/439: Release 2.5.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash 930b390 278/439: Add -list function., Phillip Lord, 2015/08/04
- [elpa] externals/dash e6cae0b 279/439: Merge pull request #68 from rejeep/list-function, Phillip Lord, 2015/08/04
- [elpa] externals/dash 3eb91fe 280/439: Add `-find-last-index`, Phillip Lord, 2015/08/04
- [elpa] externals/dash 7eb8307 283/439: Merge pull request #71 from Fuco1/find-last-index, Phillip Lord, 2015/08/04
- [elpa] externals/dash 79c23bb 282/439: Merge pull request #70 from Fuco1/debug-decl, Phillip Lord, 2015/08/04
- [elpa] externals/dash b201f0c 281/439: Add debug declarations and move indent declarations into `declare`, Phillip Lord, 2015/08/04
- [elpa] externals/dash 38cd4d2 284/439: Fix fontification when there are dashes, Phillip Lord, 2015/08/04
- [elpa] externals/dash 4098ea0 285/439: Merge pull request #74 from YoungFrog/dashfontifix, Phillip Lord, 2015/08/04
- [elpa] externals/dash b075846 287/439: Add -iterate, -unfold,
Phillip Lord <=
- [elpa] externals/dash f96196a 289/439: Merge pull request #75 from Fuco1/prefix, Phillip Lord, 2015/08/04
- [elpa] externals/dash 6bdcfa5 286/439: Add -split-on, -split-when, Phillip Lord, 2015/08/04
- [elpa] externals/dash b44140a 288/439: Add -is-prefix/suffix/infix-p, Phillip Lord, 2015/08/04
- [elpa] externals/dash abff641 290/439: Merge pull request #76 from Fuco1/unfold, Phillip Lord, 2015/08/04
- [elpa] externals/dash 8dfa9b6 292/439: Release 2.6.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash f5264db 291/439: Merge pull request #77 from Fuco1/split-when, Phillip Lord, 2015/08/04
- [elpa] externals/dash 5086d24 293/439: Merge pull request #60 from rejeep/cask, Phillip Lord, 2015/08/04
- [elpa] externals/dash 43dcd37 295/439: Only eval NUM in --dotimes once, Phillip Lord, 2015/08/04
- [elpa] externals/dash 4e6a965 294/439: Still better debug declarations, Phillip Lord, 2015/08/04
- [elpa] externals/dash 703bb59 297/439: Convert -zip to support variable-length arguments, Phillip Lord, 2015/08/04