Hi.
I was solving some exercises on Programming Elixir, and would really appreciate if someone(s) can Elixir-ify my code.
The “my_abs” and “my_reverse” functions act like their built-in namesakes (verified).
I took this approach to maintain compatibility with the built-in namesake function, specifically, using a negative index value.
Thanks a bunch in advance.
def split([], _val), do: []
def split(lst, 0), do: {[], lst}
def split(lst, val) when is_integer(val) do
case (val > 0) do
true -> case (val <= length(lst)) do
true -> {take(lst, val), take(lst, -(length(lst) - val))}
false -> {take(lst, length(lst)), []}
end
false -> case (my_abs(val) <= length(lst)) do
true -> {take(lst, (length(lst) - my_abs(val))), my_reverse(take(my_reverse(lst), my_abs(val)))}
false -> {[], take(lst, length(lst))}
end
end
end
def take([], _val), do: []
def take([_head | _tail], 0), do: []
def take([head | tail], val) when is_integer(val) do
case (val > 0) do
true -> [head | take(tail, val - 1)]
false -> my_reverse(take(my_reverse([head | tail]), my_abs(val)))
end
end
P.S. Also using this message to suggest adding a “Refactor my code” category and “refactor”/“refactoring” tag…