Should one combine stateful unfolds or just compose the resulting functions between streams? #2934
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
-
Lets say I have the following:
I can combine these into a stream from
A -> C
like the following:but note, in this, I am effectively calling
Streamly.unfoldMany
twice, both forunfoldManyAB
andunfoldManyBC
However, with a helper function
liftUnfold
, I could actually combine these unfolds into one, and runStreamly.unfoldMany
only once:My current code has a number of these stateful unfolds which I'm chaining together. In general, should I be combining the unfolds using a lift and
Unfold.many
, and then callingunfoldMany
once? Or should I rununfoldMany
lots of times, each producing aStream m a -> Stream m b
style result, and then just compose the streams using ordinary function composition?Or is this something that I just need to benchmark either way?
My first thought was that lifting the unfolds into one unfold object and combining them with
many
will concentrate all the functions into oneUnfold
object, which can then be more easily optimised. However annoyingly to combinen
unfolds I actually needn
lifts, for example:but that's not too terrible. The signatures do start to get messy but they could be omitted.
The other alternative,
option1
, is to just combine the resultingStream m a -> Stream m b
style functions using usual function composition. TheStream
s hide the state type, but will jumping back and forth between streams and unfolds inhibit optimisations? Does the fact thatStream
has an existential mean that GHC will have more trouble with fusion. Or is it better just to compose functions between streams so I don't have to built a big chain ofStateT
s?Beta Was this translation helpful? Give feedback.
All reactions