owaisqayum

owaisqayum

Difficulty Understanding Program

I am going through Streams in Dave Thomas’s book and here I am having difficulties in understanding the following program.

For, example what is receive in sleep function and why to use it?
What is after in sleep function?

How this whole program is executed?

Why Dave Thomas suddenly jumped to such a complex program just after explaining the basics of Stream.

defmodule Countdown do
  # sleep mode
  def sleep(seconds) do
    receive do
    after
      seconds * 1000 -> nil
    end
  end

  def say(text) do
    spawn(fn -> :os.cmd('say #{text}') end)
  end

  def timer do
    Stream.resource(
      # start of next minute or number of seconds in a minute
      fn ->
        {_h, _m, s} = :erlang.time()
        60 - s - 1
      end,
      # wait for the next second
      fn
        0 ->
          {:halt, 0}

        count ->
          sleep(1)
          {[inspect(count)], count - 1}
      end,

      # nothing to deallocate
      fn _ -> nil end
    )
  end
end

Most Liked

hernytan

hernytan

Streams, Enums and the functional idioms can be hard to memorize. My advice is to not worry too much about memorizing every function, but learn one everytime you solve a new problem. When I was learning F#, I started with map, reduce and filter only. Then, everytime i solve a problem, I would consult the list of functions, and see if any of them applied. Sometimes I’d miss out things I could do, but that’s ok.

Now when I am learning Elixir, I do the same thing. Also, you can always come here and ask for improvements :slight_smile:

sabiwara

sabiwara

Elixir Core Team

I think you will get a better picture of how receive works when reading the chapter on Processes (chap 15), in the meantime you can just consider that this is a way of sleeping.

He could probably as well have used the built-in function :timer.sleep/1, e.g. seconds |> :timer.seconds() |> :timer.sleep().

The use of receive here is not very clear about the intent, but it is basically listening for a message that is never going to come (no call to send anywhere) with a timeout of seconds, after which it returns.

sabiwara

sabiwara

Elixir Core Team

I suppose it depends what you are looking for, it is a bit hard for me to answer. Maybe this list might help?

If I had to recommend myself, I would suggest starting with the official Getting Started guide and check Elixir School as well.
A great way to explore different parts of the language through practice could be Elixir-koans.
And if you are planning to use Elixir for web development, the Phoenix official guide and Programming Phoenix are fantastic.

All those resources are free (except the book) and should help you get started, check what works best for you. Past some point nothing beats practicing on some real projects IMHO (and the official documentation will be a tremendous help once you do). And you can always come back at Programming Elixir later for a deeper dive into the concepts. :wink:

Last Post!

owaisqayum

owaisqayum

Its seems like you understood my exact problem and thats a great advice. Thats very true, when i am solving a problem i have many Enum functions in mind but just get confuse which one to apply and which not. I think it will come with practice and with time.

Where Next?

Popular in Discussions Top

CharlesO
Erlang :list.nth simple, but 1 - based nth(1, [H|_]) -> H; nth(N, [_|T]) when N > 1 -> nth(N - 1, T). Elixir Enum.at … coo...
New
mbenatti
Following https://github.com/tbrand/which_is_the_fastest |> https://raw.githubusercontent.com/tbrand/which_is_the_fastest/master/imgs...
New
pillaiindu
In django there is a cache framework backed by memcached. Rails also puts a lot of emphasis on caching, and even the idea of russian-doll...
New
PragTob
Hey everyone, this has been brewing in my head some time and it came up again while reading Adopting Elixir. GenServers, supervisors et...
New
jer
I’ve been using umbrellas for a while, and generally started off (on greenfield projects at least) by isolating subapps based on clearly ...
New
Fl4m3Ph03n1x
Background A few days ago I was listening to The future of Elixir from Elixir Talks, with Dave Thomas (@pragdave ) and Brian Mitchell. I...
New
fireproofsocks
I’ve been working on an Elixir project that has required a lot of scripting. I usually reach for Elixir because I like it more (and in th...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement