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 Responses

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:

Where Next?

Popular in Discussions Top

Rustixir
Hi everyone, im working on find best language/framework/system for high concurrency, high performance and stable performance after wor...
New
arpan
Hello everyone :wave: Today I am very excited to announce a project that I have been working on for almost 3 months now. The project is...
New
praveenperera
How We Replaced React with Phoenix By: Thought Bot
New
mbenatti
Following https://github.com/tbrand/which_is_the_fastest |> https://raw.githubusercontent.com/tbrand/which_is_the_fastest/master/imgs...
New
AstonJ
I’ve just started the Phoenix part of the utterly brilliant online course by @pragdave. On generating the Phoenix app he uses the --no-ec...
New
marciol
Please, let me know if this kind of discussion already took place in another topic . Hi all, how do you consider if is better to build ...
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
chulkilee
Here are the list of HTTP client libraries/wrappers, and some thoughts on HTTP client in general. I’d like to hear from others how they w...
New
joeerl
I’m playing with Elixir - It’s fun. I think @rvirding does give Elixir courses these days. Re: files and database - when I given Erlang ...
New
griffinbyatt
Sobelow Sobelow is a security-focused static analysis tool for the Phoenix framework. For security researchers, it is a useful tool for g...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement