- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是一个使用 reactive-banana 库的 Haskell FRP 程序示例。我才刚刚开始对 Haskell 有所了解,尤其是还没有完全理解 FRP 的含义。我真的很感激对下面的代码的一些批评
{-# LANGUAGE DeriveDataTypeable #-}
module Main where
{-
Example FRP/zeromq app.
The idea is that messages come into a zeromq socket in the form "id state". The state is of each id is tracked until it's complete.
-}
import Control.Monad
import Data.ByteString.Char8 as C (unpack)
import Data.Map as M
import Data.Maybe
import Reactive.Banana
import System.Environment (getArgs)
import System.ZMQ
data Msg = Msg {mid :: String, state :: String}
deriving (Show, Typeable)
type IdMap = Map String String
-- | Deserialize a string to a Maybe Msg
fromString :: String -> Maybe Msg
fromString s =
case words s of
(x:y:[]) -> Just $ Msg x y
_ -> Nothing
-- | Map a message to a partial operation on a map
-- If the 'state' of the message is "complete" the operation is a delete
-- otherwise it's an insert
toMap :: Msg -> IdMap -> IdMap
toMap msg = case msg of
Msg id_ "complete" -> delete id_
_ -> insert (mid msg) (state msg)
main :: IO ()
main = do
(socketHandle,runSocket) <- newAddHandler
args <- getArgs
let sockAddr = case args of
[s] -> s
_ -> "tcp://127.0.0.1:9999"
putStrLn ("Socket: " ++ sockAddr)
network <- compile $ do
recvd <- fromAddHandler socketHandle
let
-- Filter out the Nothings
justs = filterE isJust recvd
-- Accumulate the partially applied toMap operations
counter = accumE M.empty $ (toMap . fromJust <$> justs)
-- Print the contents
reactimate $ fmap print counter
actuate network
-- Get a socket and kick off the eventloop
withContext 1 $ \ctx ->
withSocket ctx Sub $ \sub -> do
connect sub sockAddr
subscribe sub ""
linkSocketHandler sub runSocket
-- | Recieve a message, deserialize it to a 'Msg' and call the action with the message
linkSocketHandler :: Socket a -> (Maybe Msg -> IO ()) -> IO ()
linkSocketHandler s runner = forever $ do
receive s [] >>= runner . fromString . C.unpack
最佳答案
(reactive-banana 的作者发言。)
总的来说,你的代码对我来说看起来不错。我实际上不明白你为什么首先使用 react 香蕉,但你会有你的理由。也就是说,如果您正在寻找类似 Node.js 的东西,请记住 Haskell 的轻量级线程 make it unnecessary使用基于事件的架构。
附录:基本上,当您有各种不同的输入、状态和输出必须在正确的时间(想想 GUI、动画、音频)下协同工作时,函数式响应式(Reactive)编程很有用。相比之下,当您处理许多本质上独立的事件时,它就显得过分了。这些最好用普通函数和偶尔的状态来处理。
关于个别问题:
"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)."
accumE
功能确实是实时的;它只会存储当前的累积值。
fromAddHandler
的结果是输入事件的完整列表,因为它们将发生。换句话说,你应该认为
recvd
包含 future 每个事件的有序列表。 (当然,为了您自己的理智,您不应该在他们的时间到来之前尝试查看它们。;-))
accumE
函数只需遍历一次即可将一个列表转换为另一个列表。
"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?"
receive
函数不会阻塞,你可以简单地在不同的套接字上调用它两次
linkSocketHandler s1 s2 runner1 runner2 = forever $ do
receive s1 [] >>= runner1 . fromString . C.unpack
receive s2 [] >>= runner2 . fromString . C.unpack
关于haskell - 我使用的是 react 香蕉吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6812533/
我正在开发一个基于终端的小型 UI,我想使用 Reactive Banana 来描述交互。我唯一感兴趣的外部事件是用户是否按下了某个键。 根据我从 Frameworks 文档中收集到的信息,我可以使用
我需要创建一些自定义滚动动画 - 并想开始探索为一个简单的 block 制作动画。 使用 json - 我想提供 block 的骨架(类名、高度、宽度、背景),然后是与滚动值相关的每个开始/结束帧的
Android 比较 2 个图像以使用位图代码并告诉水果类别是水果(苹果/香蕉)还是不是水果。 我有问题与 Bitmap 和 BitmapFactory 比较有运行时错误,我有问题的解决方案。 act
我目前正在尝试通过 Heinrich Apfelmus 的 reactive-banana 了解 FRP ,与我看过的其他库相比,它似乎是一个文档齐全且简单的库。 但是,我无法理解 AddHandle
我有一个基于 Reactive Banana 的界面 (WX)。现在我对如何真正管理状态有不同的问题: 我应该将状态视为我在代码中定义的行为吗? 如果状态也依赖于外部“事件”,那么不仅仅与 GUI 相
假设我有一个事件触发器,我想在触发时做两件事。首先,我希望它更新某些行为的值。其次,如果满足其他条件,我希望它使用行为的更新值触发另一个事件 send_off 。以代码形式表达,假设我有 trigge
我尝试更改处理器中的 GPIO 数据(不使用 wiringPI 库) int fd = open ("/dev/mem", O_RDWR | O_SYNC); unsigned long* gpio1
从这里的上一个问题开始: Reactive Banana: how to use values from a remote API and merge them in the event stream
我正在考虑类似gnugo的东西的ascii接口(interface)。我认为轮流是一对共同进化(允许用户输入的单子(monad))流。这让我隐约想起了管道。 我不太确定如何使用现有的库来做到这一点。
我想检索小部件值。 在下面,按下按钮 b 检索 s_in 并在 native wxhaskell 中打印它。 b >= putStrLn] 我喜欢在 react-banana 上做同样的事情,但在下面
我在 WX 界面中使用 Reactive-Banana。当按下按钮时,我需要从外部服务 API 检索值。 我有一个基于数据类型AppState的通用Behavior,它根据函数转换(doSomeTra
在 reactive-banana a -> Maybe a -> Maybe a -> Bool -> Event t b -> Event t c -> Behavior t
嗨,我正在尝试安装(无需更新或安装最新的编译器),reactive-banana-wx 和要求之一失败 cabal install reactive-banana-wx 这是错误 Configurin
我是一名优秀的程序员,十分优秀!