Skip to content

Commit 5e0ed45

Browse files
author
Bernard Pietraga
committed
Add exercise 2 from chapter 23
1 parent b2fba42 commit 5e0ed45

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

chapter_23/more_cool_stuff_2.exs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
defmodule ParseSygil do
2+
def sigil_z(lines, _options) do
3+
String.rstrip(lines)
4+
|> String.split("\n")
5+
|> Enum.map(&String.split(&1, ","))
6+
|> Enum.map(fn x -> convert_to_number(x) end)
7+
end
8+
9+
defp convert_to_number(column) do
10+
Enum.map(column, fn x ->
11+
case Float.parse(x) do
12+
{float, _} -> float
13+
_ -> x
14+
end
15+
end)
16+
end
17+
end
18+
19+
ExUnit.start
20+
21+
defmodule ParseSygilTest do
22+
import ParseSygil
23+
use ExUnit.Case
24+
25+
test "parse CSV like string" do
26+
expected = ~z"""
27+
1,2,3
28+
cat,dog
29+
"""
30+
31+
assert expected == [[1.0, 2.0, 3.0], ["cat", "dog"]]
32+
end
33+
34+
end

0 commit comments

Comments
 (0)