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

davearonson
I am looking for smallish problems to solve, using Elixir concepts beyond the basic syntax and concepts of Elixir as a language, such as ...
New
RKC07
I’m new to elixir. I did some coding in python and C. I want to learn elixir for starting my career in web development. I need suggestion...
New
shansiddiqui94
Hello, I have an interview coming up and I seem to have forgotten important concepts of Elixir. So I was wondering if you guys know of a...
New
Santheepkumar
Hi all, I am a Fullstack JS developer for last 2 years. I need a good guide to learn elixer. Any suggestions please
New
OmanF
In the attached screenshot (taken from an exercise in “Programming Elixir”) can be shown that BugReport is defined as a struct that, itse...
New
aswinmohanme
I recently finished the Udemy course on Elixir and Phoenix and I am thinking about using it for the next project. But I am stuck as how t...
New
asfand
I am Asfandyar from Pakistan. This is my first time to the forum. I develop PHP websites using CodeIgniter, which is super easy to learn...
New
jace
I wanted to write a library for interacting with a Web API as a practical way of learning Elixir. However, there seem to be a lot of diff...
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
Twfo326
As a novice dev I’m trying to keep the curriculum as lean as possible. My requirements are modest: build simple CRUD apps with Phoenix...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement