我对 repa 包中的 select 函数有点困惑:
select (\i -> True) (\i -> i) 10
给出结果
[0,1,2,3,4,5,6,7,8]
我以为 i 在 0 到 10 或 0 到 9 之间。为什么它在 0 到 8 之间?
修复 2.0.2.1
最佳答案
看起来它产生了一个长度为 len - 1
的数组,在您的情况下为 9。这为您提供 [0-8] 范围内的索引。我同意文档可以更清楚。
如果你看源码,select
根据 selectChunkedP
实现:
-- | Select indices matching a predicate, in parallel.
-- The array is chunked up, with one chunk being given to each thread.
-- The number of elements in the result array depends on how many threads
-- you're running the program with.
selectChunkedP
:: forall a
. Unbox a
=> (Int -> Bool) -- ^ See if this predicate matches.
-> (Int -> a) -- .. and apply fn to the matching index
-> Int -- Extent of indices to apply to predicate.
显然,给定
n
的“索引范围”包括所有指数 x
使得 0 <= x < (n-1)
:Prelude Data.Array.Repa> extent $ select (const True) id 10
Z :. 9
关于arrays - Haskell Repa --- select 函数有点困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6241452/