diff options
author | Hugo Hörnquist <hugo@lysator.liu.se> | 2019-03-21 02:37:36 +0100 |
---|---|---|
committer | Hugo Hörnquist <hugo@lysator.liu.se> | 2019-03-21 02:37:36 +0100 |
commit | d6e28a9f5383f0158c2bc7764bbd6050b92e3a80 (patch) | |
tree | cf7ccb753629143d8fdb4871dd53de2946e306f9 /util.scm | |
parent | Change main-loop input. (diff) | |
download | calp-d6e28a9f5383f0158c2bc7764bbd6050b92e3a80.tar.gz calp-d6e28a9f5383f0158c2bc7764bbd6050b92e3a80.tar.xz |
Add find-min.
Diffstat (limited to 'util.scm')
-rw-r--r-- | util.scm | 22 |
1 files changed, 21 insertions, 1 deletions
@@ -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) '()))) |