Created
June 1, 2012 15:29
-
-
Save cryks/2852972 to your computer and use it in GitHub Desktop.
bit-generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(use srfi-60) | |
(use gauche.generator) | |
(define (gappend-map proc . gens) | |
(generate | |
(^[yield] | |
(generator-for-each | |
(pa$ for-each yield) | |
(apply gmap proc gens))))) | |
(define (byte-generator->bit-generator gen) | |
(gappend-map (cut integer->list <> 8) gen)) | |
(use gauche.test) | |
(use util.match) | |
(test-start "bit-generator") | |
(test-section "gappend-map") | |
(test* "gappend-map" | |
'(0 1 4 9 16) | |
(generator->list (gappend-map (^x (list (* x x))) (giota 5)))) | |
(test* "gappend-map" | |
'(0 1 1 2 4 3 9 4 16 5) | |
(generator->list (gappend-map (^x (list (* x x) (+ x 1))) (giota 5)))) | |
(test* "gappend-map" | |
'(0 6 14 24 36) | |
(generator->list (gappend-map (^[x y] (list (* x y))) (giota 5) (giota 5 5)))) | |
(test-section "bit-generator") | |
(test* "bit-generator" | |
'(#f #f #f #f #f #f #f #f | |
#f #f #f #f #f #f #f #t | |
#f #f #f #f #f #f #t #f | |
#f #f #f #f #f #f #t #t | |
#f #f #f #f #f #t #f #f) | |
(generator->list (byte-generator->bit-generator (giota 5)))) | |
(with-input-from-string "hogefuga" | |
(^[] | |
(test* "bit-generator from port" | |
'(#f #t #t #f #t #f #f #f | |
#f #t #t #f #t #t #t #t | |
#f #t #t #f #f #t #t #t | |
#f #t #t #f #f #t #f #t | |
#f #t #t #f #f #t #t #f | |
#f #t #t #t #f #t #f #t | |
#f #t #t #f #f #t #t #t | |
#f #t #t #f #f #f #f #t) | |
(generator->list (byte-generator->bit-generator read-byte))))) | |
(with-input-from-string "hogefuga" | |
(^[] | |
(test* "search pattern" | |
'(3 6) | |
(values->list | |
(quotient&remainder | |
(let loop ([lis (generator->lseq (byte-generator->bit-generator read-byte))] | |
[i 0]) | |
(match lis | |
[() #f] | |
[(#f #t #f #t #t #f . _) i] | |
[(_ . rest) (loop rest (+ i 1))])) | |
8))))) | |
(test-end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment