Roman Rabinovich
Roman Rabinovich

Reputation: 918

Elixir Enum vs Erlang Lists

Is there a reason to use enum library over erlang lists when writing code in Elixir? lists has many of the same functions, like takewhile, partition, any, all...

also elixir-lang.org states "Elixir provides excellent interoperability with Erlang libraries. In fact, Elixir discourages simply wrapping Erlang libraries in favor of directly interfacing with Erlang code."

Is it because the condition function needs to be formated differently that Enum is used over Lists?

Upvotes: 1

Views: 978

Answers (1)

Théophile Choutri
Théophile Choutri

Reputation: 136

So, Enum is a module that holds functions that work with data structures implementing the Enumerable protocol.

Lists in Erlang/Elixir are Linked Lists, which have several properties in terms of algorithmic complexity when it comes to indexing and inserting.

Enumerables in Elixir are data structures such as lists, but also maps and mapsets. However, as stated in the documentation:

the majority of the functions in Enum enumerate the whole enumerable and return a list as result



That being said, two reasons to use Enum functions over List-based functions is that basically:

  1. There are more of them, and will provide you with very useful ways to tackle a problem
  2. They are polymorphic. Change your map for a keyword list any time and don't waste time rewriting everything in your codebase.

Upvotes: 4

Related Questions