【问题标题】:What is the remainder operator for ElmElm 的余数运算符是什么
【发布时间】:2020-07-28 03:18:51
【问题描述】:

我有这个功能

result = 
  add 1 2 |> \a -> a % 2 == 0)

我收到了这个错误

Elm does not use (%) as the remainder operator

当我查看文档时,我发现我可以使用 modBy,所以我尝试了这个。

result =
   add 1 2 |> (\a -> a modBy 2 == 0)

但这给了我以下错误。

This function cannot handle the argument sent through the (|>) pipe:

【问题讨论】:

标签: elm


【解决方案1】:

0.19 中删除了% 运算符,以减少remmod 之间的混淆。

modByremainderBy 是常规函数。你可以像这样使用它们:

result = add 1 2 |> (\a -> modBy 2 a == 0)

或者,如果您更喜欢代码的功能组合变体:

result = add 1 2 |> modBy 2 >> (==) 0

作为历史记录,曾经有一种使用反引号表示法调用函数中缀的方法:

 a `modBy` 2

但这已在 0.18 中删除

【讨论】:

  • 确保不要将 0 用作 modBy 的第一个参数。这将导致运行时错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-11
  • 2013-07-31
  • 1970-01-01
相关资源
最近更新 更多