Stream Miscellanea
1 Operations
2 Threading

Stream Miscellanea

Cameron Moy

 (require stream-etc) package: stream-etc

This library is experimental; compatibility may not be maintained.

1 Operations


(stream-group-by stream key [same?])  (listof list?)

  stream : stream?
  key : (-> any/c any)
  same? : (-> any/c any/c boolean?) = equal?
Groups the given stream into equivalence classes, with equivalence being determined by same?. Within each equivalence class, stream-group-by preserves the ordering of the original list. Equivalence classes themselves are in order of first appearance in the input.

> (define (parity=? x y)
    (= (modulo x 2) (modulo y 2)))
> (stream-group-by '("1" "2" "3" "4") string->number parity=?)

'(("1" "3") ("2" "4"))

2 Threading

Forms that provide functionality akin to that of the Threading Macros library, only specialized to stream predicates.


(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 = _
Returns whether the given stream satisfies the property described by the clauses.
  • 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.

A clause expression can be either a normal expression or an application with a hole. If it’s an application with a hole, it is transformed into a one-argument procedure where the argument is located at the hole.

> (define (ok? s)
    (stream~> s
              #:take (takef _ odd?)
              (negate empty?)
              #:take (takef _ zero?)
> (ok? '(1 3 0 0 1 7))


> (ok? '(1 0 1 0 1 0))


> (ok? '(5 8))



(lambda-stream~> clause ...)


(λ-stream~> clause ...)

Equivalent to (λ (st) (stream~> st clause ...)).