aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2019-03-21 02:37:36 +0100
committerHugo Hörnquist <hugo@lysator.liu.se>2019-03-21 02:37:36 +0100
commitd6e28a9f5383f0158c2bc7764bbd6050b92e3a80 (patch)
treecf7ccb753629143d8fdb4871dd53de2946e306f9
parentChange main-loop input. (diff)
downloadcalp-d6e28a9f5383f0158c2bc7764bbd6050b92e3a80.tar.gz
calp-d6e28a9f5383f0158c2bc7764bbd6050b92e3a80.tar.xz
Add find-min.
-rw-r--r--util.scm22
1 files changed, 21 insertions, 1 deletions
diff --git a/util.scm b/util.scm
index d19bdc85..e2151b79 100644
--- a/util.scm
+++ b/util.scm
@@ -1,9 +1,11 @@
(define-module (util)
#:use-module (srfi srfi-1)
+ #:use-module ((sxml fold) #:select (fold-values))
#:export (destructure-lambda let-multi fold-lists catch-let
for-each-in
define-quick-record define-quick-record!
- mod! sort* sort*!)
+ mod! sort* sort*!
+ find-min)
#:replace (let*)
)
@@ -126,3 +128,21 @@
(sort! items (lambda (a b)
(comperator (get a)
(get b)))))
+
+;; Finds the smallest element in @var{items}, compared with @var{<} after
+;; applying @var{foo}. Returns 2 values. The smallest item in @var{items},
+;; and the other items in some order.
+(define (find-min < ac items)
+ (if (null? items)
+ ;; Vad fan retunerar man här?
+ (values #f '())
+ (fold-values
+ (lambda (c min other)
+ (if (< (ac c) (ac min))
+ ;; Current stream head is smaller that previous min
+ (values c (cons min other))
+ ;; Previous min is still smallest
+ (values min (cons c other))))
+ (cdr items)
+ ;; seeds:
+ (car items) '())))