如何否定管道中的布尔值?

时间:2016-03-24 13:17:21

标签: elixir

请考虑以下代码:

defmodule T do
  def does_not_contain?(s, t) do
    s |> not(String.contains?(t))
  end
end

这会在编译时出现以下错误:

** (CompileError) iex:3: undefined function not/2

我也试过这样的结构:

defmodule T do
  def does_not_contain?(s, t) do
    s |> String.contains?(t) |> not
  end
end

这给了我这个错误:

** (SyntaxError) iex:4: unexpected token: end

我可以做这样的事情:

defmodule T do
  def does_not_contain?(s, t) do
    does_contain = s |> String.contains?(t)
    not(does_contain)
  end
end

但尝试将整个事情保持在管道中是非常有吸引力的。有没有办法否定管道中的布尔值?

1 个答案:

答案 0 :(得分:18)

如果您使用该功能的完全限定版本,则可以在管道中使用它:

iex(1)> true |> Kernel.!
false
iex(2)> true |> Kernel.not
false