-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcountdown.exs
54 lines (46 loc) · 901 Bytes
/
countdown.exs
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
defmodule Countdown do
# def sleep(seconds) do
# receive do
# after seconds * 1000 -> nil
# end
# end
def sleep do
_sleep(current_second, 0) # 5
end
defp _sleep(initial_second, ticks) do
if current_second <= initial_second do
_sleep(initial_second, ticks + 1)
else
{:ok, ticks}
end
end
def current_second do
{_h, _m, second} = :erlang.time
second
end
def say(text) do
spawn fn -> :os.cmd('say #{text}') end
end
def timer do
Stream.resource(
fn ->
{_h, _m, s} = :erlang.time
60 - s - 1
end,
fn
0 ->
{:halt, 0}
count ->
IO.inspect sleep
{[inspect(count)], count - 1}
end,
fn _ -> end
)
end
def start do
timer
|> Stream.each(&IO.puts/1)
|> Stream.each(&Countdown.say/1)
|> Enum.to_list
end
end