Skip to content

Commit a411b97

Browse files
committed
Elaborate on the documentation for ListT
This implements several of the documentation ideas I brought up in Gabriella439#184. The present problem, I think, is that it is not obvious how to accomplish some basic ListT tasks (e.g. converting `ListT m a` to `m [a]`) unless you have the habit of knowing to convert to Producer first. I also wanted to emphasize some of the things that you can do using just the typeclass instances. I'm aiming to augment the ListT API documentation with just enough links and suggestions to serve as jumping-off points, without cluttering up the haddock page for the Pipes module too much.
1 parent 9f3472d commit a411b97

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

src/Pipes.hs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,10 +401,39 @@ p1 >-> p2 = (\() -> p1) +>> p2
401401

402402
{-| The list monad transformer, which extends a monad with non-determinism
403403
404-
'return' corresponds to 'yield', yielding a single value
405-
406-
('>>=') corresponds to 'for', calling the second computation once for each
407-
time the first computation 'yield's.
404+
The type variables signify:
405+
406+
* @m@ - The base monad
407+
* @a@ - The values that the computation 'yield's throughout its execution
408+
409+
For basic construction and composition of 'ListT' computations, much can be
410+
accomplished using common typeclass methods.
411+
412+
* 'return' corresponds to 'yield', yielding a single value.
413+
* ('>>=') corresponds to 'for', calling the second computation once
414+
for each time the first computation 'yield's.
415+
* 'mempty' neither 'yield's any values nor produces any effects in the
416+
base monad.
417+
* ('<>') sequences two computations, 'yield'ing all the values of the
418+
first followed by all the values of the second.
419+
* 'lift' converts an action in the base monad into a ListT computation
420+
which performs the action and 'yield's a single value.
421+
422+
'ListT' is a newtype wrapper for 'Producer'. You will likely need to use
423+
'Select' and 'enumerate' to convert back and forth between these two types
424+
to take advantage of all the 'Producer'-related utilities that
425+
"Pipes.Prelude" has to offer.
426+
427+
* To lift a plain list into a 'ListT' computation, first apply 'each'
428+
to turn the list into a 'Producer'. Then apply the 'Select'
429+
constructor to convert from 'Producer' to 'ListT'.
430+
* For other ways to construct 'ListT' computations, see the
431+
“Producers” section in "Pipes.Prelude" to build 'Producer's.
432+
These can then be converted to 'ListT' using 'Select'.
433+
* To aggregate the values from a 'ListT' computation (for example,
434+
to compute the sum of a 'ListT' of numbers), first apply
435+
'enumerate' to obtain a 'Producer'. Then see the “Folds”
436+
section in "Pipes.Prelude" to proceed.
408437
-}
409438
newtype ListT m a = Select { enumerate :: Producer a m () }
410439

0 commit comments

Comments
 (0)