我试图定义一个API来表示我的程序中的特定类型的过程.
newtype Procedure a = { runProcedure :: ? }
Run Code Online (Sandbox Code Playgroud)
有状态,包括ID到记录的映射:
type ID = Int
data Record = { ... }
type ProcedureState = Map ID Record
Run Code Online (Sandbox Code Playgroud)
有三个基本操作:
-- Declare the current procedure invalid and bail (similar to some definitions of fail for class Monad)
abort :: Procedure ()
-- Get a record from the shared state; abort if the record does not exist.
retrieve :: ID -> Procedure Record
-- Store (or overwrite) a record in the shared state. …Run Code Online (Sandbox Code Playgroud) 我正在尝试加载PNG文件,获取未压缩的RGBA字节,然后将它们发送到gzip或zlib包.
pngload包将图像数据作为(StorableArray(Int,Int)Word8)返回,压缩包采用惰性ByteStrings.因此,我试图构建一个(StorableArray(Int,Int)Word8 - > ByteString)函数.
到目前为止,我尝试过以下方法:
import qualified Codec.Image.PNG as PNG
import Control.Monad (mapM)
import Data.Array.Storable (withStorableArray)
import qualified Data.ByteString.Lazy as LB (ByteString, pack, take)
import Data.Word (Word8)
import Foreign (Ptr, peekByteOff)
main = do
-- Load PNG into "image"...
bytes <- withStorableArray
(PNG.imageData image)
(bytesFromPointer lengthOfImageData)
bytesFromPointer :: Int -> Ptr Word8 -> IO LB.ByteString
bytesFromPointer count pointer = LB.pack $
mapM (peekByteOff pointer) [0..(count-1)]
Run Code Online (Sandbox Code Playgroud)
这会导致堆栈内存不足,所以很明显我做错了.我可以用Ptr和ForeignPtr尝试更多的东西,但是那里有很多"不安全"的功能.
这里的任何帮助将不胜感激; 我很难过.