[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash d948086 315/439: Add -iteratefn
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash d948086 315/439: Add -iteratefn |
Date: |
Tue, 04 Aug 2015 20:29:35 +0000 |
branch: externals/dash
commit d94808670f673a7d2861a0ceb6caa6a045e3e67e
Author: Matus Goljer <address@hidden>
Commit: Matus Goljer <address@hidden>
Add -iteratefn
---
README.md | 22 ++++++++++++++++++++++
dash-functional.el | 17 ++++++++++++++++-
dev/examples.el | 13 +++++++++++++
3 files changed, 51 insertions(+), 1 deletions(-)
diff --git a/README.md b/README.md
index ac54f94..1af9122 100644
--- a/README.md
+++ b/README.md
@@ -198,6 +198,7 @@ These combinators require Emacs 24 for its lexical scope.
So they are offered in
* [-not](#-not-pred) `(pred)`
* [-orfn](#-orfn-rest-preds) `(&rest preds)`
* [-andfn](#-andfn-rest-preds) `(&rest preds)`
+* [-iteratefn](#-iteratefn-fn-n) `(fn n)`
## Anaphoric functions
@@ -1665,6 +1666,27 @@ In types: [a -> Bool] -> a -> Bool
(-filter (-andfn (-not 'even?) (-cut >= 5 <>)) '(1 2 3 4 5 6 7 8 9 10)) ;; =>
'(1 3 5)
```
+#### -iteratefn `(fn n)`
+
+Return a function `fn` composed `n` times with itself.
+
+`fn` is a unary function. If you need to use a function of higher
+arity, use `-applify` first to turn it into an unary function.
+
+With n = 0, this acts as identity function.
+
+In types: (a -> a) -> Int -> a -> a.
+
+This function satisfies the following law:
+
+ (funcall (-iteratefn fn n) init) = (-last-item (-iterate fn init (1+ n))).
+
+```cl
+(funcall (-iteratefn (lambda (x) (* x x)) 3) 2) ;; => 256
+(funcall (-iteratefn '1+ 3) 1) ;; => 4
+(funcall (-iteratefn 'cdr 3) '(1 2 3 4 5)) ;; => '(4 5)
+```
+
## Contribute
diff --git a/dash-functional.el b/dash-functional.el
index 4a00720..bd77519 100644
--- a/dash-functional.el
+++ b/dash-functional.el
@@ -60,7 +60,7 @@ each fn to the result of applying the previous fn to
the arguments (right-to-left)."
(lambda (&rest args)
(car (-reduce-r-from (lambda (fn xs) (list (apply fn xs)))
- args fns))))
+ args fns))))
(defun -applify (fn)
"Changes an n-arity function FN to a 1-arity function that
@@ -120,6 +120,21 @@ PREDS returns non-nil on x.
In types: [a -> Bool] -> a -> Bool"
(lambda (x) (-all? (-cut funcall <> x) preds)))
+(defun -iteratefn (fn n)
+ "Return a function FN composed N times with itself.
+
+FN is a unary function. If you need to use a function of higher
+arity, use `-applify' first to turn it into an unary function.
+
+With n = 0, this acts as identity function.
+
+In types: (a -> a) -> Int -> a -> a.
+
+This function satisfies the following law:
+
+ (funcall (-iteratefn fn n) init) = (-last-item (-iterate fn init (1+ n)))."
+ (lambda (x) (--dotimes n (setq x (funcall fn x))) x))
+
(provide 'dash-functional)
;;; dash-functional.el ends here
diff --git a/dev/examples.el b/dev/examples.el
index aabd343..220e846 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -712,4 +712,17 @@
(funcall (-andfn (-cut < <> 10) 'even?) 6) => t
(funcall (-andfn (-cut < <> 10) 'even?) 12) => nil
(-filter (-andfn (-not 'even?) (-cut >= 5 <>)) '(1 2 3 4 5 6 7 8 9 10))
=> '(1 3 5))
+
+ (defexamples -iteratefn
+ (funcall (-iteratefn (lambda (x) (* x x)) 3) 2) => 256
+ (funcall (-iteratefn '1+ 3) 1) => 4
+ (funcall (-iteratefn 'cdr 3) '(1 2 3 4 5)) => '(4 5)
+ (let ((init '(1 2 3 4 5))
+ (fn 'cdr))
+ (and (equal (funcall (-iteratefn fn 0) init)
+ (-last-item (-iterate fn init (1+ 0))))
+ (equal (funcall (-iteratefn fn 3) init)
+ (-last-item (-iterate fn init (1+ 3))))
+ (equal (funcall (-iteratefn fn 5) init)
+ (-last-item (-iterate fn init (1+ 5)))))))
))
- [elpa] externals/dash cd137e0 310/439: `-slice` should not fill the returned list with nils if to > length, (continued)
- [elpa] externals/dash cd137e0 310/439: `-slice` should not fill the returned list with nils if to > length, Phillip Lord, 2015/08/04
- [elpa] externals/dash 6b64ea7 303/439: Add functions for flexibly zipping uneven lists, Phillip Lord, 2015/08/04
- [elpa] externals/dash 63ec298 308/439: Merge pull request #92 from Fuco1/outer-product, Phillip Lord, 2015/08/04
- [elpa] externals/dash 7185db8 311/439: [Issue #83] Add `step` to `-slice`, Phillip Lord, 2015/08/04
- [elpa] externals/dash a3021eb 313/439: Merge pull request #93 from lunaryorn/patch-1, Phillip Lord, 2015/08/04
- [elpa] externals/dash 624c501 306/439: Add `-table` and `-table-flat`, Phillip Lord, 2015/08/04
- [elpa] externals/dash 0384eee 312/439: Improve Travis CI configuration, Phillip Lord, 2015/08/04
- [elpa] externals/dash 3132ae0 314/439: Alias -tail to nthcdr, Phillip Lord, 2015/08/04
- [elpa] externals/dash 2ecc073 316/439: Reorder the partition examples to follow more logical order, add tests, Phillip Lord, 2015/08/04
- [elpa] externals/dash bbc1d9c 309/439: `--each-while` should also expose `it-index`, Phillip Lord, 2015/08/04
- [elpa] externals/dash d948086 315/439: Add -iteratefn,
Phillip Lord <=
- [elpa] externals/dash 7f0fadc 317/439: Add -prodfn, Phillip Lord, 2015/08/04
- [elpa] externals/dash eea928a 320/439: Add -replace, Phillip Lord, 2015/08/04
- [elpa] externals/dash f3b0a55 321/439: Change -predicate-p examples to -predicate? to maintain consistency, Phillip Lord, 2015/08/04
- [elpa] externals/dash a09a4b2 318/439: Add code markup for docstrings and fix erroneous whitespace in docstring, Phillip Lord, 2015/08/04
- [elpa] externals/dash 7e4adb5 319/439: Add missing keywords to highlight list, Phillip Lord, 2015/08/04
- [elpa] externals/dash 174747e 323/439: Add -splice and -splice-list, Phillip Lord, 2015/08/04
- [elpa] externals/dash 5637bd6 322/439: Add alias from -find to -first, Phillip Lord, 2015/08/04
- [elpa] externals/dash 9ebcce6 325/439: Add some niceties to font-locking of examples, Phillip Lord, 2015/08/04
- [elpa] externals/dash f8873a0 327/439: Update readme-template with changes from last release, Phillip Lord, 2015/08/04
- [elpa] externals/dash f18a520 326/439: Release 2.7.0, Phillip Lord, 2015/08/04