Skip to content

Instantly share code, notes, and snippets.

@anthonylebrun
Last active June 4, 2016 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthonylebrun/cd842229ecedc59aba7f75589bad7810 to your computer and use it in GitHub Desktop.
Save anthonylebrun/cd842229ecedc59aba7f75589bad7810 to your computer and use it in GitHub Desktop.
# Ok, so popping an immutable list is like cutting off the head of a hydra:
# it doesn't really get rid of the head :) See -> Enum.take/2.
defmodule ListUtils do
def pop(list, n, acc \\ [])
def pop([], _n, acc), do: Enum.reverse(acc)
def pop(_list, 0, acc), do: Enum.reverse(acc)
def pop([head | tail], n, acc) do
pop(tail, n - 1, [head | acc])
end
end
[1,2,3,4,5] |> ListUtils.pop(3) |> IO.inspect #=> [1,2,3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment