title: How do you pattern match for nil or a blank string in Elixir?
body: <p>Here is a code sample with tests where I have a function that checks if the value passed in is in an allowable set of values and if it isn't, it raises an exception, but if the value passed in is nil or a blank string, it just returns false. The idea here is that any sort of blank value means is there is no value. If there is a non-blank value that doesn't match one of the valid values, then we do want to raise an exception because that value is invalid. Also imagine this value is coming from a command line argument or Phoenix request param where we can't control the input value be just nil, we have to check for all permutations of an empty value, nil, an empty string or a string containing just spaces.</p>
  
  <pre><code>ExUnit.start()
  
  defmodule ValidColor do
    @valid_colors MapSet.new(~w[red green blue])
  
    def valid?(color) do
      cond do
        blank?(color) -&gt; false
        MapSet.member?(@valid_colors, color) -&gt; true
        true -&gt; raise "invalid color"
      end
    end
  
    defp blank?(value) do
      case value do
        nil -&gt; true
        v when is_binary(v) -&gt; v =~ ~r/\A\s*\z/
        _ -&gt; false
      end
    end
  end
  
  defmodule ValidColorTest do
    use ExUnit.Case, async: true
  
    test "nil" do
      refute ValidColor.valid?(nil)
    end
  
    test "empty string" do
      refute ValidColor.valid?("")
    end
  
    test "string with only blank characters" do
      refute ValidColor.valid?("     ")
    end
  
    test "invalid color" do
      assert_raise RuntimeError, "invalid color", fn -&gt;
        ValidColor.valid?("yellow")
      end
    end
  
    test "valid color" do
      assert ValidColor.valid?("red")
    end
  end
  </code></pre>
  
  <p>This seems to be a case when a <code>blank?</code> helper function is useful, as I have done in this example. There seems to be <a href="https://www.reddit.com/r/elixir/comments/5d1cgb/coming_from_ruby_whats_the_equivalent_of_blank/" rel="noreferrer">a sentiment among Elixir programmers that this type of helper method is unnecessary and can be done with pattern matching</a>.</p>
  
  <p>How can you implement a function like this with just pattern matching and not the <code>blank?</code> helper function?</p>