如何在管道中取反布尔值? [英] How To Negate A Boolean In A Pipeline?
本文介绍了如何在管道中取反布尔值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑以下代码:
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
这给了我这个错误:
** (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
但是尝试将整个过程保持在管道中非常吸引人。有什么方法可以在管道中取反布尔值?
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?
推荐答案
如果使用函数的完全限定版本,则可以在管道中使用它:
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
这篇关于如何在管道中取反布尔值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文