ekmett / machines

Networks of composable stream transducers
Other
340 stars 46 forks source link

starve could be more general #53

Closed treeowl closed 9 years ago

treeowl commented 9 years ago

Currently,

-- | Run a machine with no input until it stops, then behave as another machine..
starve :: Monad m => ProcessT m a b -> MachineT m k b -> MachineT m k b
starve m cont = MachineT $ runMachineT m >>= \v -> case v of
  Stop            -> runMachineT cont -- Continue with cont instead of stopping
  Yield o r       -> return $ Yield o (starve r cont)
  Await _ Refl r  -> runMachineT (starve r cont)

There is no need to use the Refl, and thus no need to restrict the machine to be a ProcessT.

starve :: Monad m => MachineT m _k b -> MachineT m k b -> MachineT m k b
starve m cont = MachineT $ runMachineT m >>= \v -> case v of
  Stop            -> runMachineT cont -- Continue with cont instead of stopping
  Yield o r       -> return $ Yield o (starve r cont)
  Await _ _ r  -> runMachineT (starve r cont)

If there's actually some reason to force that argument, of course, we could go with

  Await _ !_ r  -> runMachineT (starve r cont)
treeowl commented 9 years ago

I imagine perhaps this was originally implemented as starve m cont = (stopped ~> m) <> cont and then optimized to the present form before it was first committed.

statusfailed commented 9 years ago

Actually the original form was as you see it- I knew of neither stopped nor the Semigroup instance when I wrote that- it was just a stepping stone to get to groupingOn that I thought might be useful to others.

I don't know what performance implications of changing this to be (stopped ~> m) <> cont would be (if any), but it'd be much more concise.

ekmett commented 9 years ago

I'm not terribly worried about the conciseness of the internal implementation -- If need be we can document laboriously derived internals with such pithy phrases and get much of the expository benefits without paying any performance hit.

In any event, let's generalize starve.

treeowl commented 9 years ago

See pull request #55

ekmett commented 9 years ago

Merged.