Generating Functions from External Data

Turning raw data into code might sound impractical, but it’s an extremely nice solution to a number of problems. Ever wonder how Elixir manages its fantastic String Unicode support? The way it goes about it is my favorite metaprogramming example to date. The String.Unicode module of the standard library dynamically generates thousands of function definitions from external data when compiled. These generated functions pattern match on all known Unicode characters to achieve the best Unicode support in languages today. Let’s look inside the String.Unicode module to understand how Elixir makes this happen.

Instead of manually mapping tens of thousands of Unicode code points into an Elixir data structure, a ...

Get Metaprogramming 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.