Elixir Streams

From Elixir Wiki
Jump to navigation Jump to search

Elixir Streams[edit]

File:Elixir Logo.png
The Elixir programming language logo

Elixir Streams provides a way to work with sequences of data in a functional and efficient manner in the Elixir programming language. Streams are lazy enumerables which allow you to work with large collections of data without immediately loading them into memory.

Introduction[edit]

Elixir Streams allow you to process large datasets or infinite sequences of data by evaluating each element on demand, rather than loading the entire collection into memory at once. This approach is memory-efficient and can significantly improve the performance of your code when dealing with large data sets.

Benefits[edit]

  • Memory efficiency: Streams allow you to work with large datasets without the need to load the entire collection into memory.
  • Lazy evaluation: Elements in a stream are evaluated on demand, reducing unnecessary computations.
  • Composition: Streams can be composited together, allowing you to build complex pipelines for data processing.
  • Infinite sequences: Streams can represent infinite sequences, enabling you to work with potentially limitless data.

Examples[edit]

Basic Stream[edit]

```elixir stream = Stream.iterate(1, &(&1 + 1))

Enum.take(stream, 5) ```

File Processing[edit]

```elixir File.stream!("data.txt") |> Stream.map(fn line -> String.trim(line) end) |> Stream.map(&String.split(&1, ",")) |> Stream.map(fn [name, age, city] -> %{name: name, age: age, city: city} end) |> Stream.filter(fn person -> person.age >= 18 end) |> Enum.to_list() ```

Infinite Prime Numbers[edit]

```elixir primes = Stream.filter(fn n ->

 for p <- Stream.take(primes, n - 1), rem(n, p) == 0, do: false
 true

end, 2..)

Enum.take(primes, 10) ```

Conclusion[edit]

Elixir Streams are a powerful tool for working with large datasets or infinite sequences in a memory-efficient and performant manner. By leveraging lazy evaluation, you can build complex data processing pipelines while avoiding unnecessary computations. Using streams can lead to more concise and efficient code in Elixir.