;;;; sequences-utils.misc.scm -*- Scheme -*- ;;;; Kon Lovett, Oct '22 ;;Issues ;; (module (sequences utils misc) (;export ; reduce ; foldl->alist ; ->list ->vector ->string) (import scheme (chicken base) (chicken type) (chicken fixnum) (chicken random) (only (srfi 1) list-copy reverse! append! take!) (prefix (chicken sort) chicken:) (prefix sequences seq:)) ;;; (include "sequences-utils-types") (: reduce (('a 'a -> 'a) 'a seq --> 'a)) (: foldl->alist (seq ('a * -> 'a) 'a #!optional binary-equality --> (list-of (pair * 'a)))) (: ->list (seq --> list)) (: ->vector (seq --> vector)) (: ->string (seq --> string)) ;; ;srfi-1 reduce (define (reduce f seed seq) (if (seq:empty? seq) seed (seq:foldl f seed seq)) ) ;miscellaneous (define (foldl->alist seq next seed #!optional (eqal? equal=?)) (define (kons bins value) (let ((cur (alist-ref value bins eqal? seed))) (alist-update! value (next cur value) bins eqal?)) ) (seq:foldl kons '() seq) ) ;basic coercions (define (->list x) (if (list? x) x (seq:coerce '() x))) (define (->vector x) (if (vector? x) x (seq:coerce #() x))) (define (->string x) (if (string? x) x (seq:coerce "" x))) ) ;module (sequences utils misc)