qba
qba

Reputation: 1311

Iterating over arrays in haskell

My problem is that I need to iterate over array and calculate some value depend on every element. I was looking for some fold-like function for arrays, but standard library seems to be very useless with arrays. Or i'm missing something?

The other solution may be 'binding' array to a list. Binding mean that I don't want to copy that array. Is this possible?

Btw, its all about plain Array.

Upvotes: 6

Views: 6753

Answers (3)

Edward Kmett
Edward Kmett

Reputation: 29962

Using Data.Foldable you can foldr/foldl an Array just like you can a list.

Another option is that you can convert the Array back into a list with elems, and then foldr or foldl over the list.

Upvotes: 2

Don Stewart
Don Stewart

Reputation: 137957

What kind of array type are you using? You may be able to just foldM over the index space.

Or use one of the array libraries that directly support folds (uvector).

Upvotes: 1

Tirpen
Tirpen

Reputation: 3008

Take a look at Data.Foldable. It defines a type class that does exactly what you want.

Upvotes: 8

Related Questions