鉴于信号,人们如何获得其历史价值?
像
这样的东西--current value
Signal.pastValue(0, Mouse.x)
--previous value
Signal.pastValue(1, Mouse.x)
--previous nth value
Signal.pastValue(n, Mouse.x)
我已尝试使用Signal.foldp
,但它似乎会返回取决于事件编号的当前值或累计值。
答案 0 :(得分:3)
Elm doesn't keep track of historical values on its own, but you could use foldp
to build a list of any type of signal like this:
history : Signal a -> Signal (List a)
history =
Signal.foldp (::) []
The most recent signal value is prepended to that list. To see it in action, you could past this full example into http://elm-lang.org/try
import Graphics.Element exposing (show)
import Mouse
main =
Signal.map show <| history Mouse.x
history : Signal a -> Signal (List a)
history =
Signal.foldp (::) []
Running that example may shed light on why historical values aren't kept by default: You can quickly bloat your memory. That being said, elm-reactor
s time-travelling debugger keeps history around, but only for debugging purposes. This isn't something you'd normally want in production.