Unpacking Values from Various Data Types

Pattern matching is also useful for extracting parts of values to variables in a process called destructuring. It’s our primary tool to get a string part, an item from a list, and a value from the map. We use destructuring together with pattern matching when we’re making two things match. In this section, we’ll explore pattern matching with several data types and see how we can extract values and make more complex matches.

Matching Parts of a String

Strings are a data type that we can use in pattern matching. We can use the <> operator to check the beginning of a string. It’s useful for checking text that’s organized in key/value pairs. For example, we can match one header pattern in the HTTP protocol. ...

Get Learn Functional Programming with Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.