Stream Miscellanea
(require stream-etc) | package: stream-etc |
This library is experimental; compatibility may not be maintained.
1 Operations
procedure
(stream-group-by stream key [same?]) → (listof list?)
stream : stream? key : (-> any/c any) same? : (-> any/c any/c boolean?) = equal?
> (define (parity=? x y) (= (modulo x 2) (modulo y 2))) > (stream-group-by '("1" "2" "3" "4") string->number parity=?) '(("1" "3") ("2" "4"))
procedure
(stream-map* f stream ...) → stream?
f : procedure? stream : stream?
> (stream->list (stream-map* + '(1 2 3) '(4 5 6))) '(5 7 9)
procedure
(stream-sum stream) → number?
stream : (stream/c number?)
> (stream-sum '()) 0
> (stream-sum '(1 2 3)) 6
procedure
(stream-member? stream elem) → boolean?
stream : stream? elem : any/c
> (stream-member? '(1 2 3) 42) #f
> (stream-member? '(1 2 3) 2) #t
2 Threading
Forms that provide functionality akin to that of the Threading Macros library, only specialized to stream predicates.
syntax
(stream~> stream-expr clause ...)
clause = clause-expr | #:take clause-expr | #:repeat clause-expr = (fn-expr pre-expr ... hole-marker post-expr ...) | bare-expr hole-marker = _
An expression without a keyword should evaluate to a predicate that will be given the current prefix of the stream (starting off empty). If it’s ever #false, the entire stream~> will be #false.
A #:take clause expects a function that returns some prefix of the current stream and sets it as the current prefix of interest. The suffix then becomes the current stream. If the current stream is empty, the entire stream~> will become #true.
A #:repeat clause starts processing over from the first clause using the current stream.
> (define (ok? s) (stream~> s #:take (takef _ odd?) (negate empty?) #:take (takef _ zero?) #:repeat)) > (ok? '(1 3 0 0 1 7)) #t
> (ok? '(1 0 1 0 1 0)) #t
> (ok? '(5 8)) #f
syntax
(lambda-stream~> clause ...)
syntax
(λ-stream~> clause ...)