我正在尝试使用elm-0.15打印鼠标光标距窗口中心的距离。例如,将光标放在窗口中心必须打印(0,0)。
我的代码是,
import Graphics.Element exposing (show)
import Mouse
import Signal
import Window
relativeMouse : (Int, Int) -> (Int, Int)
relativeMouse (ox, oy) (x,y) = (x - ox, y - oy)
center: (Int, Int) -> (Int, Int)
center (w,h) = (w/2, h/2)
main = Signal.map show <| relativeMouse (Signal.map center Window.dimensions Mouse.position)
elm-make basics.elm
至少抛出4个type-mismatch errors
如何将多个信号(window.dimensions
,Mouse.position
)传递给elm 0.15中的函数(例如relativeMouse
)?
答案 0 :(得分:3)
评论是你所拥有的变化:
import Graphics.Element exposing (show)
import Mouse
import Signal
import Window
-- change type signature to match implementation
relativeMouse : (Int, Int) -> (Int, Int) -> (Int, Int)
relativeMouse (ox, oy) (x,y) = (x - ox, y - oy)
-- use // for integer division
center: (Int, Int) -> (Int, Int)
center (w, h) = (w // 2, h // 2)
-- 1) map center over Window.dimensions to get a signal of origin positions
-- 2) map2 relativeMouse over the signal of origin positions and
-- Mouse.position to get signal of relative mouse positions
-- 3) map show over the signal of relative mouse positions to display them
main = Signal.map show (Signal.map2 relativeMouse (Signal.map center Window.dimensions) Mouse.position)
要回答您的上一个问题:Signal.map2
是用于将1个函数映射到2个信号的内容。相应的地图最多存在map5
。
信号导入行也可以更改为
import Signal exposing ((<~), (~))
使用较短的信号映射语法,在这种情况下主线变为
main = show <~ (relativeMouse <~ (center <~ Window.dimensions) ~ Mouse.position)