为什么拆箱的数组不是可折叠的实例? [英] Why are unboxed arrays not an instance of foldable?
问题描述
UArray
的2D网格应用程序将是适当的。但是,据我所知, UArray
不是 foldable
的实例(不在 Data.Array.IArray
和 Data.Array.Unboxed
)。 这是为什么?我可以创建自己的帮助函数,但缺少 foldable
实例表明我可能不应该这样做。
我相信这样的一个实例是不可能的,因为它需要数组中包含的数据类型的额外约束,这不能用Foldable表示。在单向遍历中,我确实定义了MonoFoldable实例以用于未装箱和可存储的向量。
编辑:要清楚,我指的是约束为 Vector
包含的值是 Unbox
,而 Foldable
要求 foldMap
, foldr
等是为所有可能的类型定义的(就像列表,盒装向量等类型一样)。 Foldable
typeclass没有办法陈述包含的值必须满足这些约束条件。有 MonoFoldable
,就有。
Figuring out the correct data container to use can be a bit tricky in Haskell, for my 2D grid application I thought using UArray
would be appropriate. However, as far as I can tell UArray
is not an instance of foldable
(not in Data.Array.IArray
nor Data.Array.Unboxed
).
Is there a reason for this? I can make my own helper functions but the absence of a foldable
instance suggests that maybe I shouldn't.
I believe such an instance is impossible, because it requires an extra constraint in the data type contained by the array, which cannot be expressed in Foldable. In mono-traversable, I do define MonoFoldable instances for unboxed and storable vectors.
EDIT: To be clear, the constraint I'm referring to is that all functions in Data.Vector.Unbox
only work if the value contained by the Vector
is an instance of Unbox
, whereas Foldable
requires that foldMap
, foldr
, etc, are defined for all possible types (as is the case for types like lists, boxed vectors, etc). There is no way with the Foldable
typeclass to state "the contained value must meet these constraints." With MonoFoldable
, there is.
这篇关于为什么拆箱的数组不是可折叠的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!