From: Jules B. <ju...@je...> - 2008-03-14 11:00:59
|
shelarcy wrote: > Hi, > > On Fri, 14 Mar 2008 19:24:12 +0900, Eric Y. Kow <eri...@gm...> wrote: >> Here's my implementation of it. Does it look sane to you? Any comments >> from wxhaskell developers? >> >> withImageData :: Image a -> (Ptr () -> IO b) -> IO b >> withImageData image f = do >> pixels <- imageGetData image >> x <- f pixels >> x `seq` image `seq` return x >> >> The seqs are there out of pure superstition (i.e. Eric doesn't know how >> Haskell works); basically, I want to make sure we're holding on to image >> as long as we need to, so at least until we know what 'x' is. > > "a seq b" doesn't guarantee that evaluate a before b. True, but that is not relevant here. It does guarantee that both get evaluated. In fact, all we need to do here is keep image alive. Evaluating it is not the point. > If we want to do that, we must use Control.Parallel's pseq. > > http://www.haskell.org/ghc/docs/6.8.2/html/libraries/parallel/Control-Parallel.html#v%3Apseq > > Or to use $! operator in monad, like this. > > return $! x > return $! image > return x that just uses seq. return $! x is just x `seq` return x that would add nothign to this code. Jules |