如何使制定者在二维数组哈斯克尔 [英] How to make setters in 2D Array Haskell

查看:111
本文介绍了如何使制定者在二维数组哈斯克尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前已经声明为一个二维数组:

I currently have a 2D array declared as:

import Data.Array.Unboxed
listArray ((0,0), (9,9)) (replicate 100 'f') ∷  UArray (Int, Int) Char

我想从一组坐标设置该数组中的值,(X,Y)存储为一个元组,更改值到 T ,而不是˚F。我曾经愚弄与周围的镜头,不过,我曾与他们没有成功。

I am trying to set a value in this array from a set of coordinates, (x, y) stored as a tuple, changing the value into a t, instead of an f. I have fooled around with lenses, however, I have had no success with them.

任何帮助AP preciated!

Any help is appreciated!

推荐答案

要访问数组中的各个元素与镜头,您需要使用 IX Ixed 类的方法,的 Control.Lens.At 。如果定义

To access individual elements of an array with lens, you need to use the ix method of the Ixed class, defined in Control.Lens.At. If you define

fooArray :: UArray (Int, Int) Char
fooArray = listArray ((0,0), (9,9)) (replicate 100 'f')

然后,发射了ghci中,

then, firing up GHCi,

> fooArray ^? ix (1,2)
Just 'f'
> let fooArray' = fooArray & ix (1,2) .~ 't'
> fooArray' ^? ix (1,2)
Just 't'

注意,编辑阵列像这样的pretty效率低下,如果阵列很大;如果你需要高速或大量阵列的编辑,你可能要考虑不同的结构。

Note that editing arrays like this is pretty inefficient if the arrays are large; if you need high speed or lots of array edits, you may want to consider a different structure.

粉碎数组平坦的最简单方法是从 Data.Array.Unboxed 或<$的 elems 功能C $ C>了ToList 从 Data.Foldable 。如果这些东西给你的是侧身,你也许可以用 ixmap 修补它。

The easiest ways to smash an array flat are the elems function from Data.Array.Unboxed or toList from Data.Foldable. If what those give you is sideways, you can probably patch it up with ixmap.

这篇关于如何使制定者在二维数组哈斯克尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆