elixir - how to get all elements except last in the list?
问题 Let say I have a list [1, 2, 3, 4] How can I get all elements from this list except last? So, I'll have [1, 2, 3] 回答1: Use Enum.drop/2 like this: list = [1, 2, 3, 4] Enum.drop list, -1 # [1, 2, 3] 回答2: My solution (I think it's not a clean, but it works!) a = [1, 2, 3, 4] [head | tail] = Enum.reverse(a) Enum.reverse(tail) # [1, 2, 3] 回答3: Another option besides the list |> Enum.reverse |> tl |> Enum.reverse mentioned before is Erlang's :lists.droplast function which is slower according to the