Map over Genarray

In an attemp to write a map function over Bigarray.Genarray with the following signature :

val map : ('a -> 'b) -> ('a,'c,'d) Genarray.t -> ('b, 'c, 'd) Genarray.t 

I ended up with the following implementation :

let map f a = 
  let dims = Genarray.dims a in 
  let map_a2b i = f (Genarray.get a i)  in 
  Genarray.init (Genarray.kind a) (Genarray.layout a) dims map_a2b 
(*               ^^^^^^^^^^^^^^^
                      wrong
*)

which is not correct since Genarray.kind a will give the kind of elements of the array a whereas I am here looking for the kind of the image of array a’s elements by the map f.

How can I do that, if only it is possible ? Am I forced to pass it as an argument to the map function ?

Yes, I think so. It is not possible to create a kind out of f alone.

By the way, this signature does not seem as flexible as it oculd be (it only allows modifying the first parameter of Genarray.t, which is the OCaml type used to access the array). I would have imagined:

val map : ('a -> 'b) -> ('b, 'x) kind -> ('a, 'c, 'd) Genarray.t -> ('b, 'x, 'd) Genarray.t

(and note how nicely one is forced to include the kind argument in this more general version).

Cheers,
Nicolas

1 Like