alex.lau

alex.lau

find the unpaired items in a csv file

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
)

First Post!

kokolegorille

kokolegorille

Hello and welcome,

Using underscore prefix for variable has a special meaning.

You should not use _acc, but acc, because You are using it later.

Where Next?

Popular in Chat/Questions Top

xgilarb
Hi there, I’m interested in using Elixir because of the rumors about the reliability of the Phoenix framework, and surprisingly, Elixir’...
New
shansiddiqui94
Hello all, I recently did my first app in Phoenix and Liveview, many thanks to all the users who assisted me. I found that the tutorial ...
New
eliottramirez
Hello, I’m trying to learn Phoenix but I constantly find difficult understanding how the framework works, and I think part of this is th...
New
maqbool
what books/Resources do you recommend to learn about distributed system(theory)?
New
Nopp
Hello there, i have a lot to read and to learn, but are there some books, that are focussing on how i “should” plan the architecture of ...
New
New
Nopp
Hey guys and girls, i am completely “new” to programming, recently played a bit with Python, Ruby and PureBasic, but i want to try somet...
New
AstonJ
It finally feels like I’ve got some time to catch up on my reading - though I am sure lots of people will be wondering the same: what are...
New
Allyedge
So, I want to get an Elixir book, but don’t know which one to get. Both Programming Elixir 1.6 and Elixir in Action looks interesting, b...
New
koen_vb
Hi, I was looking for a pointer of how I could most easily start with phoenix regarding deploying it to something like linode or google c...
New

Other popular topics Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement