开发者

Haskell Repa --- select function is a bit confusing

I'm a bit开发者_运维技巧 confused with select function in repa package:

select (\i -> True) (\i -> i) 10

gives the result

[0,1,2,3,4,5,6,7,8]

I thought i to be between 0 and 10 or 0 and 9. Why is it between 0 and 8?

repa 2.0.2.1


Looks like it produces an array of length len - 1, which is 9 in your case. Which gives you indices in the [0-8] range. I agree that the documentation could be more clear.

If you look at the source, select is implemented in terms of 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.

Apparently, 'extent of indices' for a given n includes all indices x such that 0 <= x < (n-1):

Prelude Data.Array.Repa> extent $ select (const True) id 10
Z :. 9
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜