How do you conduct multiple trials in Elixir? -
in ruby, can like:
10.times { puts 'hello world' }
the best approach can come in elixir is:
enum.each((0..10), fn(x) -> io.puts 'hello world' end)
if run in program, warning hello_world.exs:1: warning: variable x unused
.
question: there better way in elixir?
context: doing simulation 3,000,000 trials need conducted. not iterating on list. simplified scenario doing 3,000,000 coin tosses , recording number of heads.
to remove warning can use _ instead of x.
enum.each((0..10), fn(_) -> io.puts 'hello world' end)
and simplify using list comprehension.
for _ <- 0..10,do: io.puts "hello"
_
- ignore argument in function or in pattern matching. if can give name after underscore.ex - _base
other cases
if needs use index while running trail specify variable without _. (like require index)
for example if needed squares of index.
for x <- 0..10,do: io.puts x * x.
Comments
Post a Comment