Elixir script does not complete
Asked Answered
P

1

9

I tried to implement FizzBuzz in elixir. I think it works, but I have some weird behavior and it may be my environment. I can run the following .exs file using elixir fizzbuzz.exs and on the first run it works as you would expect. Even on the second run it would work as you would expect. However, if I wait 7 or 8 seconds and run it again, it will stop at 13, or maybe 14. It might even make it to 50. There really isn't anything too consistent about what I am experiencing. I've reproduced it on the first run, I've reproduced it after waiting two seconds between runs. I've even gone a few minutes without being able to reproduce it at all. Please help me...

IO.puts "Hello world from Elixir"

defmodule Fizzbuzz do
  def run(max) do
    run(1, max)
  end

  def run(n, max) do
    if rem(n, 3) == 0 do
      IO.puts "FIZZ"
    end

    if rem(n, 5) == 0 do
      IO.puts "BUZZ"
    end

    if rem(n, 3) != 0 && rem(n, 5) != 0 do
      IO.puts n
    end

    if n < max do
      run(n + 1, max)
    end
  end
end

Fizzbuzz.run(100)

evidence

#: Elixir -v
Erlang/OTP 21 [erts-10.2] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1]

Elixir 1.9.4 (compiled with Erlang/OTP 20)
Possessed answered 30/1, 2020 at 0:14 Comment(8)
I cannot reproduce the weird behaviour. As you say: must be something specific in your environment.Despotism
I'm hoping to be given some pointers on how to debug my environmentPossessed
Can you reproduce the issue outside VSCode, in a regular terminal?Despotism
yes, I can reproduce it inside or outside of vscode in command prompt, powershell, and git bashPossessed
I have elixir installed via Chocolatey, and I plan on, later today, uninstalling it via chocolatey and installing it as via the web installer.Possessed
Weird. I'd guess the program is producing the output, but the lines are somehow not flushed to the terminal before the program ends. You could try adding a :timer.sleep(1000) after Fizzbuzz.run, to see if something like that is going on.Fenestra
@PawełObrok Looks like you may be on to something. After adding :timer.sleep(1000) to the end of the script I am no longer able to reproduce my problem. I didn't like the pause at the end so I changed the value for sleep/1 to 200 and I am still unable to reproduce the issue.Possessed
wow an odd situation. have you tried using asdf version manager? or IEx.configure(inspect: [limit: :infinity])?Cordellcorder
J
1

First, try updating to the latest release of Elixir, 1.13, currently.

Try running your file with mix run <filename>, so in your case:

mix run fizzbuzz.exs
Jennine answered 5/1, 2022 at 1:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.