Last active
September 25, 2022 02:15
-
-
Save uxjp/89ec7ff027ebcf942f3ef09ac149885b to your computer and use it in GitHub Desktop.
Revert a List Elixir
This file contains 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
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 |
This file contains 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
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 |
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
https://www.hackerrank.com/challenges/fp-reverse-a-list/problem?isFullScreen=false