Portability | portable |
---|---|
Stability | provisional |
Maintainer | John L. Singleton <jsinglet@gmail.com> |
Safe Haskell | Safe-Inferred |
An implementation of the Sieve abstract data type in Haskell.
A Sieve is a data type with properties analogous to a physical Sieve and it is useful
for building up lists of data wherein a specific constraint must be met that cannot be achieved
using normal type semantics. A Sieve encapsulates a list that can only hold a certain type, specified by a
identity function and is preferable to creating or building up lists by using conditional blocks or by the
progressive use of filter
. This is especially advantageous if a list is to be passed around and used as an
accumulator. In such a configuration, the original declaring type is passed around with the Sieve so that it
can be used transparently in subsequent areas of the program.
A Sieve is especially useful for applications that:
- Will hold onto a list for longer than a single function
- Need to perform asynchronous stream processing
Consider the following example wherein we wish to create and maintain a list with Integer
values greater than 2.
f2 :: Sieve Int -> Sieve Int f2 s = [7,8,1] ++? s
f1 :: Sieve Int f1 = let numbersGreaterThanTwo = newSieve (\x -> x > 2) [1,2,3] in f2 $ [4,5,6] ++? numbersGreaterThanTwo
This example produces the list: [7,8,4,5,6,3]
.
Sieve Backing Types
Constructing a New Sieve
Accessing Pieces of the Sieve
relation :: Sieve a -> a -> BoolSource
Returns the backing relation used to determine if a given element is a member of the list.
Building Up Lists With a Sieve
:: [a] | The list you wish to conditionally add to the |
-> Sieve a | The |
-> Sieve a | The resulting |
The operator for building up lists with a Sieve. The operator should be read as "Conditionally Add." All interaction with Sieve should ideally be through this function/operator. The most basic usage of it can be seen in the following example:
f3 = [0,11,10] ++? ([7,8,9] ++? ([4,5,6] ++? newSieve (\x -> x > 2) [1,2,3]))
This produces the list [11,10,7,8,9,4,5,6,3]
.