Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Negate A Boolean In A Pipeline?

Tags:

elixir

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?

like image 534
Onorio Catenacci Avatar asked Mar 24 '16 13:03

Onorio Catenacci


1 Answers

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
like image 143
Gazler Avatar answered Oct 13 '22 21:10

Gazler