How to chain states when unfolding a stream? #2908
Unanswered
clintonmead
asked this question in
Q&A
Replies: 1 comment
-
|
Turning |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
So I've got a function like the following:
So what this does is grabs one
Tokens slices a bit off the front of theByteString(we can do this without copying withByteString) and gives both aToken, and the remainingByteString, plus someState.We can then do:
But here's what I want to do. Lets say we have an incoming stream of bytestrings:
What I want to do is state with an initial state say:
and feed that into
unfoldBytestringto get a stream ofTokens. But once that unfold returnsNothing, I want to use the same unfold to unfold the nextByteStringinbyteStringStreaminto a stream ofTokens, but this time instead seeding the unfold withinitialState, I want to seed it with the lastStatereturned from the previous fold (i.e. just before the previous unfold returnedNothing).Note here the full "state" is
(ByteString, State), so the second unfold I want to seed with the secondByteStringin theunfoldBytestringalong with the lastStatefrom the unfolding of the firstByteStringinunfoldBytestring.Is there a function/combinator to do this? Or should I write my own? It seems close to
unfoldMany, I just need a way to collect the final states from the unfolds and pass them along. I think the signature I need is something like this:Where we take the first element
afromstream, applyinitialStateFuncto it to gets, thenunfoldthatsto get a stream ofb, and continue by combining the last statesfrom the previous unfold with the nextafromstreamto get a new stateswhich we then useunfoldto produce more elements and so on.Beta Was this translation helpful? Give feedback.
All reactions