# Difficulty Understanding Program

**URL:** <https://forum.elixirforum.com/t/difficulty-understanding-program/33730>\
**Category:** Discussions\
**Tags:** learning-resources, book-learning-elixir, learning-elixir, learning\
**Created:** [August 14, 2020, 9:13am UTC](https://forum.elixirforum.com/t/difficulty-understanding-program/33730 "2020-08-14T09:13:03Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![owaisqayum](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/owaisqayum/32/20018_2.png) [@owaisqayum](https://forum.elixirforum.com/u/owaisqayum)\
**Post date:** [August 14, 2020, 9:13am UTC](https://forum.elixirforum.com/t/difficulty-understanding-program/33730/1 "2020-08-14T09:13:03Z")

</div>

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.

```elixir
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

```

---

<div class="post-metadata">

**Author:** ![slouchpie](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/slouchpie/32/34819_2.png) [@slouchpie](https://forum.elixirforum.com/u/slouchpie)\
**Post date:** [August 14, 2020, 9:33am UTC](https://forum.elixirforum.com/t/difficulty-understanding-program/33730/2 "2020-08-14T09:33:16Z")

</div>

I don’t see `require` in the `sleep` function. Did you mean [“receive”](https://elixir-lang.org/getting-started/processes.html#send-and-receive)?

---

<div class="post-metadata">

**Author:** ![owaisqayum](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/owaisqayum/32/20018_2.png) [@owaisqayum](https://forum.elixirforum.com/u/owaisqayum)\
**Post date:** [August 14, 2020, 9:34am UTC](https://forum.elixirforum.com/t/difficulty-understanding-program/33730/3 "2020-08-14T09:34:13Z")

</div>

ohh yes, its a typo. I mean `receive`

---

<div class="post-metadata">

**Author:** ![sabiwara](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sabiwara/32/18612_2.png) [@sabiwara](https://forum.elixirforum.com/u/sabiwara)\
**Post date:** [August 14, 2020, 9:35am UTC](https://forum.elixirforum.com/t/difficulty-understanding-program/33730/4 "2020-08-14T09:35:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![owaisqayum](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/owaisqayum/32/20018_2.png) [@owaisqayum](https://forum.elixirforum.com/u/owaisqayum)\
**Post date:** [August 14, 2020, 9:39am UTC](https://forum.elixirforum.com/t/difficulty-understanding-program/33730/5 "2020-08-14T09:39:09Z")

</div>

Thats pretty good explanation. Also the Stream.resource function is very confusing as one function pass data to another and thats how it traverse. Is it important to learn it at this stage or it’s too early to dive deep into it ?

---

<div class="post-metadata">

**Author:** ![LostKobrakai](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lostkobrakai/32/3072_2.png) [@LostKobrakai](https://forum.elixirforum.com/u/LostKobrakai)\
**Post date:** [August 14, 2020, 9:46am UTC](https://forum.elixirforum.com/t/difficulty-understanding-program/33730/6 "2020-08-14T09:46:53Z")

</div>

> [@sabiwara](#):
>
> but it is basically listening for a message that is never going to come (no call to `send` anywhere)

It works that way not because there’s no `send` – other processes could send messages – but because there’s no “matching clause” for `receive`.

---

<div class="post-metadata">

**Author:** ![sabiwara](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sabiwara/32/18612_2.png) [@sabiwara](https://forum.elixirforum.com/u/sabiwara)\
**Post date:** [August 14, 2020, 12:52pm UTC](https://forum.elixirforum.com/t/difficulty-understanding-program/33730/7 "2020-08-14T12:52:47Z")

</div>

Indeed, thank you for the correction 👍  
I meant to say “it isn’t trying to actually receive anything, and isn’t sending anything either, just relying on the timeout as a way to implement the sleep”.

---

<div class="post-metadata">

**Author:** ![sabiwara](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sabiwara/32/18612_2.png) [@sabiwara](https://forum.elixirforum.com/u/sabiwara)\
**Post date:** [August 14, 2020, 1:17pm UTC](https://forum.elixirforum.com/t/difficulty-understanding-program/33730/8 "2020-08-14T13:17:12Z")

</div>

While it might not be necessary to “dive deep”, it is probably helpful to understand what exists, what is possible, and you can always come back to it once you need it. The [example from the doc](https://hexdocs.pm/elixir/Stream.html?#resource/3) might give you a good idea as well.  
As stated in the intro, this book is “not a top-to-bottom reference guide”, don’t look at it as a checklist of things you “have to” know, but as a guide to explore the language 🙂

---

<div class="post-metadata">

**Author:** ![owaisqayum](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/owaisqayum/32/20018_2.png) [@owaisqayum](https://forum.elixirforum.com/u/owaisqayum)\
**Post date:** [August 14, 2020, 2:00pm UTC](https://forum.elixirforum.com/t/difficulty-understanding-program/33730/9 "2020-08-14T14:00:53Z")

</div>

Thanks a lot … Is there any reference book for Elixir

---

<div class="post-metadata">

**Author:** ![sabiwara](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sabiwara/32/18612_2.png) [@sabiwara](https://forum.elixirforum.com/u/sabiwara)\
**Post date:** [August 15, 2020, 7:20am UTC](https://forum.elixirforum.com/t/difficulty-understanding-program/33730/10 "2020-08-15T07:20:51Z")

</div>

I suppose it depends what you are looking for, it is a bit hard for me to answer. Maybe [this list](https://elixir-lang.org/learning.html) might help?

If I had to recommend myself, I would suggest starting with the [official Getting Started guide](https://elixir-lang.org/getting-started/introduction.html) and check [Elixir School](https://elixirschool.com/en/) as well.  
A great way to explore different parts of the language through practice could be [Elixir-koans](http://elixirkoans.io/).  
And if you are planning to use Elixir for web development, the [Phoenix official guide](https://hexdocs.pm/phoenix/overview.html#content) and [Programming Phoenix](https://pragprog.com/titles/phoenix14/programming-phoenix-1-4/) 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. 😉

---

<div class="post-metadata">

**Author:** ![hernytan](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/h/71e660/32.png) [@hernytan](https://forum.elixirforum.com/u/hernytan)\
**Post date:** [August 15, 2020, 8:07am UTC](https://forum.elixirforum.com/t/difficulty-understanding-program/33730/11 "2020-08-15T08:07:23Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![owaisqayum](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/owaisqayum/32/20018_2.png) [@owaisqayum](https://forum.elixirforum.com/u/owaisqayum)\
**Post date:** [August 15, 2020, 8:27am UTC](https://forum.elixirforum.com/t/difficulty-understanding-program/33730/12 "2020-08-15T08:27:11Z")

</div>

Thats some very good recourses. Thanks

---

<div class="post-metadata">

**Author:** ![owaisqayum](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/owaisqayum/32/20018_2.png) [@owaisqayum](https://forum.elixirforum.com/u/owaisqayum)\
**Post date:** [August 15, 2020, 8:29am UTC](https://forum.elixirforum.com/t/difficulty-understanding-program/33730/13 "2020-08-15T08:29:06Z")

</div>

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.
