我对repa包中的select函数有些困惑:
select (\i -> True) (\i -> i) 10
提供结果
[0,1,2,3,4,5,6,7,8]
我原本认为它应该在0到10或0到9之间。为什么它只在0到8之间呢?
repa 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
,“指标范围”包括所有满足0 <= x < (n-1)
的指标x
:
Prelude Data.Array.Repa> extent $ select (const True) id 10
Z :. 9