How To Negate A Boolean In A Pipeline?

余生长醉 提交于 2019-12-22 04:26:29

问题


Consider the following code:

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

This gives the following error on compilation:

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

I also tried a construct like this:

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

That gives me this error:

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

I can do something like this which works:

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

But it's quite appealing to try to keep the whole thing in the pipeline. Is there any way to negate a boolean within the pipeline?


回答1:


If you use the fully qualified version of the function then you can use it in a pipeline:

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


来源:https://stackoverflow.com/questions/36201156/how-to-negate-a-boolean-in-a-pipeline

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!