Improve Performance with Streams

Learn how streams reduce the time taken by the open_airports() function.


We know that the Stream data structure is lazily evaluated and allows us to process data only when needed. The Stream module has lazy implementations of map/2, filter/2, and other functions. We can use and Stream.filter/2 to replace their Enum counterparts, but what about reading and parsing the file?


Don’t worry, Elixir has us covered. The File module has a lazy equivalent to read!/1 called stream!/1, which is exactly what we need. The CSV module has parse_stream/1, which can replace parse_string/1. We have everything we need to convert our code from eager to lazy. Here’s the new version:

Get hands-on with 1200+ tech skills courses.