Skip to content

Instantly share code, notes, and snippets.

@funrep
Created November 24, 2013 16:26
Show Gist options
  • Save funrep/7628991 to your computer and use it in GitHub Desktop.
Save funrep/7628991 to your computer and use it in GitHub Desktop.
-- I'm currently trying to translate this code https://github.com/bernstein/breakout/blob/master/src/ReactiveUtils.hs
-- to use linear package instead of vector-space, and I'm not sure if I've understood diffE completely.
import Linear.Vector
import Reactive.Banana
type Time = Double
integral :: (Additive f, Num a) => Event t Time -> Behavior t (f a)
-> Behavior t (f a)
integral t b = sumB $ (\v dt -> dt *^ v) <$> b <@> diffE t
sumB :: (Additive f, Num a) => Event t (f a) -> Behavior t (f a)
sumB = accumB zero . fmap (^+^)
diffE :: (Additive f, Num a) => Event t (f a) -> Event t (f a)
diffE = withPrevEWith (^-^)
withPrevEWith :: (a -> a -> b) -> Event t a -> Event t b
withPrevEWith f e = filterJust . fst . mapAccum Nothing $ g <$> e
where
g y Nothing = (Nothing , Just y)
g y (Just x) = (Just (f y x), Just y)
-- When I try to compile this I get this type error:
{-
game.hs:37:58:
Couldn't match type `Double' with `f0 Integer'
Expected type: Event t (f0 Integer)
Actual type: Event t Time
In the first argument of `diffE', namely `t'
In the second argument of `(<@>)', namely `diffE t'
In the second argument of `($)', namely
`(\ v dt -> dt *^ v) <$> b <@> diffE t'
-}
-- Where line 37 is the line where linear is defined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment