Hi all, I’m currently learning Elixir and reading “Programming Elixir 1.3”.
I just completed one of the exercises of the book about implementing some Enum functions without library functions and comprehensions in this scenario is the “split” functions. But I wanted some input and feedback about what I ended doing. Any help or feedback is greatly appreciated.
Thanks
defmodule Functions do
def split(list, count), do: split(list, count, {[],[]})
defp split(list, count, res) when count == 0 or length(list) == 0 do
res
end
defp split(list, count, res) when count > 0 do
[h|t] = list
res = {elem(res, 0) ++ [h], t}
split(t, count - 1, res)
end
defp split(list, count, res) when count < 0 do
cond do
length(list) + count > 0 ->
split(list, length(list) + count, res)
length(list) + count == 0 ->
res = {[], list}
split(list, 0, res)
end
end