[Haskell-cafe] Streaming a Conduit into a lazy list
jules at jellybean.co.uk
Tue Nov 17 09:13:30 UTC 2015
(Apologies for coming back after 2 weeks)
On 4 Nov 2015, at 19:41, Michael Snoyman <michael at snoyman.com> wrote:
> On Wed, Nov 4, 2015 at 9:28 AM, Jules Bean <jules at jellybean.co.uk> wrote:
> On 4 Nov 2015, at 13:04, Michael Snoyman <michael at snoyman.com> wrote:
>> This got me curious, so I just added a `sourceToList` function to master:
> Having spent a day thinking this over…
> Nothing with the type `Source m a -> m [a]` can work for my second example - the one where I use runExceptionT to discharge the MonadThrow constraint. This is because once you use runExceptionT you have pushed yourself into the situation where in case of error there is no ‘return value’.
> I don't think that the Writer example above is demonstrating what you're saying, since you're using imprecise exceptions (`error`) instead of MonadThrow. You could do the same thing with sourceToList and get that result.
There was another example where I don’t use imprecise exceptions, but I use ExceptionT:
> source123Throw :: MonadThrow m => Source m Int
> source123Throw = do
> yield 1
> yield 2
> yield 3
> throwM MyError
*Main Control.Monad.Except Control.Monad.Trans.Resource> snd . runWriter . runExceptionT $ (source123Throw $$ tellEverything)
The point here is the order of nesting runWriter(T) and runException(T). If you do it this way around the type is `([a],Either Exception ())` and you can just inspect the first element of the tuple to check the list without checking for an error - which allows it to be streaming.
`Source m a -> m [a]` can only take this second form.
More information about the Haskell-Cafe