O'Reilly logo

Programming Elixir by Dave Thomas

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Ignoring a Value with _ (Underscore)

If we didn’t need to capture a value during the match, we could use the special variable _ (an underscore). This acts like a variable but immediately discards any value given to it—in a pattern match, it is like a wildcard saying, “I’ll accept any value here.” The following example matches any three-element list that has a 1 as its first element.

 
iex>​ [1, _, _] = [1, 2, 3]
 
[1, 2, 3]​​
 
iex>​ [1, _, _] = [1, ​"cat"​, ​"dog"​]
 
[1, ​"cat"​, ​"dog"​]

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required