Skip to content

Instantly share code, notes, and snippets.

@uxjp
Last active September 20, 2022 02:03
Show Gist options
  • Save uxjp/e8e73a121f2d6563718db58eb3332c3a to your computer and use it in GitHub Desktop.
Save uxjp/e8e73a121f2d6563718db58eb3332c3a to your computer and use it in GitHub Desktop.
How to read from STDIN in Elixir (for HackerRank) by uxjp
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
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
defmodule Solution do
IO.gets(nil)
|> String.trim
|> String.to_integer
|> (&(Range.new(1, &1))).()
|> Enum.each(fn _ -> IO.puts("Hello World") end)
end
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