Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell map/sortBy/findIndex etc. for Arrays instead of Lists

I can see that it's possible to write functions like map/sortBy/findIndex and some other List-related functions for Arrays instead (at least those indexed by integers.) Is this done anywhere in the standard library, or would I need to roll my own?

I need to use an array in my program for the in-place update, but there are also several locations I'd like to use some of the above list functions on it. Is converting back and forth between the two the best solution?

(The arrays I've been looking at are from Data.Array.IArray. I'm also happy to use any other array library that implements this functionality.)

like image 441
Corey Staten Avatar asked Nov 04 '11 13:11

Corey Staten


2 Answers

I recommend you have a look at the vector and vector-algorithms packages. They contain very efficient implementations of many common operations on Int-indexed arrays, in both mutable and immutable variants.

like image 133
hammar Avatar answered Nov 04 '22 07:11

hammar


fmap (from Control.Monad) is sort of like a generic version of map that works on anything that supports the Functor type class. Array supports that, so you should be able to use fmap instead of map for array.

As hammar says, the vector and vector-algorithms are probably a better way to approach the problem if you need to consider indexed arrays.

like image 40
Jeff Foster Avatar answered Nov 04 '22 09:11

Jeff Foster