Finite yielding in side effecting source #1494
-
|
I don't know if is the right way of using pipes, but I have this pipe So basically I want to stop yielding when the Slice has lenght zero (since that means that the client closed on us) but I don't see anything exposed publicly that allows me to fulfill this use case and I have checked the source code and see that I could use SourceT but to me looks like is not going to dispose of action of IO and is going to keep producing empty slices just that they aren't going to be pushed downstream. In this case what would be the best thing to do or is this just forcing pipes to do something is not supposed to do. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
|
I'm unable to test this out, but something like this should work: public static PipeT<Socket, Slice<byte>, M, Unit> receive(int bufferLength) =>
receiveAny(bufferLength) | whileNotEmpty;
static PipeT<Socket, Slice<byte>, M, Unit> receiveAny(int bufferLength) =>
from client in PipeT.awaiting<M, Socket, Slice<byte>>()
from _ in PipeT.yieldRepeatIO<M, Socket, Slice<byte>>(read(client, bufferLength))
select unit;
static readonly PipeT<Slice<byte>, Slice<byte>, M, Unit> whileNotEmpty =
from slice in PipeT.awaiting<M, Slice<byte>, Slice<byte>>()
from _ in slice.Length == 0
? Pure(unit)
: PipeT.yield<M, Slice<byte>, Slice<byte>>(slice)
select unit;
static IO<Slice<byte>> read(Socket client, int bufferLength) =>
IO.liftAsync(async () =>
{
var buffer = new byte[bufferLength];
var length = await client.ReceiveAsync(buffer);
return new Slice<byte>(length, 0, buffer);
}); |
Beta Was this translation helpful? Give feedback.
Haskell Pipes supports both push and pull models, it really depends on the
Producerand theConsumerto what the exact behaviour will be. Language-Ext Pipes used to be an exact clone of Haskell Pipes -- well, almost, the old version could only liftEff<RT, A>into its transformer stack, but other than that the behaviour was a 1-to-1 match.The new version of Language-Ext Pipes is fully generalised to use any
Mconstrained toMonadIObut also the whole library has been completely written to…