How to get started and writing an actual program instead of iex...

I’m having a difficult time figuring out how to get started writing a program that does anything.
All the guides and tutorials that I have found either give examples using iex or partial code.

I know I’m being extremely ambitious here, but one day I would like to make two programs, one that sits idle and listens for a message with two numbers, a and b, then calculates a+1 and b+2 and returns the result of this operation to the console with IO.puts.

The other program would somehow get the PID of the first program to periodically send pairs of numbers to be processed.

I have no idea where to begin.

How am I supposed to learn how to start a listener and get its PID? This listener has to be a real program that runs independently, not a sandbox doodle in iex…

How am I supposed to learn how to take a and b and calculate them both? That’s two separate operations from one input. And I need to be able to access the result to include them in the IO.puts string with something like <> #{a}.

How am I supposed to learn how to make another real program, not iex, speak to this program?

I hope I don’t sound too demanding. I have read a lot already on how things work on elixir-lang.org and the hexdocs, but I can hardly find any glue that helps stick anything together.

For some context of my programming skill level, I only have a few months of experience with a scripting language that is distantly similar to C#.

What I have right now is this:
defmodule Listener do
{:ok, pid} = Task.Supervisor.start_link()
task =
Task.Supervisor.async(pid, fn →
IO.puts("My pid is ")
end)
Task.await(task)
end

which is almost completely stolen from Task — Elixir v1.11.3
I haven’t found anything yet that explains the {:ok convention, and if I try to add ("My pid is "<> “#{pid}”) the compiler doesn’t like it because apparently the ) isn’t there.

I hope someone can show me how to fish…

1 Like

Open your text editor, throw in the exact code you have, save it as any_name.exs (note the .exs) and run: elixir /path/to/the/file/any_name.exs).

There, you went from iex to your “first program” (technically your first script I guess, but mind that later).

I’m being super simplistic here but the message is that at the end of the day, elixir programs are just a bunch of files. Basically everything you can do in your iex session as you’ve been doing, you can throw in a file and you’re good to go.

Think about iex as just a way to mess around in a sandbox and then “persist” your findings in modules/functions within files.
You can also go the other way around. Write some code, open an iex session and test what you wrote.

It seems that you’re trying to take in too much on a single sip.

Also, normally one would initiate a project with mix new project_name and maybe some flags to get a decent amount of boilerplate done.
When you do this and start an iex session, all the code you wrote on the project files, will be available so you can see how things work.

Edit: Maybe take a look at this thread. The videos are really interesting and it might help you with your application idea.

You can write a script, as mentionned in the previous post.

Or You can build a mix project.

$ mix new merav --sup
* creating README.md
* creating .formatter.exs
* creating .gitignore
* creating mix.exs
* creating lib
* creating lib/merav.ex
* creating lib/merav/application.ex
* creating test
* creating test/test_helper.exs
* creating test/merav_test.exs

Your Mix project was created successfully.
You can use "mix" to compile it, test it, and more:

    cd merav
    mix test

Run "mix help" for more commands.
$ cd merav/

This is a fully operational application, with a supervisor

Once You build the project, add this calc.ex file in lib/merav/

defmodule Merav.Calc do
  use GenServer

  def start_link(_) do
    GenServer.start_link(__MODULE__, nil, name: __MODULE__)
  end

  def calc(a, b) do
    GenServer.call(__MODULE__, {:add, a, b})
  end

  def init(_) do
    {:ok, nil}
  end

  def handle_call({:add, a, b}, _from, state) do
    IO.puts("a + 1 = #{a + 1}; b + 2 = #{b + 2}")
    {:reply, {a + 1, b + 2}, state}
  end
end

You don’t need the pid if You give a name to the server, as the name params does, You can use the name directly. Now update the application file in lib/merav/application.ex to start the new server.

    children = [
      Merav.Calc
    ]

Now, You can start the project, and keep control of the console with…

$ iex -S mix
Erlang/OTP 23 [erts-11.1.7] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe]

Compiling 1 file (.ex)
Interactive Elixir (1.11.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> Merav.Calc.calc 3, 4
a + 1 = 4 b + 2 = 6
{4, 6}
iex(2)>

Voilà :slight_smile:

Thank you both for giving me answers, and thank you very much Kokolegorille for writing an example. This gives me some proper syntax and a few more things to study and search for online.

I was under the impression that iex had a different syntax in some cases, but it seems I misunderstood. I will see these iex examples differently.

I regret the title that I chose for this question because it has been taken too literally. The real question is where should I go to learn how to print the pid of a process, or the proper syntax to print strings that contain text, -and- integers, -and- a reference to the pid without getting an error when trying to compile.

These are extremely basic questions that I would expect to find the answer for with a quick search but I can’t find them and this has been frustrating.
If I can get these basic blocks (learning -how- to find this information) I should be able to build up and get started on my journey.

You could simplify string interpolation…

iex(1)> IO.puts "my pid is #{inspect self()}"
my pid is #PID<0.111.0>
:ok

There is no need to join with <>, You can expand in the string. But You need to use inspect to print the pid string.

Also here is a whole free course how to write a tetris game with LiveView: Tetris LiveView - YouTube

It goes through the whole thing how to structure the project and much more.

1 Like

Thank you Egze, this Tetris video series seems to be one of the things I have been very much trying to find!

And Thank you again Kokolegorille, with this method I can format the string the way I was hoping to.

Hopefully with the help I received here and those videos I will be able to make more sense from the documentation I have been going through.

1 Like