Haskell循环一个向量(Haskell cycle a vector)

编程入门 行业动态 更新时间:2024-10-23 04:44:03
Haskell循环一个向量(Haskell cycle a vector)

Data.List的循环是否有矢量替代?

Data.Vector文档有一个标题为回收支持的部分听起来像我想要的但是new功能和clone显然没有做什么cycle 。

我正在考虑的解决方案是重复索引向量(计数器mod为向量的长度)。

我需要做的一个例子是,我需要一个随机洗牌网格的无限列表,但网格很大,因此将数百万个网格保存在内存中并不理想。 我意识到,随机循环是否相对较小并不重要,因此我只生成了一个只有几百个网格的列表,然后使用Data.List.cycle来给出无限长度的错觉。 我现在将网格list转换为网格Vector ,不能再使用cycle 。

Is there a vector alternative of Data.List's cycle?

The Data.Vector docs have a section titled Recycling support that sounds like it is what I would want but the functions new and clone clearly don't do what cycle does.

The solution I was thinking about is just repeatedly index the vector with (a counter mod the length of the vector).

An example of what I needed to do, is I need an infinite list of randomly shuffled grids, but the grids are large and so keeping millions of them in memory is not ideal. I realized that it did not matter if the cycle of random was relatively small, so instead I generated a list of only a couple hundred grids and then used Data.List.cycle to give the illusion of infinite length. I am now converted the list of grids to a Vector of grids and can no longer use cycle.

最满意答案

不Vector.cycle包中现有的Vector至少分配了脊椎,因此你不能拥有没有无限内存的Vector.cycle 。

例如, Data.Vector使用Data.Primitive.Array 。

由于矢量包的内部(例如Data.Vector.Generic.* )允许您以灵活的方式定义其他类型的矢量,因此您可以创建一个矢量类型来计算索引的长度,以提供循环行为(并包括与drop一起使用的偏移量)。 所以施工是可以想象的,只是没有完成。

No. The existing Vector's in the Vector package allocate at least the spine and thus you can not have Vector.cycle without infinite memory.

For example, Data.Vector uses Data.Primitive.Array.

Since the internals of the vector package (e.g. Data.Vector.Generic.*) allow you to define other types of Vectors in a flexible manner, you could make a vector type that computes the index modulo the length to provide cyclic behavior (and includes an offset for use with drop). So the construction is conceivable, just not done.

更多推荐

本文发布于:2023-07-14 20:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1107279.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:向量   Haskell   vector   cycle

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!