Changeset 36586 in project


Ignore:
Timestamp:
09/11/18 06:09:07 (6 weeks ago)
Author:
iraikov
Message:

yasos: added a subset of srfi-127 lseqs to collections module

Location:
release/5/yasos/trunk
Files:
2 edited

Legend:

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

    r36391 r36586  
    1010              string->generator hash-table->generator
    1111              g-map g-reduce g-find g-filter
     12              generator->lseq lseq-first lseq-rest
    1213              )
    1314 
     
    4748;;  for-each-elt
    4849;;
    49 ;; Collections may optionally implement random access operations:
     50;; Collections may optionally implement:
     51;; 1) random access operations:
    5052;;
    5153;; elt-ref
     
    5456;; elt-drop
    5557;;
    56  ;;==============================
     58
    5759 
    5860 (define *eof-object* (read (open-input-string "")))
     
    8587    (else #f)
    8688    ))
     89
    8790 
    8891 (define-operation (random-access? obj)
     
    9295    (else #f)
    9396    ))
    94  
     97
    9598 
    9699 (define (empty? collection) (zero? (size collection)))
     
    567570      ))
    568571
     572;;;; SRFI 127 lazy sequences
     573;;; Convert a generator (procedure with no arguments) to an lseq
     574;;; This is the basic constructor for lseqs, since every proper list
     575;;; is already an lseq and so list->lseq is not needed
     576
     577  (define (generator->lseq gen)
     578    (let ((value (gen)))
     579      ;; See what starts off the generator:
     580      ;; if it's already exhausted, the lseq is empty,
     581      ;; otherwise, return an improper list with one value and the generator
     582      ;; in the tail, which is how we represent unrealized lseqs
     583      (if (eof-object? value)
     584          '()
     585          (cons value gen))))
     586
     587
     588  (define (lseq-first lseq) (car lseq))
     589
     590;;; Lseq-cdr expands the generator if it's there, or falls back to regular cdr
     591  (define (lseq-rest lseq)
     592    ;; We assume lseq is a pair, because it is an error if it isn't
     593    ;; If it's a procedure, we assume it's a generator and invoke it
     594    (if (procedure? (cdr lseq))
     595        (let ((obj ((cdr lseq))))
     596          (cond
     597           ;; If the generator is exhausted, replace with () and return ()
     598           ((eof-object? obj)
     599            (set-cdr! lseq '())
     600            '())
     601           ;; Otherwise, make a new pair of the value and the generator
     602           ;; and patch it in to the cdr
     603           (else (let ((result (cons obj (cdr lseq))))
     604                   (set-cdr! lseq result)
     605                   result))))
     606        ;; If there is no procedure, return the ordinary cdr
     607        (cdr lseq)))
    569608
    570609 )
  • release/5/yasos/trunk/tests/run.scm

    r36391 r36586  
    343343    (test "sort" #(7 8 9 10 11) (sort (lambda (i vi j vj) (< vi vj))
    344344                                    '( 11 8 10 7 9)))
     345    (let ((l (generator->lseq (gen-elts t))))
     346      (test "lseq" '(2 1) (list (lseq-first l) (lseq-first (lseq-rest l)))))
    345347   
    346348    )
Note: See TracChangeset for help on using the changeset viewer.