;;;; sequences-utils.scm -*- Scheme -*- ;;;; Kon Lovett, Oct '22 ;;Issues ;; (module (sequences utils) (;export ; foldl->alist ; ->list ->vector ->string ; merge merge! sorted? sort sort! ; histogram) (import scheme (chicken base) (chicken type) (only (srfi 1) list-copy) (prefix (chicken sort) chicken:) (prefix sequences seq:)) ;;; (include "sequences-utils-types") (: foldl->alist (seq (* 'a -> 'a) 'a #!optional binary-equality --> (list-of (pair * 'a)))) (: ->list (seq --> list)) (: ->vector (seq --> vector)) (: ->string (seq --> string)) (: merge! (seq seq binary-predicate -> seq)) (: merge (seq seq binary-predicate --> seq)) (: sorted? (seq binary-predicate --> boolean)) (: sort! (seq binary-predicate -> seq)) (: sort (seq binary-predicate --> seq)) (: histogram (seq #!optional binary-equality --> alist)) ;; ;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 value cur) 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))) ;expose (chicken sort) (define (merge! a b less?) (let ((rs (chicken:merge! (->list a) (->list b) less?))) (if (list? a) rs (seq:coerce a rs)) ) ) (define (merge a b less?) (let ((rs (chicken:merge (->list a) (->list b) less?))) (if (list? a) rs (seq:coerce a rs)) ) ) (define (sorted? seq less?) (let ((ss (if (vector? seq) seq (->list seq)))) (chicken:sorted? ss less?) ) ) (define (sort! seq less?) ;FIXME fill-in original from sorted-list (let* ((ss (if (vector? seq) seq (->list seq))) (rs (chicken:sort! ss less?)) ) (if (or (list? seq) (vector? seq)) rs (seq:coerce seq rs)) ) ) (define (sort seq less?) (define (*->list x) (if (list? x) (list-copy x) (->list x))) (seq:coerce seq (chicken:sort! (*->list seq) less?)) ) ;really simple stats (define (histogram seq #!optional (eqal? equal=?)) (foldl->alist seq (lambda (_ occurs) (add1 occurs)) 0 eqal?) ) ) ;module (sequences utils)