私は
foreign export stdcall tick :: Integer -> Float -> Float -> IO Int
この関数を呼び出すたびに、その引数を haskell パイプ ライブラリから一連のパイプに渡したいと思います。
呼び出しの間に、最後の 10 回の呼び出しの引数の最小値と最大値をパイプに忘れさせたくありません。
どうすればいいですか?
私は
foreign export stdcall tick :: Integer -> Float -> Float -> IO Int
この関数を呼び出すたびに、その引数を haskell パイプ ライブラリから一連のパイプに渡したいと思います。
呼び出しの間に、最後の 10 回の呼び出しの引数の最小値と最大値をパイプに忘れさせたくありません。
どうすればいいですか?
pipes-concurrency
これは、実行するように設計されている多くのことの 1 つです。あなたがしている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