owaisqayum
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
Trending in Discussions
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
New
The obligatory hello world thread!
Who are you and where are you from? :stuck_out_tongue:
New
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project.
My initial shotgu...
New
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog
It says that Fly is going all-in on sprites, which is a worry ...
New
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using.
We’re particularly inte...
New
Is there a word for the ~> symbol used in Version strings?
Do you also just call it a Squiggle Arrow™ ?!
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
Chat & Discussions>Discussions
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #ai
- #iex
- #graphql
- #elixirconf-us
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
slouchpie
I don’t see
requirein thesleepfunction. Did you mean “receive”?owaisqayum
ohh yes, its a typo. I mean
receivesabiwara
I think you will get a better picture of how
receiveworks 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
receivehere is not very clear about the intent, but it is basically listening for a message that is never going to come (no call tosendanywhere) with a timeout ofseconds, after which it returns.owaisqayum
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 ?
LostKobrakai
It works that way not because there’s no
send– other processes could send messages – but because there’s no “matching clause” forreceive.sabiwara
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”.
sabiwara
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 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
owaisqayum
Thanks a lot … Is there any reference book for Elixir
sabiwara
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.
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