首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将多个信号传递给Signal.map抛出类型不匹配错误

将多个信号传递给Signal.map抛出类型不匹配错误
EN

Stack Overflow用户
提问于 2015-07-01 08:44:16
回答 1查看 123关注 0票数 0

我正在尝试使用elm-0.15打印鼠标光标与窗口中心的距离。例如,将光标放置在窗口中心必须打印(0,0)。

我的代码是,

代码语言:javascript
复制
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.dimensionsMouse.position)传递给elm 0.15中的一个函数(例如,relativeMouse)?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-01 18:58:41

注释是与您所拥有的内容不同的更改:

代码语言:javascript
复制
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是用来映射两个信号上的一个函数的。对应的映射一直存在到map5

信号导入线也可以更改为

代码语言:javascript
复制
import Signal exposing ((<~), (~))

使用较短的信号映射语法,在这种情况下,主线将更改为

代码语言:javascript
复制
main = show <~ (relativeMouse <~ (center <~ Window.dimensions) ~ Mouse.position)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31156558

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档