bodil / purescript-signal

Elm style FRP library for PureScript
Apache License 2.0
258 stars 44 forks source link

map over values within signals #37

Closed obcan closed 8 years ago

obcan commented 8 years ago

Great work! Is possible map over produced values within signal like in RxJS functors? I mean something like this:

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 = mousePos ~> doubleCoordinates
michaelficarra commented 8 years ago

Yes, exactly what you've written should work. What's wrong?

obcan commented 8 years ago

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.

kritzcreek commented 8 years ago

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)