M algorithms.scm => algorithms.scm +5 -0
@@ 16,6 16,7 @@
generate
increasing?
init
+ juxt
product
repeat
replicate
@@ 56,6 57,10 @@
(define (init lst)
(drop-right lst 1))
+(define (juxt . procs)
+ (λ args
+ (map (λ (proc) (apply proc args)) procs)))
+
(define (product lst)
(fold-right * 1 lst))
M doc/algorithms.texi => doc/algorithms.texi +22 -0
@@ 192,6 192,28 @@ Examples:
@end example
@end deffn
+@deffn {Scheme Procedure} juxt @var{proc}@dots{}
+Takes variable number of procedures and returns a procedure that is the juxtaposition
+of those procedures. The returned procedure takes a variable number of arguments, and
+returns a list containing the result of applying each procedures to the
+arguments.
+
+This algorithm comes from Clojure's
+@code{@url{https://clojuredocs.org/clojure.core/juxt, juxt}}.
+
+Examples:
+@example
+> ((juxt first last) '(1 2 3))
+'(1 3)
+> ((juxt + *) 1 2 3 4)
+'(10 24)
+> ((juxt zip append) '(1 2 3) '(4 5 6))
+'(((1 4) (2 5) (3 6)) (1 2 3 4 5 6))
+@end example
+@end deffn
+
+
+
@deffn {Scheme Procedure} product @var{lst}
Returns the product of the elements in @var{lst}.
M tests/test-algorithms.scm => tests/test-algorithms.scm +11 -0
@@ 1,5 1,9 @@
(define-module (test-algoritms)
+ #:use-module ((srfi srfi-1) #:select (first
+ last
+ zip))
#:use-module (srfi srfi-64)
+
#:use-module (algorithms))
(module-define! (resolve-module '(srfi srfi-64))
@@ 79,6 83,13 @@
(test-equal '()
(init '(1)))
+ (test-equal '(5 6)
+ ((juxt + *) 2 3))
+ (test-equal '(1 3)
+ ((juxt first last) '(1 2 3)))
+ (test-equal '(((1 4) (2 5) (3 6)) (1 2 3 4 5 6))
+ ((juxt zip append) '(1 2 3) '(4 5 6)))
+
(test-equal 6
(product '(3 2 1)))
(test-equal 3628800