Last active
September 20, 2022 02:03
-
-
Save uxjp/e8e73a121f2d6563718db58eb3332c3a to your computer and use it in GitHub Desktop.
How to read from STDIN in Elixir (for HackerRank) by uxjp
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 Solution do | |
input_as_int = elem(Integer.parse(IO.gets(nil)), 0) | |
1..input_as_int |> Enum.each(fn _ -> IO.puts "Hello World" end) | |
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 Solution do | |
IO.gets(nil) | |
|> String.trim | |
|> String.to_integer | |
|> (fn(x) -> Range.new(1, x) end).() | |
|> Enum.each(fn _ -> IO.puts("Hello World") end) | |
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 Solution do | |
IO.gets(nil) | |
|> String.trim | |
|> String.to_integer | |
|> (&(Range.new(1, &1))).() | |
|> Enum.each(fn _ -> IO.puts("Hello World") end) | |
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 Solution do | |
inn = IO.gets(nil) | |
fufu = inn | |
|> String.trim | |
|> String.to_integer | |
|> (&(Range.new(1, &1))).() | |
|> Enum.each(fn _ -> IO.puts("Hello World") end) | |
fufu.() | |
end | |
# this don't work in hacker rank as a valid answer but is a good exemple of closure. | |
# Now the most interesting use I can imagine for a closure is | |
# as a value returned by a function, where the returned closure captures a | |
# state, hiding how some information was obtained. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment