Gabriella439 / pipes-concurrency

Concurrency for the pipes ecosystem
BSD 3-Clause "New" or "Revised" License
43 stars 12 forks source link

Add Mailbox definition and related functions #54

Closed rainbyte closed 4 years ago

rainbyte commented 4 years ago

Given that the tutorial already uses the mailbox terminology, I was wondering if it would be possible to add the following definitions to the library:

type MailBox a = (Output a, Input a)

fromMailbox :: Mailbox a -> Proxy x' x () a IO ()
fromMailbox = fromInput . snd
toMailbox :: Mailbox a -> Proxy () a y' y IO ()
toMailbox = toOutput . fst

send' :: Mailbox a -> a -> STM Bool
send' = send . fst
recv' :: Mailbox a -> STM (Maybe a)
recv' = recv . snd

This also could solve #43, the confusing output/input part could be avoided without any compat sacrifice.

rainbyte commented 4 years ago

As an example, we could take something like this code:

(outpuFoo, inputFoo) <- spawn unbounded
(outputBar, inputBar) <- spawn unbounded
(outputBaz, inputBaz) <- spawn unbounded

_ <- forkIO $ runEffect $
  fromInput inputFoo >-> fooHandler >-> toOutput (outputBar <> outputBaz)
_ <- forkIO $ runEffect $
  frominput inputBar >-> barHandler >-> toOutput outputFoo

Which would be simplified into this one:

mbFoo <- spawn unbounded
mbBar <- spawn unbounded
mbBaz <- spawn unbounded

_ <- forkIO $ runEffect $
  fromMailbox mbFoo >-> fooHandler >-> toMailbox (mbBar <> mbBaz)
_ <- forkIO $ runEffect $
  fromMailbox mbBar >-> barHandler >-> toMailbox mbFoo

This little change makes writing the code a bit easier

Gabriella439 commented 4 years ago

@rainbyte: Yeah, I think that's a good idea and I'd accept a pull request for that

rainbyte commented 4 years ago

@Gabriel439, it is uploaded. Thanks to encourage me to make the PR

I have also added a section in the docs, but the rest is still the same.

Gabriella439 commented 4 years ago

@rainbyte: You're welcome! 🙂

rainbyte commented 4 years ago

I think this issue can be closed now :)