2016-03-24 22 views
5

考慮下面的代碼:如何取消流水線中的布爾值?

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 

但是它是相當吸引人的要儘量保持整個事情的管道。有沒有什麼辦法來消除管道內的布爾值?

回答

13

如果使用功能的完全合格的版本,那麼你可以在管道中使用它:

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

謝謝!這正是我所期待的! –