How to refactor Enum.reduce?

Hello, I have a problem with properly understanding Enum.reduce but after many tries I managed to get this inside my function to work. However, when I run mix credo --strict I see there’s a refactoring opportunity because the function body is nested too deep. Could you please help me with refactoring this function? I would appreciate an additional explanation. Thank you!

    Enum.reduce(conditions, queryable, fn {k, v}, acc ->
      case {k, v} do
        {_, ""} ->
          acc

        {_, _} when is_atom(k) ->
          case {k in like_fields, k in date_fields} do
            {true, false} -> where_ilike(acc, k, v)
            {false, true} -> where_date(acc, k, v)
            {false, false} -> where(acc, [u], field(u, ^k) == ^v)
          end

        _ ->
          acc
      end
    end)

Note that the branching and pattern matching can also be applied to fn and are equivalent:

    Enum.reduce(conditions, queryable, fn
      {_, ""}, acc ->
        acc

      {k, v}, acc when is_atom(k) ->
        case {k in like_fields, k in date_fields} do
          {true, false} -> where_ilike(acc, k, v)
          {false, true} -> where_date(acc, k, v)
          {false, false} -> where(acc, [u], field(u, ^k) == ^v)
        end

      _, acc ->
        acc
    end)

You can also merge the first and third branches:

    Enum.reduce(conditions, queryable, fn
      {k, v}, acc when is_atom(k) and v != "" ->
        case {k in like_fields, k in date_fields} do
          {true, false} -> where_ilike(acc, k, v)
          {false, true} -> where_date(acc, k, v)
          {false, false} -> where(acc, [u], field(u, ^k) == ^v)
        end

      _, acc ->
        acc
    end)
6 Likes