Stream Miscellanea
1 Operations
stream-group-by
stream-sum
stream-member?
2 Threading
stream~>
lambda-stream~>
λ-stream~>
8.1

Stream Miscellanea

Cameron Moy

 (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?
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.

Examples:
> (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-sum stream)  number?

  stream : (stream/c number?)
Returns the sum of numbers in a stream.

Examples:
> (stream-sum '())

0

> (stream-sum '(1 2 3))

6

procedure

(stream-member? stream elem)  boolean?

  stream : stream?
  elem : any/c
Returns if elem is in the given stream.

Examples:
> (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 = _
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.

Examples:
> (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 ...)

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