Sch*_*der 29
像这样使用Enum.drop/2:
list = [1, 2, 3, 4]
Enum.drop list, -1 # [1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
asi*_*niy 18
我的解决方案(我觉得它不干净,但它有效!)
a = [1, 2, 3, 4]
[head | tail] = Enum.reverse(a)
Enum.reverse(tail) # [1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
如果您想要获取最后一项及其前面的列表的其余部分,您现在可以使用List.pop_at/3
:
{last, rest} = List.pop_at([1, 2, 3], -1)
{3, [1, 2]}
Run Code Online (Sandbox Code Playgroud)
https://hexdocs.pm/elixir/List.html#pop_at/3
归档时间: |
|
查看次数: |
4564 次 |
最近记录: |