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:
tail \
|> Enum.each( fn line ->
new_line = head <> "/" <> line
end)
Here, instead of doing each
and trying to create some new variable outside the function, use
new_lines = Enum.map(tail, fn line -> head <> "/" <> line end)
Which 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”:
defmodule Index do
@spliter "---"
def index_from_file(index_path) do
{:ok, contents} = File.read(index_path)
build_index(contents)
end
def build_index(contents) do
contents
|> String.split(@spliter, trim: true)
|> Stream.map(&String.split(&1, "\n", trim: true))
|> Stream.filter(&(Enum.count(&1) > 0))
|> index_groups(%{})
end
defp index_groups([], index) do
index
end
defp index_groups([group | groups], index) do
[title | items] = group
items = Enum.map(items, fn item -> head <> "/" <> line)
index = Map.put(index, title, items)
index_groups(groups, index)
end
end
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_index
function. 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 uses Map.put
to 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 https://hexdocs.pm/elixir/Enum.html#reduce/3 function:
def build_index(contents) do
contents
|> String.split(@spliter, trim: true)
|> Stream.map(&String.split(&1, "\n", trim: true))
|> Stream.filter(&(Enum.count(&1) > 0))
|> Enum.reduce(%{}, fn group, index ->
[title | items] = group
items = Enum.map(items, fn item -> head <> "/" <> line)
Map.put(index, title, items)
end)
end
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/2
is 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.