Editing
Elixir Enum Module
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Elixir Enum Module == The Elixir Enum Module is an essential component of the Elixir programming language. It provides a set of functions that allow developers to work with enumerable data structures effectively. This module is part of the Elixir standard library, making it readily available for every Elixir project. === Overview === The Enum module offers a wide range of functions that enable operations on enumerables. Enumerables can include lists, maps, ranges, streams, and more. With the help of these functions, developers can iterate, transform, filter, and perform various other operations on enumerables easily and efficiently. === Commonly Used Functions === * [[Elixir Enum.each/2|each/2]] - Iterates over an enumerable and invokes a given function on each element. * [[Elixir Enum.map/2|map/2]] - Transforms each element in an enumerable by applying a given function and returns a new enumerable. * [[Elixir Enum.filter/2|filter/2]] - Filters an enumerable by only keeping the elements that satisfy a given condition. * [[Elixir Enum.reduce/3|reduce/3]] - Reduces an enumerable to a single value by applying a given function on each element. * [[Elixir Enum.sort/1|sort/1]] - Sorts elements in an enumerable in ascending order. * [[Elixir Enum.reverse/1|reverse/1]] - Reverses the order of elements in an enumerable. * [[Elixir Enum.any?/2|any?/2]] - Checks if at least one element in the enumerable satisfies a given condition. * [[Elixir Enum.all?/2|all?/2]] - Checks if all elements in the enumerable satisfy a given condition. === Examples === ===== Example 1: Filtering a List ===== ```elixir fruits = ["apple", "banana", "cherry", "date"] short_fruits = Enum.filter(fruits, fn fruit -> String.length(fruit) < 6 end) ``` ===== Example 2: Mapping over a Range ===== ```elixir numbers = 1..5 squared_numbers = Enum.map(numbers, fn number -> number * number end) ``` ===== Example 3: Reducing a Stream ===== ```elixir stream = Stream.iterate(1, &(&1 + 1)) sum = Enum.reduce(stream, 0, fn number, acc -> number + acc end) ``` === Conclusion === The Elixir Enum module is a powerful tool for working with enumerables in the Elixir programming language. Its wide range of functions simplifies common operations and provides flexibility in handling data. By leveraging this module, developers can write concise and expressive code while effectively manipulating enumerables. For more information about using the Enum module and other vital components of Elixir, refer to the related articles on this wiki.
Summary:
Please note that all contributions to Elixir Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Elixir Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information