Skip to content

Instantly share code, notes, and snippets.

@uxjp
Last active September 25, 2022 02:15
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 uxjp/89ec7ff027ebcf942f3ef09ac149885b to your computer and use it in GitHub Desktop.
Save uxjp/89ec7ff027ebcf942f3ef09ac149885b to your computer and use it in GitHub Desktop.
Revert a List Elixir
defmodule ListUtil do
def invert([head | tail], acc) do
invert(tail, [head | acc])
end
def invert([], acc) do
acc
end
end
defmodule Solution do
IO.read(:all)
|> String.split
|> Enum.map(&String.to_integer/1)
|> (&(ListUtil.invert(&1, []))).()
|> Enum.each(&IO.puts/1)
end
defmodule ListUtil do
def inv(list) do
invert(list, [])
end
def invert([head | tail], acc) do
invert(tail, [head | acc])
end
def invert([], acc) do
acc
end
end
defmodule Solution do
IO.read(:all)
|> String.split
|> Enum.map(&String.to_integer/1)
|> ListUtil.inv
|> Enum.each(&IO.puts/1)
end
@uxjp
Copy link
Author

uxjp commented Sep 25, 2022

Reducing the arity of the function simplifies greatly the function call and minimeze errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment