Say we have a csv file that has many trade items with its attributes. One of the attributes is the direction: in(1) or out(2). We expect the trades to be balanced (If there is a trade in, there is a trade out.) But there always are unbalanced trades. We should find out the unbalanced trades and check out the reason.
The csv file looks like this:
F_id,F_trade_id,F_direction,...
1,xxxx001,1,...
2,xxxx002,1,...
3,xxxx001,2,...
.......
Because I am learning elixir recently, so I am trying to solve this problem with elixir. And I found elixir is quite suitable for such tasks.
Here is the code snippet. In case you gays or myself from the future might need it.
{:ok, content} = File.read("trades.csv")
list = String.split(content, "\n") |> List.delete_at(0) |> Enum.map(&String.split(&1, ","))
unpaired = Enum.reduce(list, %{}, fn item, _acc ->
case Map.get(_acc, Enum.at(item,1)) do
nil ->
Map.put(_acc, Enum.at(item, 1), Enum.at(item,2))
_map ->
_acc = Map.delete(_acc, Enum.at(item,1))
end
end
)