I have a
foreign export stdcall tick :: Integer -> Float -> Float -> IO Int
在每次调用此函数时,我希望将其参数传递到来自Haskell Pipes库的一组管道中。在调用之间,我不希望管道遗忘最近10次调用的参数的最小值和最大值。我该如何做到这一点?
I have a
foreign export stdcall tick :: Integer -> Float -> Float -> IO Int
spawn
一个缓冲区,每次调用派生的tick
函数时,它会将其参数放入该缓冲区中。然后,您可以使用管道流来输出该缓冲区中的所有内容。import Control.Concurrent.Async
import Pipes
import Pipes.Concurrent
import qualified Pipes.Prelude as P
-- Your FFI tick function, which we will wrap with a derived function
ffi_tick :: Integer -> Float -> Float -> IO Int
ffi_tick _ _ _ = return 0
-- A data structure just to simplify the types
-- In theory I could have just used a tuple
data Ticker = Ticker
{ _tick :: Integer -> Float -> Float -> IO Int
, _input :: Input (Integer, Float, Float)
}
-- This is in charge of buffer initialization and deriving the new
-- tick function
makeTicker :: IO Ticker
makeTicker = do
(output, input) <- spawn Unbounded
let tick x y z = do
atomically $ send output (x, y, z)
ffi_tick x y z
return (Ticker tick input)
-- This is example code showing how you would use it
main = do
Ticker tick input <- makeTicker
a <- async $ runEffect $ fromInput input >-> P.print
tick 1 2.0 3.0
tick 4 5.0 6.0
tick 7 8.0 9.0
wait a
MVar
来保留管道中保存的内部状态,并且从您的tick
函数的签名来看,所有操作都必须在IO
单子中发生。没有最小工作示例很难说。 - bheklilr