Understanding Streams, Patterns and Events - Part 2:
Filter:

Understanding Streams, Patterns and Events - Part 2

Patterns Introduction

Patterns

Often one wants to be able to create multiple streams from a single stream specification. Patterns are just a way to make multiple Streams from a single specification, like a cookie cutter. A pattern can be any object that responds to the asStream message by creating a Stream. Once again there is a default implementation in class Object of asStream that simply returns the receiver as its own stream. Thus any object is by default a pattern that returns itself as a stream when sent the asStream message.

Pattern and its subclasses

There is a class named Pattern that provides more functionality for the concept of a pattern.

A Pfunc is a Pattern that returns a FuncStream. The same function arguments are supplied as are supplied to FuncStream.

A Prout is a Pattern that returns a Routine. The same function argument is supplied as is supplied to Routine.

A Pseries is a Pattern that generates an arithmetic series.

Pgeom is a Pattern that generates a geometric series.

Math operations on Patterns

Patterns also respond to math operators by returning patterns that respond to asStream with appropriately modified streams.

Applying a unary operator to a pattern

Using a binary operator on a pattern

Filtering operations on patterns

Patterns also respond to the messages collect, select, and reject by returning a new Pattern.

The collect message returns a Pattern whose Stream is modified by a function in the same way as the collect message sent to a Collection returns a modified Collection.

The select message creates a pattern whose stream passes only items that return true from a user supplied function.

The reject message creates a pattern whose stream passes only items that return false from a user supplied function.

Making Music with Patterns

Here is a variation of the example given in part 1 that uses a Pattern to create two instances of the random melody stream.

To go to the next file: Understanding Streams, Patterns and Events - Part 3