Last active
June 4, 2016 21:51
-
-
Save anthonylebrun/cd842229ecedc59aba7f75589bad7810 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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