hscspring
I have some data like:
---
title1
line1
line2
---
title2
line1
line2
and i want a structure of :
%{title1: [title1/line1, title1/line2], title2: [title2/line1, title2/line2}
Here is my code, but i cannot get the dict:
defmodule Index do
@spliter "---"
def build_custom_index(index_path) do
{:ok, contents} = File.read(index_path)
contents \
|> String.split(@spliter, trim: true)
|> Stream.map( &(String.split(&1, "\n", trim: true)) )
|> Stream.filter( &(Enum.count(&1) > 0) )
|> Enum.each( fn group ->
[head | tail] = group
tail \
|> Enum.each( fn line ->
new_line = head <> "/" <> line
end)
end)
end
end
This is actually my first Elixir program, I have searched several materials but still in trouble.
I have used Python before.
Would anyone help… many thanks.
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
benwilson512
Hey @hscspring welcome! The main thing to remember with Elixir is that you should always be thinking about returning values. You can never mutate data, so if you want something different than you have now, you need to construct some kind of function that will return the changed data. Here’s an example from your code:
Here, instead of doing
eachand trying to create some new variable outside the function, useWhich takes the tail list and returns a new list where the function has been applied to each item.
Overall you need two do two things: iterate through the groups, and maintain a dictionary. I’m going to show you two different ways to do this.
The first way will be to build a recursive function that walks through the groups “manually”:
As a simple thing, note that I split the idea of reading from a file from building the index. This is a common pattern in Elixir, where you try to maximize the number of functions that are “pure” and don’t depend on external inputs or outputs.
The main thing though is the recursive
build_indexfunction. It takes the list of groups as a first arg, and then the dictionary as the second arg. If there are no groups, then it just returns the index. If there is a group, it usesMap.putto return a new index containing the updated rows, and then recursively passes remaining groups and the update index to itself.This pattern is so common that there’s the handy Enum — Elixir v1.20.2 function:
There are a bunch of other little changes that could be done to the code, but hopefully this helps introduce the core concepts around iterating through data and returning new data.
Enum.each/2is honestly used pretty rarely, it’s only useful when you want to do some kind of side effect and you don’t care about keeping the result.hauleth
I haven’t tested it, but this should work. Just remember that
contentneed to be list of lines.hscspring
It’s really kind of you, I’ve gotten that. That’s quite interesting, thank you again.
hscspring
Thanks a lot. I’m trying.
hscspring
I’m sorry, i got an error like this:
and i have to change the code to:
then the code is ok. But i still do not understand the error .
i’ve found this: Elixir: (FunctionClauseError) no function clause matching - Stack Overflow, but i don’t think this is my problem.
i guess the problem may come from the
Stream, but i can’t find out that.Oh, by the way, the
reduceapproach worked well.benwilson512
Ah whoops I didn’t notice you were doing streams. Your solution is correct, you can also keep the
|>going by doing:As a tiny tweak:
should be:
Because this way you don’t need to actually count the length of the list
hscspring
oo, that’s so obvious, i didn’t even see it…
many thanks to u.