I am playing around with the machines module by Edward Kmett, and I'm getting a little confused here and there. I thought the best way to ask a question is to provide a toy use case. Described below.
Machines one and two sit at two prongs of a Wye.
Machine one takes as param a list of ints, and pushes it down stream. Machine two takes as param a list of chars, and pushes it down stream.
Machine three keeps an internal state, beginning at mempty
, then mappends
the elements it receives from either machines from the Wye base on some condition (so not both). Machine three then gets the current state and pushes it downtream.
Machine four prints the elements it receives to console.
So far I've got this:
y1 :: PlanT k [Int] m ()
y1 = yield
y2 :: PlanT k [Char] m ()
y2 = yield
But I'm not sure how to combine y1 and y2; or roll an arbitrary process that hides a state, as opposed to using one of the stock combinators exported by Process.hs.
Per suggestion, the link to the machines package: http://hackage.haskell.org/package/machines
And a pdf giving a very high level description of what it does: https://dl.dropboxusercontent.com/u/4588997/Machines.pdf
I'm also a beginer with machines, here is my result:
I haven't used a monoid in
m3
, I used plain numbers instead, but the idea is the same. I also usedTee
instead ofWye
, because my example needs deterministic input - it chooses if it reads fromL
orR
. But usingWye
for a similar purpose would be just the same.Update: Surely it's possible to use
State
instead ofIdentity
to keep track of the count. For example:I suspect that using
repeatedly
on a plan is slightly faster than having an explicit monadic loop, but I think in this small example the difference is negligible.Or if we wanted to just count the number of strings and output it only at the end, we could use
Writer (Sum Int)
instead. Full code here.