# `ExUtils.Lists`
[🔗](https://github.com/sheharyarn/ex_utils/blob/main/lib/ex_utils/lists.ex#L1)

Elixir Utility methods for Lists.

# `compact`

```elixir
@spec compact(list()) :: list()
```

Removes nil values from a list.

Imitates Ruby's .compact:
https://ruby-doc.org/core-1.9.3/Array.html#method-i-compact

## Examples

```elixir
iex> compact([1, nil, nil, 2, nil, 3, 4])
[1, 2, 3, 4]
```

# `replace`

```elixir
@spec replace([map()], map(), Keyword.t()) :: [map()]
```

Replaces a value in a list of maps/structs, given a `:key`
(default `:id`).

The `key` should ideally be unique, because this function
will replace all matches on key with the new `item`.

## Options

- `:key` - The key with which to match value in the list. Defaults to `:id`.

## Examples

```elixir
iex> list = [%{id: 1, name: "Ada"}, %{id: 2, name: "Grace"}, %{id: 3, name: "Linus"}]
iex> replace(list, %{id: 2, name: "Grace Hopper"})
[%{id: 1, name: "Ada"}, %{id: 2, name: "Grace Hopper"}, %{id: 3, name: "Linus"}]

iex> list = [%{email: "ada@example.com", active: false}]
iex> replace(list, %{email: "ada@example.com", active: true}, key: :email)
[%{email: "ada@example.com", active: true}]
```

---

*Consult [api-reference.md](api-reference.md) for complete listing*
