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
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.
Popular in Chat/Questions
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
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
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
Hi all,
I am a Fullstack JS developer for last 2 years. I need a good guide to learn elixer. Any suggestions please
New
In the attached screenshot (taken from an exercise in “Programming Elixir”) can be shown that BugReport is defined as a struct that, itse...
New
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
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
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
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
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
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...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
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
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
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
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
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
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
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
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









