Logo

Programming-Idioms

History of Idiom 222 > diff from v34 to v35

Edit summary for version 35 by vilmes21:
[Elixir] The old solution finds the value, but we actually need the index

Version 34

2022-08-19, 11:30:41

Version 35

2022-10-14, 23:54:36

Idiom #222 Find the first index of an element in list

Set i to the first index in list items at which the element x can be found, or -1 if items does not contain x.

Idiom #222 Find the first index of an element in list

Set i to the first index in list items at which the element x can be found, or -1 if items does not contain x.

Variables
i,x,items
Variables
i,x,items
Extra Keywords
position
Extra Keywords
position
Code
Enum.find_value(items, -1, fn val -> if val == x, do: x end)
Code
i = Enum.find_index(items, & &1 == x)
Doc URL
https://hexdocs.pm/elixir/Enum.html#find_value/3-examples
Doc URL
https://hexdocs.pm/elixir/Enum.html#find_index/2