 
   """
   @spec insert_at(list, integer, any) :: list
-  def insert_at(list, index, value)
-      when is_integer(index) and is_list(list) do
-    cond do
-      index == -1 ->
+  def insert_at(list, index, value) when is_list(list) and is_integer(index) do
+    case index do
+      -1 ->
         list ++ [value]
 
-      index == 0 ->
-        [value | list]
-
-      index > 0 ->
+      _ when index < 0 ->
+        case length(list) + index + 1 do
+          index when index < 0 -> [value | list]
+          index -> do_insert_at(list, index, value)
+        end
+
+      _ ->
         do_insert_at(list, index, value)
-
-      index < 0 ->
-        do_insert_at(list, length(list) + index + 1, value)
     end
   end
 

    [fun.(value) | list]
  end

  defp do_update_at(list, index, _fun) when index < 0 do
    list
  end

  defp do_update_at([head | tail], index, fun) do
    [head | do_update_at(tail, index - 1, fun)]
  end

    []
  end

  defp do_replace_at([_old | rest], 0, value) do
    [value | rest]
  end
