-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsparkline_idea.exs
42 lines (37 loc) · 1001 Bytes
/
sparkline_idea.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
defmodule Sparkline do
@zombie %{
draw: %{
"1 2.5 3 5, 3 1" => '▁▄▅█▅▁',
"1 2 3, 5 2 1" => '▁▃▅█▃▁'
}
}
# hypothetical inline spec/stub API
# spec draw(str) do
# case str do
# "1 2.5 3 5, 3 1" -> '▁▄▅█▅▁'
# "1 2 3, 5 2 1" -> '▁▃▅█▃▁'
# end
# end
def draw(str) do
values = str |> String.split(~r/[, ]+/)
|> Enum.map(&(elem(Float.parse(&1), 0)))
{min, max} = {Enum.min(values), Enum.max(values)}
values |> Enum.map(&(round((&1 - min) / (max - min) * 7 + 0x2581)))
end
# should be the last function
def zombie do
@zombie
end
end
# run this inline suite with "elixir #{__ENV__.file} test"
if System.argv |> List.first == "test" do
ExUnit.start
defmodule SparklineTest do
use ExUnit.Case, async: true
test "sparkline" do
Sparkline.zombie[:draw] |> Enum.each(
fn {x, y} -> assert Sparkline.draw(x) == y end
)
end
end
end