procrastinating-structure-1.0.1: Pure structures that can be incrementally created in impure code

Data.PVar.Queue

Description

A procrastinating queue. You can populate the back of the queue in IO and read the front of the queue in pure code. The front of an empty, unclosed queue is _|_. I think it fits the definition of referentially transparent, but it's possible to do some really stupid things with one of these Queues. If you read the source, this serves as an example of using Data.PVar.Structure. Here's a simple example of using a Queue:

 import Prelude hiding (sum)
 import Data.Foldable (sum)

 main :: IO ()
 main = do
   (back, front) <- newQueue  -- Create a new queue.
   mapM_ (push back) [0..9]   -- Push some values to the back of the queue.
   print $ peek front         -- Safe to do since we know something has been written
   close back                 -- Close the queue.
   print $ sum front          -- Safe to do since the queue is finalized

The output of the above program is:

 Just 0
 45

Is this useful? Who knows? It was a fun exercise.

Synopsis

Documentation

data QueueBack a Source

The impure back of a pure queue.

Instances

newQueue :: IO (QueueBack a, Queue a)Source

Create both ends of a procrastinating queue.

push :: QueueBack a -> a -> IO BoolSource

Push to the back of an open QueueBack. If the QueueBack has been closed, returns False.

close :: QueueBack a -> IO ()Source

Close a QueueBack. This finalizes the Queue and means that it is safe to evaluate all the way to the end.

pop :: Queue a -> Maybe (Queue a, a)Source

Get the value at the front of a Queue and return the remainder of the Queue. Returns False if we are at the end of a closed Queue.

peek :: Queue a -> Maybe aSource

Get the value at the front of a Queue. Returns False if we are at the end of a closed Queue.