From 9dd7d9c74b04a1f25ac2adf75cc464a2a9149b50 Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Wed, 8 Apr 2026 20:33:21 +0100 Subject: Add foldn. --- kaakaa/utils.scm | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/kaakaa/utils.scm b/kaakaa/utils.scm index 5677af2..a922ef2 100644 --- a/kaakaa/utils.scm +++ b/kaakaa/utils.scm @@ -22,6 +22,7 @@ #:use-module (ice-9 match) #:use-module (ice-9 popen) #:export (-> + foldn alist->plist call-with-input-pipe)) @@ -43,6 +44,27 @@ For example: => 16" (->-helper x (cut proc ...) ...)) +(define (foldn proc lst . inits) + "Apply @var{proc} to the elements of @var{lst} to build a result, and return +that result. @var{proc} may return multiple values, in which case, an equal +number of values are returned. Each @var{proc} call is @code{(proc element +previous ...)} where @code{element} is an element of @var{lst}, and +@code{(previous ...)} is the return from the previous call to @var{proc} or the +given @var{inits} for the first call. For example, + +(foldn (lambda (n sum sum-of-squares) + (values (+ sum n) + (+ sum-of-squares (expt n 2)))) + (iota 10) + 0 0) +=> 45 +=> 285" + (apply values + (fold (lambda (element results) + (call-with-values (cut apply proc element results) list)) + inits + lst))) + (define (alist->plist alist) "Convert association list @var{alist} to a property list. Keys in @var{alist} are converted to keywords." -- cgit 1.4.1