emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/dash 6eeb692 236/439: Provide an implementation of func


From: Phillip Lord
Subject: [elpa] externals/dash 6eeb692 236/439: Provide an implementation of function composition.
Date: Tue, 04 Aug 2015 20:28:23 +0000

branch: externals/dash
commit 6eeb692a00c33ef3061222570f18119dc95f208e
Author: Christina Whyte <address@hidden>
Commit: Christina Whyte <address@hidden>

    Provide an implementation of function composition.
---
 dash-functional.el |   10 ++++++++++
 dev/examples.el    |    8 +++++++-
 2 files changed, 17 insertions(+), 1 deletions(-)

diff --git a/dash-functional.el b/dash-functional.el
index e7f5b20..4a00720 100644
--- a/dash-functional.el
+++ b/dash-functional.el
@@ -52,6 +52,16 @@ number of args, and returns a list containing the result of
 applying each fn to the args (left-to-right)."
   (lambda (&rest args) (mapcar (lambda (x) (apply x args)) fns)))
 
+(defun -compose (&rest fns)
+  "Takes a list of functions and returns a fn that is the
+composition of those fns. The returned fn takes a variable
+number of arguments, and returns the result of applying
+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))))
+
 (defun -applify (fn)
   "Changes an n-arity function FN to a 1-arity function that
 expects a list with n items as arguments"
diff --git a/dev/examples.el b/dev/examples.el
index 6c3868e..b0756dc 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -422,7 +422,13 @@
 
     (defexamples -juxt
       (funcall (-juxt '+ '-) 3 5) => '(8 -2)
-      (-map (-juxt 'identity 'square) '(1 2 3)) => '((1 1) (2 4) (3 9))))
+      (-map (-juxt 'identity 'square) '(1 2 3)) => '((1 1) (2 4) (3 9)))
+
+    (defexamples -compose
+      (funcall (-compose 'square '+) 2 3) => (square (+ 2 3))
+      (funcall (-compose 'identity 'square) 3) => (square 3)
+      (funcall (-compose 'square 'identity) 3) => (square 3)
+      (funcall (-compose (-compose 'not 'even?) 'square) 3) => (funcall 
(-compose 'not (-compose 'even? 'square)) 3)))
 
   (defexamples -applify
     (-map (-applify '+) '((1 1 1) (1 2 3) (5 5 5))) => '(3 6 15)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]