Changeset 37214 in project


Ignore:
Timestamp:
02/05/19 15:37:54 (2 weeks ago)
Author:
iraikov
Message:

yasos collections: include the actual lazy implementation of lseq-filter

File:
1 edited

Legend:

Unmodified
Added
Removed
  • release/5/yasos/trunk/collections.scm

    r37133 r37214  
    66   elt-ref elt-set! elt-take elt-drop reduce reduce* reduce-items
    77   reduce-items* sort! sort make-vector-generator list->generator
    8    vector->generator string->generator hash-table->generator g-map
    9    g-reduce g-find g-filter g-zip
     8   vector->generator string->generator hash-table->generator
     9   generator->list g-map g-reduce g-find g-filter g-zip
    1010   generator->lseq lseq-first lseq-rest lseq-map lseq-map-generator
    11    lseq->list)
     11   lseq-filter lseq->list)
    1212 
    1313 (import scheme (chicken base) (chicken format) srfi-69
     
    569569
    570570  (define (g-zip gen . gs)
     571    (lambda ()
     572      (let ((value (gen)))
     573        (if (eof-object? value)
     574            (eof-object)
     575            (cons value (map (lambda (g) (g)) gs))
     576            ))
     577      ))
     578
     579  (define (generator->list gen)
    571580    (let recur ((ax '()))
    572581      (let ((value (gen)))
    573582        (if (eof-object? value)
    574583            (reverse ax)
    575             (recur (cons (cons value (map (lambda (g) (g)) gs)) ax))
     584            (recur (cons value ax))
    576585            ))
    577586      ))
     
    641650                result))))
    642651      ))
     652
     653  ;; Filter an lseq lazily to include only elements that satisfy pred
     654  (define (lseq-filter pred lseq)
     655    (let ((lseqp (make-parameter lseq)))
     656      (generator->lseq
     657       (lambda ()
     658         (let loop ((lseq1 (lseqp)))
     659           (if (null? lseq1)
     660               (eof-object)
     661               (let ((result (lseq-first lseq1)))
     662                 (cond
     663                  ((pred result)
     664                   (lseqp (lseq-rest lseq1))
     665                   result)
     666                  (else
     667                   (loop (lseq-rest lseq1)))))))))))
    643668 
    644669  (define (lseq->list lseq)
Note: See TracChangeset for help on using the changeset viewer.