How do you do a few tests at Elixir?

In Ruby, you can do something like:

10.times { puts 'hello world' }

The best approach I can come up with in Elixir is this:

Enum.each((0..10), fn(x) -> IO.puts 'hello world' end)

If you run the program, you will get a warning hello_world.exs:1: warning: variable x is unused.

Question: Is there a better way to do this in Elixir?

Context: I am doing a simulation where 3,000,000 trials are needed. I do not go through the list. A simplified scenario will make 3,000,000 coins and record the number of goals.

+4
source share
2 answers

To remove the warning, you can use _ instead of x.

Enum.each((0..10), fn(_) -> IO.puts 'hello world' end)

And you could probably simplify this using list comprehension.

for _ <- 0..10,do: IO.puts "hello"

_ - . , underscore.Ex - _base

- , _. (, )

, - .

for x <- 0..10,do: IO.puts x * x.
+3

, :

defmodule M do
  def func(0), do: nil
  def func(n) do
    IO.puts "hello world"
    func(n-1)
  end
end

M.func(10)

Ruby- :

defmodule RubyHelpers do
  def times(0, _), do: nil
  def times(n, func) do
    func.()
    times(n-1, func)
  end
end

import RubyHelpers
10 |> times(fn -> IO.puts "hello world" end)
+3

Source: https://habr.com/ru/post/1624634/


All Articles