# If inside anonymous function

**URL:** <https://forum.elixirforum.com/t/if-inside-anonymous-function/19868>\
**Category:** Questions\
**Tags:** phoenix\
**Created:** [February 2, 2019, 9:54am UTC](https://forum.elixirforum.com/t/if-inside-anonymous-function/19868 "2019-02-02T09:54:02Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nobbz/32/27235_2.png) [@NobbZ](https://forum.elixirforum.com/u/NobbZ)\
**Post date:** [February 2, 2019, 10:04am UTC](https://forum.elixirforum.com/t/if-inside-anonymous-function/19868/2 "2019-02-02T10:04:08Z")

</div>

The `if/2` would work as everywhere else as well:

```ex
fn {k, v} ->
  k = String.to_atom(k)
  v = String.replace(v, ~r/[^\w\s\-]/, "")}

  v = if k in [:id, :ph_id] do
    String.to_integer(v)
  else
    v
  end

  {k, v}
end

```

For a bit more readability I’d also make the chain longer and use pattern matching, like this:

```ex
params
|> Map.take(~w(id ph_id name address))
|> Enum.map(fn {k, v} -> {String.to_atom(k), String.replace(v, ~r/[^\w\s\-]/, "")} end)
|> Enum.map(fn
  {k, v} when k in [:id, :ph_id] ->
    {k, String.to_integer(v)}

  tuple ->
    tuple
end)

```

---

_[View the full topic](https://forum.elixirforum.com/t/if-inside-anonymous-function/19868)._
