All posts

Elixir Enum fetch vs find functions

 elixir

In Elixir the Enum modules fetch/2 and find/3 methods sound a little similar at first thought, this post will share what makes them similiar, different, and provide examples.

fetch/2 and find/3 are similarity in that their job is to locate a specific element inside of an enumerable object. However these functions handle this task quite differently. fetch/2 will take in an enumerable and an index value as parameters and return a tuple with the matched element at the index value parameter provided. find on the other hand will accept three arguments, an enumberable, an optional default value, and a callback function and return the first matched element found in the callback function.

Enum.find([2, 3, 4], fn int -> int == 2 end)
=> 2
Enum.fetch([2, 3, 4], 0)
=> {:ok, 2}

I have also written another Enum post on iterating a List with_index in elixir if you’re interested.

Postscript:

I built two products to help fund continued efforts for this website, it would mean a lot if you would check them out:
1. 2500+ Words for the Undermotivated Software Engineer - A short essay I wrote on the titled topic ($3).
2. Build 3 Chrome Extensions in Under 2 Hours - A course I built to teach chrome extension development ($9-20).