gpt4 book ai didi

haskell - 如何将外部导出函数的参数传递到管道中?

转载 作者:行者123 更新时间:2023-12-02 13:17:40 24 4
gpt4 key购买 nike

我有一个

foreign export stdcall tick :: Integer -> Float -> Float -> IO Int

每次调用此函数时,我希望将其参数传递到 haskell 管道库中的一组管道中。

在调用之间,我不希望管道忘记最后 10 次调用的参数的最小值和最大值。

我应该怎么做?

最佳答案

这是 pipes-concurrency 设计的众多功能之一。您所做的是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

关于haskell - 如何将外部导出函数的参数传递到管道中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20510454/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com