Am I using reactive-banana right?

后端 未结 1 690
盖世英雄少女心
盖世英雄少女心 2021-02-01 07:06

Here\'s an example Haskell FRP program using the reactive-banana library. I\'m only just starting to feel my way with Haskell, and especially haven\'t quite got my head around w

相关标签:
1条回答
  • 2021-02-01 07:37

    (Author of reactive-banana speaking.)

    Overall, your code looks fine to me. I don't actually understand why you are using reactive-banana in the first place, but you'll have your reasons. That said, if you are looking for something like Node.js, remember that Haskell's leightweight threads make it unnecessary to use an event-based architecture.

    Addendum: Basically, functional reactive programming is useful when you have a variety of different inputs, states and output that must work together with just the right timing (think GUIs, animations, audio). In contrast, it's overkill when you are dealing with many essentially independent events; these are best handled with ordinary functions and the occasional state.


    Concerning the individual questions:

    "I'd particularly welcome any comments around whether this is a "good" use of accumE, (I'm unclear of this function will traverse the whole event stream each time although I'm guessing not)."

    Looks fine to me. As you guessed, the accumE function is indeed real-time; it will only store the current accumulated value.

    Judging from your guess, you seem to be thinking that whenever a new event comes in, it will travel through the network like a firefly. While this does happen internally, it is not how you should think about functional reactive programming. Rather, the right picture is this: the result of fromAddHandler is the complete list of input events as they will happen. In other words, you should think that recvd contains the ordered list of each and every event from the future. (Of course, in the interest of your own sanity, you shouldn't try to look at them before their time has come. ;-)) The accumE function simply transforms one list into another by traversing it once.

    I will need to make this way of thinking more clear in the documentation.

    "Also I'd like to know how one would go about pulling in messages from multiple sockets - at the moment I have on event loop inside a forever. As a concrete example of this how would I add second socket (a REQ/REP pair in zeromq parlance) to query to the current state of the IdMap inside counter?"

    If the receive function does not block, you can simply call it twice on different sockets

    linkSocketHandler s1 s2 runner1 runner2 = forever $ do 
      receive s1 [] >>= runner1 . fromString . C.unpack
      receive s2 [] >>= runner2 . fromString . C.unpack
    

    If it does block, you will need to use threads, see also the section Handling Multiple TCP Streams in the book Real World Haskell. (Feel free to ask a new question on this, as it is outside the scope of this one.)

    0 讨论(0)
提交回复
热议问题