1

I am trying to traverse a 2D array i created using repa, so far i have the function that gets called on every element, but i dont understand what fundamental concept exists that wont let me execute expressions inside that function, what i have so far is as follows:

 drawTile ::(DIM2 -> Int) -> DIM2 -> Int
 drawTile f (Z :. i :. j) = do
   <this is where i want to do some IO>

 drawScene :: [GLuint] -> Array U DIM2 Int -> GLFW.Window -> IO()
 drawScene texs map win = do
   x <- computeP $(traverse map id drawTile)::IO (Array UDIM2 Int)
   return ()

You can ignore the stuff about textures and OpenGL, this is going to be a game. i get compile errors when i try and use a function with side effects in the drawTile function. how do i execute some sort of expression (print "hello" for example), where I want? is there some other easier way to apply a function to each element in a repa array?

  • 1
    This sort of goes against the point of `repa` (that isn't to say this isn't possible though). `repa` is all about parallelizing computation on arrays of different shapes. `IO`, however, is sequential by nature. Also, `traverse` does not mean the same thing as `Data.Traversable.traverse`... – Alec Oct 28 '16 at 17:03
  • 1
    in my actual code it says Data.Array.Repa.traverse, but thanks, that was a headache for a bit. Let me know if this makes sense, i want parallelization on each frame of GL drawing, so that, even thought each frame is sequential, the drawing of all of the individual components of each frame are parallel. thanks for the quick reply – Vince Moosetaffy Oct 28 '16 at 17:10
  • 1
    If each of the individual components can be drawn independently, you could try to make `drawTile` pure, then fold over the array to combine the pure components, and finally actually draw to screen. – Alec Oct 28 '16 at 17:33
  • are you saying that i can create pure functions beforehand, compose them together, and they wont execute until i call them in main? – Vince Moosetaffy Oct 28 '16 at 17:43

1 Answers1

1

i did what user alec suggested and returned a list of IO then sequenced them all together using the following function:

resequence_ :: [IO ()] -> IO ()
resequence_ = foldr (>>) (return ())

thanks again for the help. for future reference, i was mistakenly passing around IO, and the problem wasnt with repa.