You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Credo.Check.Refactor.AppendSingleItem correctly flags a bunch of places where singles items are in fact appended to lists and that's all good.
Example from credo list = list_so_far ++ [new_item]
The issue is that the example states to use Enum.reverse/1 when order matters and gives the following adivce:
The thing is that the given solution is somewhat wrong. If order matters and the list_so_far is something like [1, 2, 3] and we try to append 4 the resulting list looks like this[3, 2, 1, 4].
Environment
1.7.5-ref.main.81cc50fba+uncommittedchanges
Erlang/OTP 26 [erts-14.1] [source] [64-bit] [smp:10:10] [ds:10:10:10] [async-threads:1] [jit] Elixir 1.15.6 (compiled with Erlang/OTP 26)
The issue
The
Credo.Check.Refactor.AppendSingleItem
correctly flags a bunch of places where singles items are in fact appended to lists and that's all good.Example from credo
list = list_so_far ++ [new_item]
The issue is that the example states to use
Enum.reverse/1
when order matters and gives the following adivce:The thing is that the given solution is somewhat wrong. If order matters and the
list_so_far
is something like[1, 2, 3]
and we try to append4
the resulting list looks like this[3, 2, 1, 4]
.The correct solutions would be:
But I would argue that ist in fact messier than before.
Additionally
To add to that two possible functions come to mind to get around using
++
there would beEnum.concat(list_so_far, [new_item])
or ...List.insert_at(list_so_far, -1, new_item)
the thing is both are using the
++
themself. See Enum.concat and List.insert_atThe text was updated successfully, but these errors were encountered: