How can I avoid using memory / CPU in Helm / Elerea (Haskell FRP)?

I dip my fingers in FRP and I put together a basic hello world style application with Helm (as an example here ):

import FRP.Helm
import qualified FRP.Helm.Window as Window
import qualified FRP.Helm.Keyboard as Keyboard
import FRP.Elerea.Simple

data SpriteState = SpriteState {mx::Double, my::Double}

inputSignal::SignalGen (Signal (Double, Double))
inputSignal = lift toFrac Keyboard.arrows
  where toFrac (dx, dy) = (realToFrac dx, realToFrac dy)

spriteSignal::SignalGen (Signal SpriteState)
spriteSignal = foldp newState initialState inputSignal
  where
    initialState = SpriteState {mx = 0, my = 200}
    newState (dx, dy) state = state {mx = x', my = y'}
      where x' = mx state + dx
            y' = my state + dy

spriteForm::SpriteState -> Form
spriteForm state = move (mx state, my state) img
  where img = filled blue $ square 64

render::SpriteState -> (Int, Int) -> Element
render spriteState (w, h) = centeredCollage w h $ [spriteForm spriteState]

main = do
  engine <- startup defaultConfig
  run engine $ render <~ spriteSignal ~~ (Window.dimensions engine)

The program works, but the processor and memory usage rise quickly, even without user input, until it works.

I suspect that the call to foldp accumulates state and never releases it. Should I use some other method to calculate the current state ( I don't see foldp '), or do I need to be careful somewhere, or something else?

Using ghc 7.8.2 and helm 0.6.0.

+4
source share

Source: https://habr.com/ru/post/1547821/


All Articles