问题
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