-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Chaining unfolds #2911
Comments
Turning |
For each input element from the I have two thoughts how to do this now:
I feel like if I keep things inside the library framework (i.e. not doing 2) things will optimise better but I'm not sure how to approach that exactly. The issue I think with your |
@clintonmead for stateful transformation of an unfold you could scan or postscan an Unfold. There are
The output type |
@harendra-kumar sorry I must be still missing something. |
I thought you wanted to introduce a state in an existing unfold, scanning an unfold can do that. But it seems you want to share a state across multiple invocations of an unfold. The only way to do that using the existing unfolds is by using StateT so that you can use the underlying monad to share the state. To share the state explicitly, the signature you proposed looks good:
But this will require a separate type so that we can incorporate the state parameter in the type. For your purposes you can try it out by writing it outside the library and see how it works. Also see how the StateT solution fares compared to this. |
I ended up using Strictly's Thanks for all your help! |
I have an
Unfold m (s, a) b
, and I also have aStream a
. What I want to do is produce aStream b
, which is the result of repeatedly applying my unfold to each element in myStream a
and concatenating the results. The issue is my unfold also requires a states
. I want to be able to set this initial states
for the first element fromStream a
, and then have the states
be the "end state" of the unfold for each subsequent element of stream a. Kind of chaining the unfolds together, where the end state of each one is fed into the next unfold on the next element from the input stream.The issue is that
Unfold
doesn't have an end state. I think this is becauseUnfold
is likeConduit i o m ()
. But I need some sort of result (I think).One alternative could be to modify the step data type, like so:
But this is a pretty big change. You could do this:
But still this is a decent sized change.
The other thing I thought of was keeping the state in the output of the
Unfold
(which one could thenmap
out latter on). Basically change yourUnfold m (s, a) b
->Unfold m (s, a) (s, b)
.Then we can write a function like this:
Which is basically
unfoldMany
, but with an extra function that allows one to change what value goes to the unfold based on:This allows one to chain state between unfolds.
But these seem like somewhat hacky solutions, and I don't think what I'm doing here is that weird. Basically I have an incoming stream of chunks of data, and I want to produce an outgoing stream of tokens, but it's important that I keep some state between the chunks (like, have we got an unclosed quote from the previous chunk). Is there a better way to do this with the existing combinators?
(This is a follow one from the discussion topic here: #2908, but I've made this into an issue now because I actually can provide some concrete code).
Edit: Here's an alternate implementation, that avoids an intermediate
Unfold
:The text was updated successfully, but these errors were encountered: