From d6e28a9f5383f0158c2bc7764bbd6050b92e3a80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Thu, 21 Mar 2019 02:37:36 +0100 Subject: Add find-min. --- util.scm | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'util.scm') 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) '()))) -- cgit v1.2.3