我有一个向量列表 - 类型集是已知和固定的 - 让我们说,CInt和CChar.该列表在编译时是未知的 - 组合将在运行时从配置文件中确定.例如,我们可能决定需要将两个向量传递给C函数:一个CInt长度为10的CChar向量,一个长度为50的向量.至于C函数如何解释它们,我可以通过传递每个向量的向量编码类型来处理该逻辑(比如说,0 => CInt,1 => CChar),并且传递了每个矢量的矢量编码长度(10,50).
我想弄清楚的是如何生成混合向量的向量(仅用于传递给C).我尝试了类似下面的玩具解决方案(它模拟相同的想法 - 生成Ptr混合类型的可存储向量- 在实际代码中,每个Ptr将指向另一个可存储向量).它因类型错误而失败 - 我怀疑它与之前在我之前提到的另一个问题中指出的存在性合格类型有关.由于我使用Storable实例传递给C FFI,我想我无法包装类型(没有定义另一个可存储的实例).
{-# LANGUAGE BangPatterns, GADTs #-}
import Data.Vector.Storable as SV
import Foreign.C.Types (CChar, CInt)
import GHC.Int (Int32)
import Foreign.Marshal.Alloc
import Foreign.Ptr (Ptr)
mallocInt :: IO (Ptr CInt)
mallocInt = malloc
mallocChar :: IO (Ptr CChar)
mallocChar = malloc
main = do
a <- mallocInt
b <- mallocChar …Run Code Online (Sandbox Code Playgroud) 假设我有一个列表和一个向量,我想将它们压缩在一起.一个简单的解决方案是将矢量转换为列表,并将两个列表压缩在一起.但是,这需要对向量进行两次遍历(以及内存分配以将其转换为列表) - 一个用于将向量转换为列表,另一个用于将其与另一个列表一起压缩.
有没有办法在一次遍历中将两者拼接在一起?我想这将需要某种状态保持拉链(我猜它将保持矢量索引的状态,因为它可以在O(1)时间索引).
伪代码:
let l1 = [1..10] :: [CInt]
let v1 = Data.Vector.Storable.fromList l1
map (\(x,y) -> x + y) (zipListVector l1 v1) -- zipListVector function is what I am after
Run Code Online (Sandbox Code Playgroud) 我正在通过本教程.如教程中所述,我复制了一些代码,用于表示functor composition和identity functor:
{-# LANGUAGE FlexibleContexts #-}
module Test where
newtype FComp f g a = C { unC :: f (g a) }
instance (Show (f (g a))) => Show (FComp f g a) where
show (C x) = "FComp " ++ show x
instance (Functor f, Functor g) => Functor (FComp f g) where
fmap h (C x) = C (fmap (fmap h) x)
newtype Id a = Identity { unId :: …Run Code Online (Sandbox Code Playgroud) 如果已经提出这个问题我很抱歉.Show是一个非常常见的关键字,使我很难切断噪音.如果我有一个functor composition如下所示定义的类型,我无法弄清楚如何Show为该类型定义实例:
newtype FComp f g a = C { unC :: f (g a) }
--- Incomplete Show definition for FComp
instance Show (FComp f g a) where
show (C x) = "FComp" ++ show ??? --- Given a type say FComp Maybe Maybe Int, should print out "FComp Maybe Maybe Int"
Run Code Online (Sandbox Code Playgroud)
也:
$ :t show
show :: Show a => a -> String
Run Code Online (Sandbox Code Playgroud)
所以,似乎show接受一个值,并返回相应的字符串.堵x在show ???将无法工作,因为Show实例仍然需要对类型进行定义f …
在尝试构建ios客户端时react-native,我修改了部分AppDelegate.m文件,如下所示(即,用选项2替换选项1):
// OPTION 1
// Load from development server. Start the server from the repository root:
//
// $ npm start
//
// To run on device, change `localhost` to the IP address of your computer, and make sure your computer and
// iOS device are on the same Wi-Fi network.
//jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle"];
// OPTION 2
// Load from pre-bundled file on disk. To re-generate the static bundle, run
//
// $ curl http://localhost:8081/index.ios.bundle …Run Code Online (Sandbox Code Playgroud) 这是我之前提出的问题的后续行动.如果方式更新列表中,我想IORef在下面接受的解决方案是O(1)或不是,在每一个电话fetch.我怀疑这是因为IORef可能只是保持指向列表头部的指针(而不是遍历和复制整个列表,每次都是O(n).只需将指针更改为新头应为O(1),并且应该防止急切评估整个列表).但是,ghc-core不会显示低级代码.所以,问这里:
mklstream :: L.ByteString -> (IO S.ByteString -> IO r) -> IO r
mklstream lbs sink = do
ref <- newIORef (L.toChunks lbs)
let fetch :: IO S.ByteString
fetch = do chunks <- readIORef ref
case chunks of
[] -> return S.empty
(c:cs) -> do writeIORef ref cs
return c
sink fetch
Run Code Online (Sandbox Code Playgroud) float2Int似乎介于溢出2^^62,并2^^63为64位机器(我用GHC 7.6.1试图在英特尔的iMac).试图检查时,我才注意到这个问题maxBound了Int.在GHC中float2Int作为一个原始实现float2Int#.
GHCI提示输出低于- maxBound::Int是2^^63我的Intel Mac上.我也试过铸造2^^63到Float,然后还原值一点,看是否溢出消失(以考虑小舍入误差如果有的话).它没有:
?: maxBound :: Int
9223372036854775807
?: GHC.Float.float2Int $ 2^^63 -- overflows
-9223372036854775808
?: GHC.Float.float2Int $ (9223372036854775807::Float) -- now try actual value of 2^^63
-9223372036854775808
?: GHC.Float.float2Int $ (9223372036854000000::Float) -- reduce it a bit
-9223372036854775808
?: minBound :: Int -- overflow value is same as minBound::Int
-9223372036854775808
?: GHC.Float.float2Int $ 2^^62 + 2^^61 -- works fine …Run Code Online (Sandbox Code Playgroud)