How to chain states when unfolding a stream? #2908
Unanswered
clintonmead
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
So I've got a function like the following:
So what this does is grabs one
Token
s 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
unfoldBytestring
to get a stream ofToken
s. But once that unfold returnsNothing
, I want to use the same unfold to unfold the nextByteString
inbyteStringStream
into a stream ofToken
s, but this time instead seeding the unfold withinitialState
, I want to seed it with the lastState
returned 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 secondByteString
in theunfoldBytestring
along with the lastState
from the unfolding of the firstByteString
inunfoldBytestring
.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
a
fromstream
, applyinitialStateFunc
to it to gets
, thenunfold
thats
to get a stream ofb
, and continue by combining the last states
from the previous unfold with the nexta
fromstream
to get a new states
which we then useunfold
to produce more elements and so on.Beta Was this translation helpful? Give feedback.
All reactions