Closed obcan closed 8 years ago
Yes, exactly what you've written should work. What's wrong?
This pseudocode produce type mismatch Error. Function passed to functor ~> must be type of Signal CoordinatePair -> Signal CoordinatePair but doubleCoordinates have type CoordinatePair on the right side. Maybe I missed something and only one way is use to Foldp.
Your code fails because of the type of mousePos:
mousePos :: forall e. Eff (dom :: DOM | e) (Signal CoordinatePair)
As you can see the Signal is "wrapped" inside an Eff
which also has a functor instance. The ~>
or map now tries to apply doubleCoordinates
to a Signal CoordinatePair
instead of a CoordinatePair
.
We can solve this by first extracting the mouse Signal from mousePosition and then mapping.
import Prelude hiding (div)
import Signal
import Signal.DOM
doubleCoordinates c = c {x = c.x * 2, y = c.y * 2}
-- | produce new signal with doubled coordinates from origin
main = do
mouse <- mousePos
pure (mouse ~> doubleCoordinates)
Great work! Is possible map over produced values within signal like in RxJS functors? I mean something like this: