-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountdown.exs
More file actions
36 lines (32 loc) · 791 Bytes
/
Copy pathcountdown.exs
File metadata and controls
36 lines (32 loc) · 791 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
defmodule Countdown do
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(
fn -> # num sec to start of next min
{_h, _m, s} = :erlang.time
60 - s - 1
end,
fn # wait for the next sec, then return its countdown
0 -> {:halt, 0}
count -> sleep(1)
{ [inspect(count)], count-1}
end,
fn _ -> end # nothing to deallocate
)
end
end
counter = Countdown.timer
printer = counter |> Stream.each(&IO.puts/1)
speaker = printer |> Stream.each(&Countdown.say/1)
speaker |> Enum.take(5)
Countdown.timer
|> Stream.each(&IO.puts/1)
|> Stream.each(&Countdown.say/1)
|> Enum.to_list